I have a series of files that always start with at least 5 numerical values like:


Using this answer I learned to use the find and regextype in a command to delete files like this:

find . -regextype posix-extended -iregex './a_[0-9]{6}' -execdir bash -c '[[ ${1##./a_} > 000750 ]] && echo $1' "removing: " {} \;

I altered this command to suite my needs like this:

find . -regextype posix-extended -iregex './[0-9]{5}' -execdir bash -c '[[ ${1##./} > 000001 ]] && echo $1' "removing: " {} \;

I execute the command and it does not delete the files. I cannot figure out how to remove all .jpg that have numeric values in the first 5 digits and then _


Firstly, the command as written doesn't delete anything: as noted in the a nswer you linked

Test this out with the echo command, and once you're sure it lists the correct files, use rm instead.

Second, it's over-complicated for your case, since you don't want to limit the removal to a range or subset of the matches (so you don't need the shell test stuff) - you could simply do

find -regextype egrep -regex '.*/[0-9]{5}_.*\.jpg' -print

(to test) and replace -print by -delete once you're satisfied it's working right.

However even regex seems overcomplicated - a simple glob match is actually less typing:

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

(again, replace -print by -delete when you're sure).

If you don't need to search recursively, a simple shell glob should work:

rm -i [0-9][0-9][0-9][0-9][0-9]_*.jpg
  • 1
    I strongly suggest considering using the command trash from the package trash-cli instead of -delete or rm when using anything like regex or wildcards. Partially just to entrench it as a habit. Too darn easy to make a tragic mistake the other way. Steeldriver, please correct me if I'm wrong in assuming delete is a sort of faster rm built in to find. I can't find it in man find. Maybe your version is newer. My findutils is 4.6.0+git+20160126-2. – Lew Rockwell Fan May 25 '17 at 2:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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