Linux, How to delete a folder with more than 1 million files?

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 !

Clément Andraud

Posted 2013-02-12T10:21:39.267

Reputation:

1try find /directory/path -type f -delete – Eddy_Em – 2013-02-12T10:23:32.970

See Perl to the rescue: case study of deleting a large directory

– choroba – 2013-02-12T10:32:41.910

Answers

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

thiton

Posted 2013-02-12T10:21:39.267

Reputation: 826

1The first command is hardly any better than rm *. – Lev Levitsky – 2013-02-12T10:25:02.983

2@LevLevitsky: Yes, it is, because xargs is aware of command line length limitations and calls rm more than once. – thiton – 2013-02-12T10:25:46.583

Seems like I underestimated xargs, thanks for the info and +1 :) – Lev Levitsky – 2013-02-12T10:29:30.277

How can i know if this command is working well ? I don't want to wait 4 hours for nothing^^ – None – 2013-02-12T10:40:48.953

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 – 2013-02-12T10:53:39.800

@Clément Andraud: Either slap a -v on the rm command (but this will print an enormous amount of data if there are as many files as you say, and actually slow the process down considerably), or execute e.g. watch -n 10 'du -ms directory' in another shell and see the file size going down. – Daniel Andersson – 2013-02-12T13:42:48.760

GNU find has the -exec + switch which makes the xargs pipe unnecessary. E.g. find dir -type f -exec rm {} + will also minimize process invocations in the same way as xargs, but the piping isn't needed. As compared to -delete, this will enable switches to be added to rm, just as with xargs. – Daniel Andersson – 2013-02-12T13:45:39.353

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.

NPE

Posted 2013-02-12T10:21:39.267

Reputation: 1 169

Hum ok, Your solution killed my server ^^ – None – 2013-02-12T10:27:26.257

I'll wait a bit for the command to stop – None – 2013-02-12T10:27:54.540

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

Ed Heal

Posted 2013-02-12T10:21:39.267

Reputation: 149

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 – 2018-08-11T10:29:28.880

2Be 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 – 2013-02-12T10:26:50.857