How do you do a dry run of rm to see what files will be deleted?

I want to see what files will be deleted when performing an rm in linux. Most commands seem to have a dry run option to show just such information, but I can’t seem to find such an option for rm. Is this even possible?

Asked By: Cory Klein


You can say:

rm -i

to run it in interactive mode, so rm will prompt you to confirm whether each file should be deleted. You could just answer no to each file to see which ones would be affected.

Answered By: Justin Ethier

Say you want to run:

rm -- *.txt

You can just run:

echo rm -- *.txt

or even just:

echo *.txt

to see what files rm would delete, because it’s the shell expanding the *.txt, not rm.

The only time this won’t help you is for rm -r.

If you want to remove files and directories recursively, then you could use find instead of rm -r, e.g.

find . -depth -name "*.txt" -print

then if it does what you want, change the -print to -delete:

find . -depth -name "*.txt" -delete

(-delete implies -depth, we’re still adding it as a reminder as recommended by the GNU find manual).

Answered By: Mikel

You can use ls to list all the files that will be removed by rm:

ls ../path/*.txt

If you need to list to view the files that will be deleted with a recursive rm, then use the -R flag with ls:

ls -R ../path/*.txt
Answered By: Veneet Reddy

Just wanted to belatedly point out a handy solution that Joshua mentioned in the comments:

yes n | rm -i myFile1 myFile2 myFile3

This runs rm in interactive mode, which prompts a y/n response for each file it would delete, but via yes n it automatically declines this prompt for each file. The output isn’t super pretty, but is parseable with a little cleanup.

Answered By: Cory Klein
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.