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?

2
  • 10
    Try watch -n 5 du -s LargeDirectory. Watch as it goes to zero. Jan 13, 2016 at 22:04
  • @MarkPlotnick: on MacOS, have to install it with brew install watch, or see here for instructions.
    – smci
    Jun 1, 2019 at 3:35

6 Answers 6

39

from

man rm 

use the -v option:

-v, --verbose
explain what is being done
2
  • 19
    that’s not progress though. how does “now deleting foo/bar” help you determine how far the complete thing is? May 23, 2016 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, 2017 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.

4
  • 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). Mar 12, 2018 at 19:38
  • This actually is what I was looking for, thank you. Other comments left something lacking.
    – Hellreaver
    Aug 30, 2019 at 8: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, 2022 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, 2023 at 8: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

4
  • 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 6, 2017 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 Apr 7, 2017 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, 2020 at 14:12
  • @zupazt3 it looks like Oh My Zsh (ohmyz.sh)
    – onewhaleid
    Feb 23 at 12:09
5

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)

1
  • 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, 2023 at 8: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)

2
  • 1
    rm doesn't write to stdout, and watch doesn't read from stdin, so the pipe between them is pointless. If you want to run them at the same time try rm -f * & watch 'ls | wc -l' or consider rm -v * Mar 1, 2020 at 18:56
  • hmm, okay. Thanks Mar 1, 2020 at 19:58
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .