44

Using rm -rf LargeDirectory to delete a large directory can take a large amount of time to complete depending on the size of the directory.

Is it possible to get a status update or somehow monitor the progress of this deletion to give a rough estimate as to where along in the process the command is?

John
  • 1,081

6 Answers6

39

from

man rm 

use the -v option:

-v, --verbose
explain what is being done
xyx
  • 757
  • 18
    that’s not progress though. how does “now deleting foo/bar” help you determine how far the complete thing is? – flying sheep May 23 '16 at 13:03
  • 2
    @flyingsheep believe it removes everything in alphabetical order, at least in OSX it does, so it does help in some way.. – Tim Baas May 11 '17 at 15:22
31

I see the question is old. I want to share what it works for me to maybe help some else.

I get the progress bar using pv command line Pipe Viewer

This is the command

rm -rv DIR_OR_FILE_NAME | pv -l -s $( du -a DIR_OR_FILE_NAME | wc -l ) > /dev/null

If you need root permissions for the dir or file to delete,

sudo rm -rv DIR_OR_FILE_NAME | pv -l -s $( sudo du -a DIR_OR_FILE_NAME | wc -l ) > /dev/null
  • rm -rv: -r to recursively remove DIRs and files. -v verbose it lists all the files and directories that is removing.

  • pv -l -s: -l to count lines instead of bytes. -s set the total lines to be removed.

  • $( du -a <dir_or_file> | wc -l ): du -a returns a list all files and directories from the dir specified. wc -l returns the count of lines outputted by du -a.

  • > /dev/null: send the output of rm -rv to nowhere.

Ezequiel
  • 311
  • Good idea.  This may give an incorrect file/directory count if any names contain newlines; find {dir} -printf . | wc -c would be safer (but -printf is a GNU extension). – Scott - Слава Україні Mar 12 '18 at 19:38
  • This actually is what I was looking for, thank you. Other comments left something lacking. – Hellreaver Aug 30 '19 at 08:03
  • du will fail on items that were already deleted too quickly. Those errors can probably just be silenced, maybe by filtering stderr or wherever they are sent. Otherwise a great solution for long deletions. – Atorian Dec 19 '22 at 15:11
  • This is AMAZING! It would have helped a few years ago if I'd have known about it - I was moving large chunks of data around and had no idea how long each move would take. – KolonUK Feb 17 '23 at 08:54
7

You can view the progress of any currently running commands with Coreutils Progress Viewer(cv). It isn't like issuing a single command, but you can see the progress with it.

example of CV being used

I'm sure someone can come up with an alias to run this with the command. It also works with cp, mv, dd, tar, gzip/gunzip, cat, etc.. More details about it can be found at

gitthub Xfennec/progress

Patronics
  • 103
  • 5
    Since progress (as "cv" is now known) tracks open files and rm does not open the files it deletes, this may not actually be very useful. – dhag Apr 06 '17 at 16:40
  • 2
    @dhag Your right, I thought that this would work for rm. Now I'm debating whether to remove the answer. hmm – Ashitakalax Apr 07 '17 at 19:29
  • 1
    @Ashitakalax It's a bit off-topic, but how to get this prompt that you're using? Looks really cool. – zupazt3 Jan 30 '20 at 14:12
  • @zupazt3 it looks like Oh My Zsh (https://ohmyz.sh/) – onewhaleid Feb 23 '24 at 12:09
4

If the directory to be removed has a very large number of files, du command might take a long time to compute the size of remaining files.

If you have a general sense of how much space the files take up or how much free space there should be after deleting the files, then as an alternative, while the rm command is running, in a separate terminal session/window, try watching how much free-space is left/used on the HDD.

For example:

Run watch df -h (all devices) or watch df -h /dev/path/to/disk (specific HDD)

Justas
  • 141
  • 1
    This was perfect for my purposes, monitoring the progress of deleting an old time machine backup on a NAS to free up space, where the directory was big enough that even running ls would hang for a while. – Patronics Mar 23 '23 at 08:10
3
rm * & watch 'ls -1 | wc -l'

It removes all files and watch remaining number of files (in current directory)

rm * & watch 'du -h'

it removes all files and watch remaining directory size (in current directory)

You can customize 'rm' command, e.g. 'rm -rf *', removing without confirmation all files/directories recursively (in current directory)

3

Cross-posting from https://github.com/tqdm/tqdm/issues/991, if you have tqdm installed (snap install tqdm, conda install tqdm, pip3 install tqdm, etc) then

rm -rfv LargeDirectory | tqdm --total $(du -a LargeDirectory | wc -l) >/dev/null