Kys
Kys

Reputation: 2625

Linux command for removing all ~ files

What command can I use in Linux to check if there is a file in a given directory (or its subdirectories) that contains a ~at the end of the file's name?

For example, if I'm at a directory called t which contains many subdirectories, etc, I would like to remove all files that end with a ~.

Upvotes: 1

Views: 1186

Answers (4)

A B
A B

Reputation: 8866

Watch out for filenames with spaces in them!

find ./ -name "*~" -type f -print0 | xargs -0 rm

Upvotes: 9

ghostdog74
ghostdog74

Reputation: 342443

with GNU find

find /path -type f -name "*~" -exec rm {} +

or

find /path -type f -name "*~" -delete

Upvotes: 8

find ./ -name '*~' -print0 | xargs -0 rm -f

Here find will search the directory ./ and all sub directories, filtering for filenames that match the glob '*~' and printing them (with proper quoting courtesy of alberge). The results are passed to xargs to be appended to rm -f and the resulting string run in a shell. You can use multiple paths, and there are many other filters available (just read man find).

Upvotes: 2

Alphaneo
Alphaneo

Reputation: 12539

you can use a find, grep, rm combination, something like

find | grep "~" | xargs rm -f

Probably others have better ideas :)

Upvotes: 1

Related Questions