Argument list too long error for rm, cp, mv comman

2018-12-31 16:29发布

I have several hundred PDFs under a directory in UNIX. The names of the PDFs are really long (approx. 60 chars).

When I try to delete all PDFs together using the following command:

rm -f *.pdf

I get the following error:

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

What is the solution to this error? Does this error occur for mv and cp commands as well? If yes, how to solve for these commands?

30条回答
只靠听说
2楼-- · 2018-12-31 16:46

And another one:

cd  /path/to/pdf
printf "%s\0" *.[Pp][Dd][Ff] | xargs -0 rm

printf is a shell builtin, and as far as I know it's always been as such. Now given that printf is not a shell command (but a builtin), it's not subject to "argument list too long ..." fatal error.

So we can safely use it with shell globbing patterns such as *.[Pp][Dd][Ff], then we pipe its output to remove (rm) command, through xargs, which makes sure it fits enough file names in the command line so as not to fail the rm command, which is a shell command.

The \0 in printf serves as a null separator for the file names wich are then processed by xargs command, using it (-0) as a separator, so rm does not fail when there are white spaces or other special characters in the file names.

查看更多
裙下三千臣
3楼-- · 2018-12-31 16:48

find . -type f -name '*xxx' -print -delete

查看更多
查无此人
4楼-- · 2018-12-31 16:48

The below option seems simple to this problem. I got this info from some other thread but it helped me.

for file in /usr/op/data/Software/temp/application/openpages-storage/*; do
    cp "$file" /opt/sw/op-storage/
done

Just run the above one command and it will do the task.

查看更多
余生无你
5楼-- · 2018-12-31 16:50

you can use this commend

find -name "*.pdf"  -delete
查看更多
梦该遗忘
6楼-- · 2018-12-31 16:50

If they are filenames with spaces or special characters, use:

find -maxdepth 1 -name '*.pdf' -exec rm "{}" \;

This sentence search all files in the current directory (-maxdepth 1) with extension pdf (-name '*.pdf'), and then, delete each one (-exec rm "{}").

The expression {} replace the name of the file, and, "{}" set the filename as string, including spaces or special characters.

查看更多
与风俱净
7楼-- · 2018-12-31 16:51

find has a -delete action:

find . -maxdepth 1 -name '*.pdf' -delete
查看更多
登录 后发表回答