Minh Tran
Minh Tran

Reputation: 510

How do I delete files with a pattern?

I have some files whose name is postfixed by (copy), which I want to remove. For example: f1.jpg, f1(copy).jpg, f2.jpg, f2(copy).jpg. What command can I Use to remove all copy files?

Upvotes: 0

Views: 182

Answers (3)

gniourf_gniourf
gniourf_gniourf

Reputation: 46813

Or find with its wonderful (non-POSIX) -delete switch:

find . -maxdepth 1 -name '*(copy).jpg' -type f -delete

This can be superior to globs in case you have a lot of files, where globs would yield a Argument list too long error. It might be faster too in case of lots of files. The -maxdepth 1 switch (not POSIX) to avoid recursion into subdirectories. The -type f switch to only deal with files. You can add the -print switch if you want to see which files are deleted.

Upvotes: 2

Oleksandr Kovtunenko
Oleksandr Kovtunenko

Reputation: 179

please try this if you rm -rf "*\(copy\)*.jpg"

Upvotes: 0

that other guy
that other guy

Reputation: 123410

You can use the rm command to remove files:

rm *\(copy\)*

The \s are needed because parentheses are special to the shell. Alternatively, rm *copy*.

Upvotes: 4

Related Questions