rm complains argument list too long
October 20th, 2007 mysurface Posted in Common, find, rm, xargs | Hits: 35998 | 5 Comments »
There is a limitation of rm command, where you can’t delete a large groups of files with *. For examples,
rm -rf something*
If there are large amount of files initiate with something, rm will fails and complains
/bin/rm: Argument list too long.
The solution is to make use of find, xargs and rm.
find . -name 'something*' -print0 | xargs -0 rm -rf







October 20th, 2007 at 12:52 pm
This works too. “ls | xargs rm”
October 20th, 2007 at 5:11 pm
Hi,
What I love Linux for is that one task can be done in several ways. So, to do what you need with rm, you can execute:
ls | while read file;do rm -rf $file;done
Not so short as previous one, but this one is more flexible… :)
October 20th, 2007 at 11:23 pm
There’s an even shorter way if you have GNU find:
find . -name ‘something*’ -delete
I’m not sure find programs have the -delete action, but GNU find does.
October 21st, 2007 at 1:19 am
danesh: That is much much shorter.
Artem : I totally agree with you.
Jim : Will try that when I have another brunch of rubbish to remove.
October 24th, 2007 at 9:54 pm
possibly faster than xargs and/or find
printf “rm %s\n” something*|sh