tags:

views:

77

answers:

4

Hi,

in bash, I would like to use the command "find" to find files which contain the numbers from 40 to 70 in a certain position like c43_data.txt. How is it possible to implement this filter in find ?

I tried file . -name "c**_data.txt" | grep 4, but this is not very nice

Thanks

+3  A: 

Perhaps something like:

find . -regextype posix-egrep -regex "./c(([4-6][0-9])|70)_data.txt"

This matches 40 - 69, and 70.

You may also use the iregex option for case-insensitive matching.

Nick Presta
No, this won't match 41-49, etc.
danben
`find . -regex "c[4-7][0-9]_data.txt"` - Yours would only match multiples of ten; 40, 50, 60, 70.
tj111
I did not know about the regex flag, thanks
Alberto Zaccagni
Thanks. I just woke up and I'm a little groggy, obviously. :-)
Nick Presta
regex matches the whole path. if you want to use the alternation , your regextype should change. `find . -regextype posix-egrep -regex "./c([4-6][0-9]|70)_data.txt"`
Thanks, 1ch1g0.
Nick Presta
A: 

ls -R | grep -e 'c[4-7][0-9]_data.txt'

find can be used in place of ls, obviously.

danben
ok, thanks a lot
asdf
Will this not match files that have 71-79 in them?
Nick Presta
Yes, that will match 71-79, and also doesn't give the pathnames of the files.
Alok
by the way, how can one two criterias at the same time? let's say ls -R | grep -e 'c[4-7][0-9]_data.txt' AND ls -R | grep -e 'c[1-2][3-4]_data.txt' ???
asdf
asdf: grep -e 'c\([4-7][0-9]\|[1-2][3-4]\)_data.txt'Generally: man egrep
SF.
A: 

Try something like:

find . -regextype posix-egrep -regex '.\*c([3-6][0-9]|70).\*'

with the appropriate refinements to limit this to the files you want

Dancrumb
+1  A: 
$ ls
c40_data.txt  c42_data.txt  c44_data.txt  c70_data.txt  c72_data.txt  c74_data.txt
c41_data.txt  c43_data.txt  c45_data.txt  c71_data.txt  c73_data.txt  c75_data.txt

$ find . -type f \( -name "c[4-6][0-9]_*txt" -o -name "c70_*txt" -o -name "c[1-2][3-4]_*.txt" \) -print
./c43_data.txt
./c41_data.txt
./c45_data.txt
./c70_data.txt
./c40_data.txt
./c44_data.txt
./c42_data.txt
ghostdog74