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'm working on a website for a client, and I just see a folder containing more than 1 million cache files.

I need to delete him, but i can't, i have this message : Argument list too long

I started to delete all files starting with aaa*, bbb*, ccc* .... but I do not want to spend my life... (rm -f aaa*.string.php)

How can i do that ?? Thanks !

share|improve this question

migrated from stackoverflow.com Feb 12 '13 at 13:31

This question came from our site for professional and enthusiast programmers.

1  
try find /directory/path -type f -delete –  Eddy_Em Feb 12 '13 at 10:23
    
add comment

3 Answers

up vote 5 down vote accepted

find and xargs are your friends, as always:

find -type f -print0 | xargs -0 rm

Or, for the special case of deletion (thanks, Eddy_Em):

find -type f -delete
share|improve this answer
1  
The first command is hardly any better than rm *. –  Lev Levitsky Feb 12 '13 at 10:25
1  
@LevLevitsky: Yes, it is, because xargs is aware of command line length limitations and calls rm more than once. –  thiton Feb 12 '13 at 10:25
    
Seems like I underestimated xargs, thanks for the info and +1 :) –  Lev Levitsky Feb 12 '13 at 10:29
    
How can i know if this command is working well ? I don't want to wait 4 hours for nothing^^ –  Clément Andraud Feb 12 '13 at 10:40
    
BTW there is another (not right, but available) way: unlink directory (without unlinking files) and run fsck. I didn't try its running time, but some people said, that it's even could be much less than find … -delete on some filesystems. –  Eddy_Em Feb 12 '13 at 10:53
show 2 more comments

Instead of removing the individual files, you could remove and then re-create the folder itself:

cd folder/..
rm -rf folder
mkdir folder

If this works in your scenario, this will probably be much faster than any method based on explicitly enumerating and removing the individual files.

share|improve this answer
    
Hum ok, Your solution killed my server ^^ –  Clément Andraud Feb 12 '13 at 10:27
    
I'll wait a bit for the command to stop –  Clément Andraud Feb 12 '13 at 10:27
add comment

Try

ls | xargs -n 200 rm -f

Will eventually remove everything

You can use grep to do pattern matching if you want to delete a subset of the files

share|improve this answer
1  
Be careful when piping into xargs, it will happily interpret spaces as delimiters. Have a space in your filename, and have neverending fun. Standard pattern is find -print0 and xargs -0. –  thiton Feb 12 '13 at 10:26
add comment

Your Answer

 
discard

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