【Linux】Linux argument list too long錯(cuò)誤解決方法

問題1: 在某個(gè)文件夾執(zhí)行命令完全卡死

在某個(gè)文件夾執(zhí)行命令完全卡死,(ctrl+z,ctrl+c等都不能用),表現(xiàn)為:

  1. 在文件夾執(zhí)行l(wèi)s等命令卡死

  2. 在服務(wù)器任何地方執(zhí)行df -h與du -sh卡死(sudo fdisk -l管用)

  3. cd 文件夾卡死

只好關(guān)閉當(dāng)前shell,重新登錄。

產(chǎn)生原因:
該文件夾中有一個(gè)服務(wù)掛載在該文件夾某一目錄下,因突然關(guān)機(jī)等異常情況導(dǎo)致該服務(wù)無限制等待,機(jī)器重啟后也不能正常連接。

解決方法:
查看與該文件夾相應(yīng)的服務(wù)掛載情況:mount -l | grep 有問題文件夾名,如mount -l | grep "/root/bakup"

image.png

umount -l 文件夾,此刻,再訪問文件夾即可使用命令了!

問題2: 目錄下文件太多,執(zhí)行命令報(bào)錯(cuò) arguments too long

作為一個(gè)linux用戶/系統(tǒng)管理員, 有些時(shí)候你會(huì)遇到以下錯(cuò)誤提示:

ls  /path/to/
rm  -rf  /path/to/

bash: /bin/ls: Argument list too long
bash: /bin/rm: Argument list too long

產(chǎn)生原因:
“Argument list too long”參數(shù)列表過長(zhǎng)錯(cuò)誤經(jīng)常發(fā)生在用戶在一行簡(jiǎn)單命令中提供了過多的參數(shù)而導(dǎo)致,經(jīng)常在ls *, cp *, rm * 等中出現(xiàn),一般是因?yàn)槭艿?shell 參數(shù)個(gè)數(shù)限制所致

解決方法:

方法1 : 將文件群手動(dòng)劃分為比較小的組合

mv [a-l]*  /path/to/foo/
mv [m-z]*  /path/to/foo/

這是最基本的方法,只是簡(jiǎn)單的使參數(shù)數(shù)量符合要求,這種方法應(yīng)用范圍有限,只適用于文件列表中的名字分布比較均勻,另外這也是個(gè)初級(jí)用戶可以考慮的解決方案,不過需要很多重復(fù)命令和對(duì)文件名分布的觀察與猜測(cè)。

方法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命令,使其一次處理一個(gè),這樣就完全避免了過量參數(shù)的存在,另外通過不同的參數(shù),可以指定除了名稱以外的時(shí)間戳,權(quán)限,以及inode等匹配模式。

方法3: 使用xargs命令

用xargs 命令將文件以每 100 個(gè)為一組為單位處理

cd  /path/to/foo/
ls *.jpg  |  xargs -n 100    rm -rf

xargs 命令會(huì)將文件以每 100 個(gè)為一組做 rm 處理。

方法4: 使用 ls 結(jié)合 awk 操作

可以使用 awk 一次刪除一個(gè)的方式進(jìn)行刪除,但必須先進(jìn)入該目錄下。

cd  /path/to/bar/
ls -l |  awk  '{ print "rm -f ",$9}' |  sh

參考

Linux 命令 argument list too long 錯(cuò)誤解決
https://blog.51cto.com/u_14782715/5082973
https://blog.csdn.net/fdipzone/article/details/41558461

Linux刪除文件出現(xiàn)/bin/rm: Argument list too long解決方法
https://southcat.net/1481.html

Linux下經(jīng)過 rm -f 刪除大量文件時(shí)報(bào)錯(cuò):Argument list too long
http://www.javashuo.com/article/p-pucmuhoh-v.html

Linux文件系統(tǒng)十問
https://mp.weixin.qq.com/s/pOKjwl3ONPMPSRF6RSmvaw

新建一個(gè)空文件占用多少磁盤空間?
https://mp.weixin.qq.com/s/9YeUEnRnegplftpKlW4ZCA

文件過多時(shí)ls命令為什么會(huì)卡住?
https://mp.weixin.qq.com/s/g-fFoYsBJkonV3ezdGDJKA

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容