vinod
vinod

Reputation: 87

How to print the deleted file names along with path in shell script

I am deleting the files in all the directories and subdirectories using the command below:

find . -type f -name "*.txt" -exec rm -f {} \;

But I want to know which are the files deleted along with their paths. How can I do this?

Upvotes: 1

Views: 584

Answers (1)

JB.
JB.

Reputation: 42094

Simply add a -print argument to your find.

$ find . -type f -name "*.txt" -print -exec rm -f {} \;

As noted by @JonathanRoss below, you can achieve an equivalent result with the -v option to rm.

It's not the scope of your question, but more generally it gets more interesting if you want to delete directories recursively. Then:

  • a simple -exec rm -r argument keeps it silent
  • a -print -exec rm -r argument reports the toplevel directories you're operating on
  • a -exec rm -rv argument reports all you're removing

Upvotes: 3

Related Questions