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

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?


The reason this occurs is because bash actually expands the asterisk to every matching file, producing a very long command line.

Try this:

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

Warning: this is a recursive search and will find (and delete) files in subdirectories as well. Tack on -f to the rm command only if you are sure you don't want confirmation.

You can do the following to make the command non-recursive:

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

Another option is to use find's -delete flag:

find . -name "*.pdf" -delete

tl;dr

It's a kernel limitation on the size of the command line argument. Use a for loop instead.

Origin of problem

This is a system issue, related to execve and ARG_MAX constant. There is plenty of documentation about that (see man execve, debian's wiki).

Basically, the expansion produce a command (with its parameters) that exceeds the ARG_MAX limit. On kernel 2.6.23 , the limit was set at 128 kB . This constant has been increased and you can get its value by executing:

getconf ARG_MAX
# 2097152 # on 3.5.0-40-generic

Solution

Use a for loop as it's recommended on BashFAQ/095 and there is no limit except for RAM/memory space:

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

Also this is a portable approach as glob have strong and consistant behavior among shells (part of POSIX spec).

If you insist, you can use find but really don't use xargs as it "is dangerous (broken, exploitable, etc.) when reading non-NUL-delimited input":

find . -name '*.pdf' -exec rm {} +

References

  • I'm getting "Argument list too long". How can I process a large list in chunks? @ wooledge
  • execve(2) - Linux man page (search for ARG_MAX) ;
  • Error: Argument list too long @ Debian's wiki ;
  • Why do I get “/bin/sh: Argument list too long” when passing quoted arguments? @ SuperUser

  • find有一个-delete行为:

    find . -maxdepth 1 -name '*.pdf' -delete
    
    链接地址: http://www.djcxy.com/p/97120.html

    上一篇: 从变量1中查找``占位符{} sed并将其放入变量2中

    下一篇: rm,cp,mv命令的参数列表太长错误