HOWTO delete many files in Linux
Manipulating huge amounts of files with PHP I have made a mistake and created lots of directories (that means LOTS! ~10mil) and each directory contains ~10k files. So when trying to see the contents of the directory with ls machine just stalls.
So I have a need to remove all bogus files and directories to regain access to the folder. In the directory I have tried:
Code: rm -rf *
But that's no good as the are TOO MANY files and directories for it to handle and it give the error: "argument list too long: rm". Done some research and it seems the only god way is to pipe file by file to rm comand using find, just like the example below:
Code: find . -name ‘*’ | xargs rm
Great I though! but it did not removes the files that contains the spaces in them, so here is another solution that will accept all the files and directories:
Code: find . -name ‘*’ -exec rm {} \;
Now this is a bad thing if you want to remove only from the current directry as it will search in subdirectories too so we need to add a '-maxdepth 0' to the command just like this:
Code: find . -maxdepth 0 -name ‘*’ -exec rm {} \;
Well, in my case I did not needed to restrict the directory depth, but I thought that could be helpful for someone with that problem.
|