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?
- 1I would also like to see which files will generate errors for lack of permissions.Simon Woodside– Simon Woodside2017-02-10 22:19:38 +00:00Commented Feb 10, 2017 at 22:19
4 Answers
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).
- find is a good choice IMO. And if you want a graphical presentation use
tree.noisebleed– noisebleed2012-07-25 09:49:03 +00:00Commented Jul 25, 2012 at 9:49 - 2Almost perfect.
find . -name "*.txt" -deletedoesn't seem to be recursive tho. Removed all the files, but didn't remove directories that were listed in-printOscar Godson– Oscar Godson2013-07-21 09:17:48 +00:00Commented Jul 21, 2013 at 9:17 - 1@OscarGodson Assuming the directories were left empty after removing the files, you can remove them with
find . -type d -empty -deletedatguy– datguy2017-01-03 20:08:53 +00:00Commented Jan 3, 2017 at 20:08 - 1@OscarGodson -
findwill not delete directories that still contain files (equivalent torm dir/without-r). You would have to empty the directory first - or usefindto-exec rmwith the appropriate options to do this. e.g.find . -depth -name "*.txt" -exec rm -r {} +shalomb– shalomb2017-04-17 19:08:51 +00:00Commented Apr 17, 2017 at 19:08 - 1Using the
echo rm *.txtmethod 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 thefindmethod does what I have requested. So I'll run with that.)inspirednz– inspirednz2017-10-10 04:27:55 +00:00Commented Oct 10, 2017 at 4:27
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.
- 14This is rather difficult with a large number of files.Cory Klein– Cory Klein2011-02-07 20:13:06 +00:00Commented Feb 7, 2011 at 20:13
- 22yes n | rm -i #comment to meet minimum comment lengthJoshua– Joshua2016-05-04 18:46:32 +00:00Commented May 4, 2016 at 18:46
- this does not work with -rf flags, e,g. ` rm -irf *.txt ` does remove without promptingtertek– tertek2025-09-08 13:52:37 +00:00Commented Sep 8 at 13:52
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 - Good point. Though if you know the level of nesting then you can achieve this by
ls */*/*.txtVeneet Reddy– Veneet Reddy2018-07-24 09:43:30 +00:00Commented Jul 24, 2018 at 9:43 - 1You're right. With the
-Roption.Veneet Reddy– Veneet Reddy2018-07-24 10:32:46 +00:00Commented Jul 24, 2018 at 10:32 - This is definitely the best answer. It is almost identical to the needed
rmcommand and intuitive.Akaisteph7– Akaisteph72024-09-06 21:35:35 +00:00Commented Sep 6, 2024 at 21:35
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.
- 2Doesn't work for recursive rm because that asks for confirmation before descending into directoriesdeed02392– deed023922022-01-26 13:56:14 +00:00Commented Jan 26, 2022 at 13:56