Deleting millions of files in a linux directory
Sep 23, 2009 #sveltekit#svelte
Today I found myself with a directory containing about 6 million files which shouldn’t have been there in the first place. Any attempt to ls or rm failed.
Thank god for the “strace” command that I discovered a few weeks ago I was able to delete the files I wanted.
Here are the steps :
cd directory-with-millions-of-files
ls -l &
#[1] 7453
strace -p 7453 2>&1 | grep lstat | cut -d ‘”‘ -f 2 > /tmp/to_delete.list
of course 7453
needs to be changed to the proper pid
wait about a minute and then ctrl-c to stop strace and kill -9 7453 to stop the ls
for i in `cat /tmp/to_delete.list`; do rm ./$i; done
What we did is basically:
- launched the
ls -l
as a background process which outputs the process id. - strace this pid which shows us all that’s going on internally in
ls -l
. - strace echoes to much stuff on the std and err console. we apply some refirecting, grepping and cutting to get the filename which we write into a tmp file
- after about a minute on my system, the file already has a million lines, we kill strace and ls
- last but not least, we loop over the lines in out tmp file and start deleting the files.
- You could change your grep to only target specific files/extensions
Have fun