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?
And another one:
printf
is a shell builtin, and as far as I know it's always been as such. Now given thatprintf
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, throughxargs
, which makes sure it fits enough file names in the command line so as not to fail therm
command, which is a shell command.The
\0
inprintf
serves as a null separator for the file names wich are then processed byxargs
command, using it (-0
) as a separator, sorm
does not fail when there are white spaces or other special characters in the file names.find . -type f -name '*xxx' -print -delete
The below option seems simple to this problem. I got this info from some other thread but it helped me.
Just run the above one command and it will do the task.
you can use this commend
If they are filenames with spaces or special characters, use:
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.
find
has a-delete
action: