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

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

rm -f *.pdf

我收到以下错误:

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

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


当前回答

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

其他回答

你可以用这个表扬

find -name "*.pdf"  -delete

我在将源目录复制到目标目录时遇到了同样的问题

源目录包含文件约3 lakcs

我使用cp和选项-r,这对我很有用

cp-r abc/def/

它会将所有文件从abc复制到def,而不会发出参数列表过长的警告

您可以使用bash数组:

files=(*.pdf)
for((I=0;I<${#files[@]};I+=1000)); do
    rm -f "${files[@]:I:1000}"
done

这样,它将以每一步1000个文件的批量擦除。

我只知道一个办法。这个想法是将你拥有的pdf文件列表导出到一个文件中。然后将该文件分成几个部分。然后删除每个部分中列出的pdf文件。

ls | grep .pdf > list.txt
wc -l list.txt

wc-l是计算list.txt包含的行数。当你知道它有多长时,你可以决定把它分成两半、四等分或其他什么。使用split-l命令例如,将其分成600行。

split -l 600 list.txt

这将创建一个名为xaa、xab、xac等的文件,具体取决于如何拆分它。现在,要将这些文件中的每个列表“导入”到命令rm中,请使用以下命令:

rm $(<xaa)
rm $(<xab)
rm $(<xac)

对不起,我的英语不好。

我遇到过几次这个问题。许多解决方案将为每个需要删除的文件运行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一无所知的人都可以操作这段代码。