我在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命令是否也会出现此错误?如果是,如何解决这些命令?
当前回答
对于没有时间的人。在终端上运行以下命令。
ulimit -S -s unlimited
然后执行cp/mv/rm操作。
其他回答
还有一个:
cd /path/to/pdf
printf "%s\0" *.[Pp][Dd][Ff] | xargs -0 rm
printf是一个内置的shell,据我所知,它一直都是这样的。既然printf不是一个shell命令(而是一个内置命令),它就不会出现“参数列表太长…”致命错误。
因此,我们可以安全地将其与*等shell globbing模式一起使用。[Pp][Dd][Ff],然后我们通过xargs将其输出通过管道传输到remove(rm)命令,这确保它在命令行中适合足够的文件名,以免rm命令(shell命令)失败。
printf中的\0用作文件名的空分隔符,然后由xargs命令处理,使用它(-0)作为分隔符,因此当文件名中有空格或其他特殊字符时,rm不会失败。
参数列表太长
作为cp、mv和rm的问题标题,但答案主要代表rm。
Un*x命令
仔细阅读命令的手册页!
对于cp和mv,有一个-t开关,用于目标:
find . -type f -name '*.pdf' -exec cp -ait "/path to target" {} +
and
find . -type f -name '*.pdf' -exec mv -t "/path to target" {} +
脚本方式
bash脚本中使用了一个整体解决方案:
#!/bin/bash
folder=( "/path to folder" "/path to anther folder" )
if [ "$1" != "--run" ] ;then
exec find "${folder[@]}" -type f -name '*.pdf' -exec $0 --run {} +
exit 0;
fi
shift
for file ;do
printf "Doing something with '%s'.\n" "$file"
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一无所知的人都可以操作这段代码。
对于这个问题,下面的选项似乎很简单。我从其他线程获得了这些信息,但它对我有所帮助。
for file in /usr/op/data/Software/temp/application/openpages-storage/*; do
cp "$file" /opt/sw/op-storage/
done
只需运行上面的一个命令,它就会完成任务。
如果你想删除30/90天以上(+)或30/90天以下(-)的文件/文件夹,可以使用以下ex命令
例如:对于删除90天文件/文件夹后的90天不包括上述内容,这意味着91、92……100天
find <path> -type f -mtime +90 -exec rm -rf {} \;
例如:对于您想要删除的最近30天的文件,请使用以下命令(-)
find <path> -type f -mtime -30 -exec rm -rf {} \;
如果你想保存文件超过2天
find <path> -type f -mtime +2 -exec gzip {} \;
如果您只想查看过去一个月的文件/文件夹。前任:
find <path> -type f -mtime -30 -exec ls -lrt {} \;
30天以上,然后列出文件/文件夹前任:
find <path> -type f -mtime +30 -exec ls -lrt {} \;
find /opt/app/logs -type f -mtime +30 -exec ls -lrt {} \;