Menu

Delete millions of files in linux

November 8, 2020
No Comments

How to delete millions of files in linux? The problem is quite well known and, most importantly, not as easy as it seems. Symptoms:

  • Unable to do ls in a folder, terminal hangs
  • Unable to remove as well in MC for the same reason I can’t access a folder.
  • Console utilities refuse to delete files because Argument list too long
  • Find ls etc will also give out How to remove millions of files in linux Argument list too long

If you have in the same directory the files you want and those you don’t, we have to delete files by mask, and here lies the main problem, most console tools will not be able to give you such a big list, and therefore work with it. The first to fail is rm, which will tell you it’s too big an array.

[su_note]rm -f /tmp/sess_*[/su_note]

Under a certain condition, it still works out as a clever variant of

[su_note]find /tmp/sess_* -type f -exec rm -v {} \;[/su_note]

By optimizing it we can add a couple hundred thousand more files and again we will have the same problem, too big array of data.

[su_note]find /tmp/sess_* -delete[/su_note]

The main problem is that I need to delete a certain file and not an entire folder or all files in it. As a result, to delete 14 000 000 files, I had to make a radical move. First, I had to abandon the regulars, because they required to build the whole tree. I also had to reject any utilities that build the entire list in favor of building it line by line.

We use the same find

[su_note]find /tmp -type f > /home/find.txt[/su_note]

Next, after getting the file, in my case it was about 200 megabytes, let’s put

[su_note]/tmp/sess* —> rm -f /tmp/sess[/su_note]

Leave a Reply

Your email address will not be published. Required fields are marked *