remove files that are in find result

1.1k Views Asked by At

I want to remove all files that have ".json" extension. so I run

find . -name "*.json"

this will result in some files

x.json
y.json
z.json

then I do rm for each file, but this is a very tedious task when you have many files.

is there any way to mix rm with find result

3

There are 3 best solutions below

0
On BEST ANSWER

If using GNU find, it's dead simple:

find directory/ -name "*.json" -delete
1
On

You can use -exec option with find.

find . -name '*.json' -exec rm -f {} \;
0
On

You can use xargs which will give found files to another command.

The advantage on -exec is that does not create one new process per file:

find . -name "*.json" | xargs rm