Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

can someone steer me in the right direction. I have 2GB worth of images, which include 3 types. Eg:

  • thumb-12345.jpg
  • popup-12345.jpg
  • 12345.jpg

Is there a quick script/command I could run to remove all the files which do not have 'thumb' or 'popup' in the name. Basically - I just need to remove the '12345.jpg'

Quite new to this, any help would be appreciated! Cheers

share|improve this question
up vote 5 down vote accepted

In all the snippets below, run the command to check if the files it displays are really the ones you want to delete. Then perform the deletion by replacing echo with rm or -print with -delete

If all your files are in the same directory, you can remove the ones whose name starts with a digit.

echo [0-9]*.jpg

If your files live in a directory hierarchy, use find.

find . -name '[0-9]*.jpg' -print

If you specifically want to match popup and thumb and no other prefix:

find . -name '*.jpg' \( -name 'popup-*' -o -name 'thumb-*' -o -print \)

In bash ≥4.0 or zsh, there's a simpler way to traverse subdirectories, as long as you don't run into a command line length limitation. In bash, first run shopt -s globstar. Then:

echo **/[0-9]*.jpg
share|improve this answer
errr My snippets don't delete any data. I hate to bug you but I really really seldom give out something that deletes data without asking. (At least not unless someone really deserves that) – Server Horror Sep 18 '11 at 14:10
@ServerHorror Er, yes? We're on the same page then, my snippets don't delete data either. – Gilles Sep 18 '11 at 14:20
You hit me on a sensitive topic with your first paragraph -- nothing to worry about :) – Server Horror Sep 18 '11 at 14:40

Haha more than one way and less processes :)

Start by finding what you want to keep:

find -E . -type f -regex '.*((popup)|(thumb))-[[:digit:]]*\.jpg' 

Negate the expression:

find -E . -type f \! -regex '.*((popup)|(thumb))-[[:digit:]]*\.jpg' 

Works for your sample data....

If you're satisfied add a -delete.

Read up on it in re_format(3) and find(1) to know which options to apply so that your find understands extended regular expressions

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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