Linux: how to get files with a name that ends with a number greater than 10

I have files with names like this: 'abcdefg_y_zz.jpg' "Abcdefg" is a sequence of numbers, and "y" and "zz" are letters. I need to get all files that have a sequence of digits ending with a number greater than 10. Files that have "fg" greater than 10.

Does anyone have an idea on how to do this in a bash script?

+4
source share
6 answers

Well, technically, based on all of your information ...

ls | grep '[0-9]{5}[1-9][0-9]_[[:alpha:]]_[[:alpha:]]{2}.jpg'

+2
source

How about this? Just exclude those that have 0 in position f.

 ls -1 | grep -v "?????0?_?_??.jpg" 

Update Since you want> 10, not> = 10, you will also need to exclude 10. So do this:

 ls -1 | grep -v "?????0*_?_??.jpg" | grep -v "??????10_?_??.jpg" 
+1
source

with lots of scripts

 #!/bin/bash for i in seq FIRST INCREMENT LAST do cp abcde$i_y_zz.jpg /your_new_dir //or whatever you want to do with those files done 

so in your line of example with seq will be

 for i in seq 11 1 100000000 
0
source

If the file names are ordered, this awk solution works:

 ls | awk 'BEGIN { FIELDWIDTHS = "5 2" } $2 > 10' 

Explanation

  • FIELDWIDTHS = "5 2" means that $1 will refer to the first 5 characters and $2 to the next 2.
  • $2 > 10 matches when field 2 is greater than 10 and implicitly calls the default code block, i.e. '{ print }'
0
source

Only one process:

 ls ?????+(1[1-9]|[2-9]?)_?_??.jpg 
0
source

All of the solutions presented so far are beautiful, but anyone with some experience in shell programming knows that parsing ls never a good idea and should be avoided . In this case, this does not actually apply, and we can assume that the file names follow a certain pattern, but this is a rule that should be remembered. More details here .

What you want can be much safer with GNU find - if you run the command in the directory where the files are located, it will look something like this:

 find . -regextype posix-egrep -regex '\./[0-9]{5}[1-9][0-9]_[[:alpha:]]_[[:alpha:]]{2}.jpg$' 
0
source

All Articles