I have a directory that has subdirectories, and I want to delete images named like thumbnail-name_WIDTHxLENGTH.{jpg,png}, where WIDTH and LENGTH are both decimal integers, like box-image-150x150.jpg and circle-image-320x320.png. So basically it's any jpg/png that contains an x surrounded by digits.

  • Can you give some examples? Are width and length always (and only) digits? What can name contain? – chaos Oct 27 '15 at 17:48
  • For example box-image-150x150.jpg and circle-image-320x320.png so basically any jpg/png that contains numbers with an x in the middle example 567x876 – fer Oct 27 '15 at 18:00
# Old school way for directory-walking: find (emacs regex; use `(jpg|png)` on BSD find)
# use -iregex for case-insensitive
find some/dir -regex '.*[0-9]+x[0-9]+.\(jpg\|png\)' -delete
# ksh93, zsh & bash: globstar + brace expansion + extglob
# for ksh, use `set -o globstar` first; for bash, use `shopt -s globstar extglob nullglob` first.
# TODO: Too lazy to write zsh cshnullglob and case-insensitive globs
rm -- **/*[0-9]x+([0-9]).{jpg,png}

The term “regular expression” (generally) doesn’t apply to selecting files from the command line.1  The technical term is pathname expansion, more commonly known as globbing, pattern matching, or wildcard expansion.  Based on what you have said, a first attempt at an answer would be

rm -- *[0-9]x[0-9]*

which removes every file in the current directory whose name consists of

  • anything (*), followed by
  • a digit ([0-9]), followed by
  • the letter x, followed by
  • another a digit ([0-9]), followed by
  • anything (*).

The -- protects you against files whose names begin with -.  I strongly urge you to do

ls -ld -- *[0-9]x[0-9]*

first, to see what files are matched and will be removed.  If you discover that you’re matching files that you didn’t mean to, use *[0-9][0-9][0-9]x[0-9][0-9][0-9]* to get only files that contain (at least) a three-digit number, x, and another three-digit number.  And/or append .jpg (at the end; i.e., use *[0-9]x[0-9]*.jpg) to restrict the command to files with that extension, and then repeat the command with .png.  Alternatively, use

rm -i -- *[0-9]x[0-9]*

to have rm ask you for confirmation for each file.

You mentioned “a directory that has directories. All of them have jpgs.”  If you need to search a directory tree, one approach is to type

shopt -u globstar

and then prepend **/ to the wildcard; i.e., add it at the beginning; e.g.,

rm -i -- **/*[0-9]x[0-9]*

Yet another way to scan a subdirectory tree is

find . -name "*[0-9]x[0-9]*" -type f -exec rm -i -- {} +

and you can “preview” this command (see what files it will match) by typing

find . -name "*[0-9]x[0-9]*" -type f -print

Again, if this matches too many files, you can add digits and/or add the extension(s).


  • You must quote the wildcard in the find command.  You must not quote the wildcard in the other commands.
  • The find command will automatically find files whose names begin with ..  The others will not, unless you type shopt -s dotglob first.
  • The find command will act on plain files only, because of the -type f.  The others will act on directories (and other special files) if their name contains numberxnumber.  This shouldn’t be a problem for directories, since rm will not remove directories.  But, as I said, do the ls first and carefully check the output, or use rm -i and think before you type y.

1 find does have the ability to match regular expressions, with the -regex test, but you don’t need it for this problem.


Use find with xargs:

find / -name "*[0-9]*x*[0-9]*.jpg" | xargs /bin/rm -f


find / -name "*[0-9]x[0-9]*.jpg" | xargs /bin/rm -f

or like 'authur2e5' suggested in the comments below:

find some/dir -name "*[0-9]x[0-9]*.jpg" -delete
  • 1
    find some/dir -name "*[0-9]x[0-9]*.jpg" -delete – Arthur2e5 Oct 27 '15 at 18:58
  • @Arthur2e5 - I believe the way you wrote it will only look for a single number and not something like 150 (edit: I was wrong...the asterisk before it will catch the other numbers), but essentially that is another correct.way. I prefer xargs to -delete myself but it's all about personal preference. – Jeight Oct 27 '15 at 19:00
  • You are confused. If you want regex / Kleene astericks, use -regex '.*[0-9]+x[0-9]+.jpg'. – Arthur2e5 Oct 27 '15 at 19:04
  • And using -delete is a bit faster and much safer (not important here). – Arthur2e5 Oct 27 '15 at 19:05
  • 1
    @G-Man I think xargs -0 is an extension too. – Arthur2e5 Oct 27 '15 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.