我在UNIX的一个目录下有几百个PDF。PDF的名称非常长(约60个字符)。

当我尝试使用以下命令一起删除所有PDF时:

rm -f *.pdf

我收到以下错误:

/bin/rm: cannot execute [Argument list too long]

此错误的解决方案是什么?mv和cp命令是否也会出现此错误?如果是,如何解决这些命令?


当前回答

我遇到过几次这个问题。许多解决方案将为每个需要删除的文件运行rm命令。这是非常低效的:

find . -name "*.pdf" -print0 | xargs -0 rm -rf

我最终编写了一个python脚本,根据文件名中的前4个字符删除文件:

import os
filedir = '/tmp/' #The directory you wish to run rm on 
filelist = (os.listdir(filedir)) #gets listing of all files in the specified dir
newlist = [] #Makes a blank list named newlist
for i in filelist: 
    if str((i)[:4]) not in newlist: #This makes sure that the elements are unique for newlist
        newlist.append((i)[:4]) #This takes only the first 4 charcters of the folder/filename and appends it to newlist
for i in newlist:
    if 'tmp' in i:  #If statment to look for tmp in the filename/dirname
        print ('Running command rm -rf '+str(filedir)+str(i)+'* : File Count: '+str(len(os.listdir(filedir)))) #Prints the command to be run and a total file count
        os.system('rm -rf '+str(filedir)+str(i)+'*') #Actual shell command
print ('DONE')

这对我来说非常有效。我能够在大约15分钟内清除一个文件夹中超过200万个临时文件。我从一点点代码中对tar进行了注释,这样任何一个对python一无所知的人都可以操作这段代码。

其他回答

如果它们是带有空格或特殊字符的文件名,请使用:

find -name "*.pdf"  -delete

仅适用于当前目录中的文件:

find -maxdepth 1 -name '*.pdf' -delete

这句话搜索当前目录(-maxdeph 1)中扩展名为pdf(-name'*.pdf')的所有文件,然后删除。

之所以出现这种情况,是因为bash实际上将星号扩展到每个匹配的文件,从而产生一个非常长的命令行。

试试看:

find . -name "*.pdf" -print0 | xargs -0 rm

警告:这是一个递归搜索,也会在子目录中查找(和删除)文件。只有当您确定不需要确认时,才在rm命令中使用-f。

可以执行以下操作以使命令非递归:

find . -maxdepth 1 -name "*.pdf" -print0 | xargs -0 rm

另一个选项是使用find的-delete标志:

find . -name "*.pdf" -delete

当一个应用程序创建了数百万无用的日志文件,填满了所有inode时,我也遇到过类似的问题。我求助于“定位”,将所有“定位”的文件放入文本文件,然后逐一删除。花了一段时间,但完成了任务!

你可以试试这个:

for f in *.pdf
do
  rm "$f"
done

编辑:ThiefMaster的评论建议我不要向年轻的壳牌公司的绝地武士透露这种危险的做法,所以我会添加一个更“安全”的版本(为了在有人拥有“-rf…pdf”文件时保存东西)

echo "# Whooooo" > /tmp/dummy.sh
for f in '*.pdf'
do
   echo "rm -i \"$f\""
done >> /tmp/dummy.sh

运行以上步骤后,只需在您喜爱的编辑器中打开/tmp/ddumy.sh文件,并检查每一行是否有危险的文件名,如果发现,请将其注释掉。

然后在工作目录中复制dummy.sh脚本并运行它。

所有这些都是出于安全考虑。

我遇到过几次这个问题。许多解决方案将为每个需要删除的文件运行rm命令。这是非常低效的:

find . -name "*.pdf" -print0 | xargs -0 rm -rf

我最终编写了一个python脚本,根据文件名中的前4个字符删除文件:

import os
filedir = '/tmp/' #The directory you wish to run rm on 
filelist = (os.listdir(filedir)) #gets listing of all files in the specified dir
newlist = [] #Makes a blank list named newlist
for i in filelist: 
    if str((i)[:4]) not in newlist: #This makes sure that the elements are unique for newlist
        newlist.append((i)[:4]) #This takes only the first 4 charcters of the folder/filename and appends it to newlist
for i in newlist:
    if 'tmp' in i:  #If statment to look for tmp in the filename/dirname
        print ('Running command rm -rf '+str(filedir)+str(i)+'* : File Count: '+str(len(os.listdir(filedir)))) #Prints the command to be run and a total file count
        os.system('rm -rf '+str(filedir)+str(i)+'*') #Actual shell command
print ('DONE')

这对我来说非常有效。我能够在大约15分钟内清除一个文件夹中超过200万个临时文件。我从一点点代码中对tar进行了注释,这样任何一个对python一无所知的人都可以操作这段代码。