1

I have a directory on my Linux box with over 10000 files that I have to delete. Running...

sudo rm -rf /var/tmp/*

Gives the error message...

sudo: unable to execute /bin/rm: Argument list too long

The solution to this is to run

sudo find /var/tmp | xargs sudo rm

This only works for files with no spaces in the file name. However, some of the files have names with spaces in them and they are not deleted. For example, if a file is named 'A File With Spaces in the Name.dat', Running the command gives me errors like this....

rm: cannot remove `/var/tmp/A': No such file or directory
rm: cannot remove `File': No such file or directory
rm: cannot remove `With': No such file or directory
rm: cannot remove `Spaces': No such file or directory
rm: cannot remove `in': No such file or directory
rm: cannot remove `the': No such file or directory
rm: cannot remove `Name.dat': No such file or directory

How do I pass the complete file path to xargs sudo rm without breaking up the file name.

2 Answers 2

6

Tell find and xargs to use a different file separator:

find /var/tmp -print0 | sudo xargs -0 rm

Or tell find to execute rm directly:

sudo find /var/tmp -exec rm {} +

Or tell find to just delete the files:

sudo find /var/tmp -delete

Note: -mindepth 1 might be useful, to avoid deleting the /var/tmp directory itself.

...or really, just drop the *, it's not necessary if you were going to use recursive mode in the first place.

sudo rm -rf /var/tmp
4
  • @raido: Note the update. Jun 20, 2012 at 19:42
  • 1
    Just a detail. Avoid do sudo rm, it will be much slower if you got to execute sudo before each rm. Run the command in a root console instead.
    – fmanco
    Jun 20, 2012 at 19:53
  • @criziot: It shouldn't be much slower, since xargs and the -exec + form will execute rm only once for a whole ton of files. One could still change it to sudo xargs and sudo find to avoid the problem. Jun 20, 2012 at 21:16
  • @grawity: I didn't notice the -exec + form. In fact I was not even aware that it exists. But you're obviously right. Thanks
    – fmanco
    Jun 20, 2012 at 21:32
1

Just to add to the previous answer to avoid problem with deleting directories:

find /var/tmp -type f -exec sudo rm {} +

find /var/tmp -depth -type d -exec sudo rmdir {} +

rmdir -p can also be used for removing all descendant of a directory instead of using -depth.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .