問題1: 在某個文件夾執行命令完全卡死
在某個文件夾執行命令完全卡死,(ctrl+z,ctrl+c等都不能用),表現為:
在文件夾執行ls等命令卡死
在服務器任何地方執行df -h與du -sh卡死(sudo fdisk -l管用)
cd 文件夾卡死
只好關閉當前shell,重新登錄。
產生原因:
該文件夾中有一個服務掛載在該文件夾某一目錄下,因突然關機等異常情況導致該服務無限制等待,機器重啟后也不能正常連接。
解決方法:
查看與該文件夾相應的服務掛載情況:mount -l | grep 有問題文件夾名,如mount -l | grep "/root/bakup"
umount -l 文件夾,此刻,再訪問文件夾即可使用命令了!
問題2: 目錄下文件太多,執行命令報錯 arguments too long
作為一個linux用戶/系統管理員, 有些時候你會遇到以下錯誤提示:
ls /path/to/
rm -rf /path/to/
bash: /bin/ls: Argument list too long
bash: /bin/rm: Argument list too long
產生原因:
“Argument list too long”參數列表過長錯誤經常發生在用戶在一行簡單命令中提供了過多的參數而導致,經常在ls *, cp *, rm * 等中出現,一般是因為受到 shell 參數個數限制所致
解決方法:
方法1 : 將文件群手動劃分為比較小的組合
mv [a-l]* /path/to/foo/
mv [m-z]* /path/to/foo/
這是最基本的方法,只是簡單的使參數數量符合要求,這種方法應用范圍有限,只適用于文件列表中的名字分布比較均勻,另外這也是個初級用戶可以考慮的解決方案,不過需要很多重復命令和對文件名分布的觀察與猜測。
方法2 : 使用find命令
find /path/to/foo/ -name "*.jpg" -type f -exec rm -rf {} \;
find /path/to/bar/ -type f -name '201301*sms.txt' |xargs -t -I {} mv {} /path/to/fooo/
-exec command
Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of #;' is encountered. The string {}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a ') or quoted to protect them from expansion by the shell. The command is executed in the starting directory.
通過find命令,將文件清單輸出到rm命令,使其一次處理一個,這樣就完全避免了過量參數的存在,另外通過不同的參數,可以指定除了名稱以外的時間戳,權限,以及inode等匹配模式。
方法3: 使用xargs命令
用xargs 命令將文件以每 100 個為一組為單位處理
cd /path/to/foo/
ls *.jpg | xargs -n 100 rm -rf
xargs 命令會將文件以每 100 個為一組做 rm 處理。
方法4: 使用 ls 結合 awk 操作
可以使用 awk 一次刪除一個的方式進行刪除,但必須先進入該目錄下。
cd /path/to/bar/
ls -l | awk '{ print "rm -f ",$9}' | sh
參考
Linux 命令 argument list too long 錯誤解決
https://blog.51cto.com/u_14782715/5082973
https://blog.csdn.net/fdipzone/article/details/41558461
Linux刪除文件出現/bin/rm: Argument list too long解決方法
https://southcat.net/1481.html
Linux下經過 rm -f 刪除大量文件時報錯:Argument list too long
http://www.javashuo.com/article/p-pucmuhoh-v.html
Linux文件系統十問
https://mp.weixin.qq.com/s/pOKjwl3ONPMPSRF6RSmvaw
新建一個空文件占用多少磁盤空間?
https://mp.weixin.qq.com/s/9YeUEnRnegplftpKlW4ZCA
文件過多時ls命令為什么會卡???
https://mp.weixin.qq.com/s/g-fFoYsBJkonV3ezdGDJKA