Debian - deleting thousands of files

Deleting the directory should work. You might run in to trouble if you are trying to delete individual files because there are so many. You are not running rm -rf dirname/* are you?

My guess is that you are loosing the ssh connection because of inactivity, are you sure the command was not executed correctly on the server?

In any case, a possible workaround would be to use nohup:

nohup rm -rf dirname

You can launch the command and log off, it will continue in the background.


UPDATE:

I found this question over at U&L.SE, I recommend you have a look at it. Unfortunately, I cannot recreate your problem. I have made a directory with 2516007 files and could always delete it using rm -rf:

$ mkdir ha
$ for X in $(seq 1 10000);do touch {1..10000}_$X; done
$ ls -f ha | wc -l 
  2516007     <= I stopped before the for loop finished, but this should be enough
$ time rm -rf ha/
  real  23m11.695s
  user  0m3.540s
  sys           0m42.891s

So, it took 20 minutes to delete ~2 million empty files. It seems reasonable that it could take hours to delete a huge amount of non-empty files.

Anyway, some of the suggestions from the U&L question should help. Specifically this one:

find /delb -type f  --delete

or this one (slightly modified from the original)

find /delb -type f -print0 | xargs -0r rm -f

Finally, a trick you could do is delete a few files at a time. If you know that some file names start with the string "foo", others "bar" etc, do this:

rm -rf /delb/foo*
rm -rf /delb/bar*

and so on. You could also try something like this:

for n in {a..z}; do rm -rf /delb/$n*; done
for n in {A..Z}; do rm -rf /delb/$n*; done
for n in {0..9}; do rm -rf /delb/$n*; done

This last command will sequentially delete each file name beginning with each letter of the alphabet, then any file names beginning with numbers.

Tags:

Linux

Debian