I have a list of 1M folders/directory, which I need to delete on my system. What is the best possible way to do it ?
I am looking for best possible solution, which will not consume lot of time, as I a have some processes which will be waiting for its completion.
PS: I can put all folders name in a file, if required, or do it in batch, if we can not do it in one go.
Use the xargs
tool. It will read all folder names from the file and call a command - in this case rmdir
.
xargs rmdir < list_of_folders
If you are sure you can delete non-empty folders, use rm -r
instead of rmdir
.
I think this is about the fastest you can get. rmdir
will act as fast as it can (simple OS call), and using xargs
ensures that you do not create 1M separate processes.
You may exploit that there are no "nested" directories in your list.
By that, if you have three folder a/
a/b/
and a/c/
, and b/
and c/
are the only entries in a/
, then you can omit a/b/
and a/c/
and just call rm -r a/
.
But it will not be worth checking that by ls
, as ls
will also cost time which you probably won't save.