2

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 !

2

3 Answers 3

8

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
7
  • 1
    The first command is hardly any better than rm *. Feb 12, 2013 at 10:25
  • 2
    @LevLevitsky: Yes, it is, because xargs is aware of command line length limitations and calls rm more than once.
    – thiton
    Feb 12, 2013 at 10:25
  • Seems like I underestimated xargs, thanks for the info and +1 :) Feb 12, 2013 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, 2013 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, 2013 at 10:53
3

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.

2
  • Hum ok, Your solution killed my server ^^
    – Clément Andraud
    Feb 12, 2013 at 10:27
  • I'll wait a bit for the command to stop
    – Clément Andraud
    Feb 12, 2013 at 10:27
0

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

2
  • 2
    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, 2013 at 10:26
  • this will be slower than find and won't work for files with spaces or new lines in their names. See Why not parse ls?
    – phuclv
    Aug 11, 2018 at 10:29

You must log in to answer this question.