delete files matching pattern
94,495
A string contains “a number followed by an x
followed by a number” if and only if it contains a digit followed by an x
followed by a digit, i.e. if it contains a substring matching the pattern [0-9]x[0-9]
. So you're looking to remove the files whose name matches the pattern *[0-9]x[0-9]*[0-9]x[0-9]*.jpg
.
find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' -delete
If your find
doesn't have -delete
, call rm
to delete the files.
find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' -exec rm {} +
Related videos on Youtube
Author by
mikkelbreum
Updated on September 18, 2022Comments
-
mikkelbreum over 1 year
I need to recursively remove all files in all subdirs where the filename contains a number followed by an 'x' followed by a number, at least two times.
Example:
I'd want to remove these files:
'aaa-12x123-123x12.jpg' 'aaa-12x12-123x12-12x123.jpg'
But I do NOT want to remove these files:
'aaa.jpg' 'aaa-12x12.jpg' 'aaaxaaa-123x123.jpg' 'aaaxaaa-aaaxaaa.jpg'
How can I do that (from the bash shell)
-
mikkelbreum almost 11 yearsThank you! 12.000 files gone i 2 sec. That saved me some manual labour!
-
Tamlyn almost 9 yearsNeither
-delete
nor-exec rm
worked for me in Bash on Windows. But this did:find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' | xargs rm
-
Gilles 'SO- stop being evil' almost 9 years@Tamlyn Use
-print0
andxargs -0
, otherwise the command will fail with file names containing spaces or single quotes. But-delete
and-exec rm
do work on Windows. If something doesn't work, it's not due to their use. -
user3426706 over 8 yearsWill this work with Windows as well?
-
Gilles 'SO- stop being evil' over 8 yearsIt'll work if you have a port of Unix utilities such as Cygwin or GNUWin32. Obviously it won't work out of the box on Windows. Take care that Windows has an unrelated program called
find
, so make sure the Unix utilities are first inPATH
.