5

I have a set of images like these

12345-image-1-medium.jpg 12345-image-2-medium.png 12345-image-3-large.jpg

what pattern should I write to select these images and delete them

I also have these images that don't want to select

12345-image-profile-small.jpg 12345-image-profile-medium.jpg 12345-image-profile-large.png

I have tried this regex but not worked

1234-image-[0-9]+-small.*

I think bash not support regex as in Javascript, Go, Python or Java

9
  • Are there any other images in this directory that should not be deleted and if so what are their names? Commented May 26, 2020 at 18:55
  • yeah there are other images eg: 12345-image-profile-small.jpg that I don't want to delete Commented May 26, 2020 at 18:56
  • Did you try a pattern that didn't work? What was it? Commented May 26, 2020 at 18:56
  • yeah but the tricky part is selecting the number part. Commented May 26, 2020 at 18:59
  • 1
    Test with ls -al [[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]-image-[[:digit:]]* if that matches what you want to delete, then rm [[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]-image-[[:digit:]]* Commented May 26, 2020 at 19:04

3 Answers 3

6

Ok I solve it with this pattern

12345-image-*[0-9]-*

eg:

rm -rf 12345-image-*[0-9]-*

it matches all the file names start with 12345-image- then a number then - symbol and any thing after that

as I found it's globbing in bash not regex and I found this app really use full

Sign up to request clarification or add additional context in comments.

2 Comments

That will match a whole lot more than you think, but only if the filenames start with 12345. From your comment of "I have just tested this regex 1234-image-..." you indicated that the number of digits was not fixed at five -- it this a requirement?.
as I tested it worked. I just want to select files start with specific id then have -image- then a number and an extension at the end
1
for pic in 12345*.{jpg,png};do rm $pic;done

for more information on wildcards take a look here

2 Comments

I have just edited the question plz have a check on update
1

So long as you do NOT have filenames with embedded '\n' character, then the following find and grep will do:

find . -type f | grep '^.*/[[:digit:]]\{1,5\}-image-[[:digit:]]\{1,5\}'

It will find all files below the current directory and match (1 to 5 digits) followed by "-image-" followed by another (1 to 5 digits). In your case with the following files:

$ ls -1
123-image-99999-small.jpg
12345-image-1-medium.jpg
12345-image-2-medium.png
12345-image-3-large.jpg
12345-image-profile-large.png
12345-image-profile-medium.jpg
12345-image-profile-small.jpg

The files you request are matched in addition to 123-image-99999-small.jpg, e.g.

$ find . -type f | grep '^.*/[[:digit:]]\{1,5\}-image-[[:digit:]]\{1,5\}'
./123-image-99999-small.jpg
./12345-image-3-large.jpg
./12345-image-2-medium.png
./12345-image-1-medium.jpg

You can use the above in a command substitution to remove the files, e.g.

$ rm $(find . -type f | grep '^.*/[[:digit:]]\{1,5\}-image-[[:digit:]]\{1,5\}')

The remaining files are:

$ l1
12345-image-profile-large.png
12345-image-profile-medium.jpg
12345-image-profile-small.jpg

If Your find Supports -regextype

If your find supports the regextype allowing you to specify which set of regular expression syntax to use, you can use -regextype grep for grep syntax and use something similar to the above to remove the files with the -execdir option, e.g.

$ find . -type f -regextype grep -regex '^.*/[[:digit:]]\+-image-[[:digit:]]\+.*$' -execdir rm '{}' +

I do not know whether this is supported by BSD or Solaris, etc.., so check before turning it loose in a script. Also note, [[:digit:]]\+ tests for (1 or more) digits and is not limited to 5-digits as shown in your question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.