Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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
share|improve this answer
    
Brilliant! Thanks! –  b10hazard Jun 20 '12 at 18:53
    
@raido: Note the update. –  grawity Jun 20 '12 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. –  criziot Jun 20 '12 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. –  grawity Jun 20 '12 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 –  criziot Jun 20 '12 at 21:32

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.