matteobu02
matteobu02

Reputation: 55

Is it possible to print and then remove files found with a single 'find' bash command?

I'm trying to use a single 'find' command to find and print all the files named with a specific pattern. All good with that for now. But now I'm trying to delete those files without writing a second command, so I tried to use the arguments '-delete' and '-exec rm -f {} ;' but none of them actually print the files in the terminal.

So, is it actually possible to print those files and then delete them with a single find command ?

Upvotes: 1

Views: 173

Answers (1)

Socowi
Socowi

Reputation: 27215

Of course this is possible. Use find's -print command:

find ... -print -delete

-print is the implicit default action for find. When you explicitly specify an action like -delete or -exec the default is overwritten, so you have to explicitly specify it again. From man find:

If no expression is given, the expression -print is used

Upvotes: 6

Related Questions