103

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?

  • I would also like to see which files will generate errors for lack of permissions. – Simon Woodside Feb 10 '17 at 22:19
138

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 . -name "*.txt" -print

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

find . -name "*.txt" -delete
| improve this answer | |
  • find is a good choice IMO. And if you want a graphical presentation use tree. – noisebleed Jul 25 '12 at 9:49
  • 2
    Almost perfect. find . -name "*.txt" -delete doesn't seem to be recursive tho. Removed all the files, but didn't remove directories that were listed in -print – Oscar Godson Jul 21 '13 at 9:17
  • 1
    @OscarGodson Assuming the directories were left empty after removing the files, you can remove them with find . -type d -empty -delete – datguy Jan 3 '17 at 20:08
  • 1
    @OscarGodson - find will not delete directories that still contain files (equivalent to rm dir/ without -r). You would have to empty the directory first - or use find to -exec rm with the appropriate options to do this. e.g. find . -depth -name "*.txt" -exec rm -r {} + – shalomb Apr 17 '17 at 19:08
  • Using the echo rm *.txt method seems to have a drawback. It presents the results in a concatenated list (a long string with one file after the previous one), as opposed to a vertical list. Is there any way to get the output to display in a vertical list (one file per line) ? (UPDATE: I see that the find method does what I have requested. So I'll run with that.) – inspirednz Oct 10 '17 at 4:27
22

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.

| improve this answer | |
  • 8
    This is rather difficult with a large number of files. – Cory Klein Feb 7 '11 at 20:13
  • 11
    yes n | rm -i #comment to meet minimum comment length – Joshua May 4 '16 at 18:46
6

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
| improve this answer | |
  • Good point. Though if you know the level of nesting then you can achieve this by ls */*/*.txt – Veneet Reddy Jul 24 '18 at 9:43
  • 1
    You're right. With the -R option. – Veneet Reddy Jul 24 '18 at 10:32
0

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

yes n | rm -i <whatever files you want to delete>

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.