我在UNIX的一个目录下有几百个PDF。PDF的名称非常长(约60个字符)。
当我尝试使用以下命令一起删除所有PDF时:
rm -f *.pdf
我收到以下错误:
/bin/rm: cannot execute [Argument list too long]
此错误的解决方案是什么?mv和cp命令是否也会出现此错误?如果是,如何解决这些命令?
我在UNIX的一个目录下有几百个PDF。PDF的名称非常长(约60个字符)。
当我尝试使用以下命令一起删除所有PDF时:
rm -f *.pdf
我收到以下错误:
/bin/rm: cannot execute [Argument list too long]
此错误的解决方案是什么?mv和cp命令是否也会出现此错误?如果是,如何解决这些命令?
当前回答
使用GNU并行(sudo apt install parallel)非常简单
它以多线程方式运行命令,其中“{}”是传递的参数
E.g.
ls/tmp/myfiles*|并行“rm{}”
其他回答
我用解决了
我在macOS上使用zsh
我只移动了数千个jpg文件。在一行命令中的mv内。
确保要移动的文件名中没有空格或特殊字符
for i in $(find ~/old -type f -name "*.jpg"); do mv $i ~/new; done
您可以使用bash数组:
files=(*.pdf)
for((I=0;I<${#files[@]};I+=1000)); do
rm -f "${files[@]:I:1000}"
done
这样,它将以每一步1000个文件的批量擦除。
rm命令对可以同时删除的文件有限制。
一种可能性是,您可以根据文件模式多次使用rm命令删除它们,例如:
rm -f A*.pdf
rm -f B*.pdf
rm -f C*.pdf
...
rm -f *.pdf
也可以通过find命令删除它们:
find . -name "*.pdf" -exec rm {} \;
对于这个问题,下面的选项似乎很简单。我从其他线程获得了这些信息,但它对我有所帮助。
for file in /usr/op/data/Software/temp/application/openpages-storage/*; do
cp "$file" /opt/sw/op-storage/
done
只需运行上面的一个命令,它就会完成任务。
我遇到过几次这个问题。许多解决方案将为每个需要删除的文件运行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一无所知的人都可以操作这段代码。