Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm trying to match these file names with the find command:

4702011-10-21CR719557-R85262-ALPHA.jar
4702011-10-21CR719557-R85262-BETA.jar

I'm reasonable familiar with Java-Perl regex syntax. Unfortunately, find only supports emacs and posix syntax.

Even after looking at the emacs syntax I cannot get a basic match to work.

find . -regex "^[[:digit:]]+" -- matches nothing
find . -regex "^[[:digit:]]+.*(ALPHA\|BETA)\.jar" -- matches nothing

This is pretty straightforward in Java and Perl, for example:

ls | grep -P "^\d+.*(ALPHA|BETA)\.jar"

works.

share|improve this question
    
I added an grep using Perl regex as an example of what I want. –  KaizenSoze Nov 9 '11 at 16:53
    
I can only give one correct answer, though everyone was correct. Thanks. –  KaizenSoze Nov 9 '11 at 18:24
    
For your amusement the complete command:find . -regextype posix-awk -regex ".*/[0-9\-]+CR[0-9]+-R[0-9]+-(ALPHA|BETA)\.jar" ! ( -name *689305* ) -exec java -jar {} --describe \; | grep "^CR" | sort | uniq –  KaizenSoze Nov 9 '11 at 18:24

3 Answers 3

up vote 2 down vote accepted

a regex for matching those files with either posix-awk or posix-extended regxtype would be:

find . -regextype posix-awk -regex ".*/[[:digit:]]+-[[:digit:]]+-[A-Z0-9]+-[A-Z0-9]+-(ALPHA|BETA)\.jar"

because find -regex does matches, not searches

you can specify regex type with -regextype. see the manpage of your find implementation to check what regex engines are supported.

sample (get the files no matter the dir depth):

root@smgw:/tmp# ls -1
4702011-10-21CR719557-R85262-ALPHA.jar
4702011-10-21CR719557-R85262-BETA.jar
one
root@smgw:/tmp# find . -regextype posix-awk -regex ".*/[[:digit:]]+-[[:digit:]]+-[A-Z0-9]+-[A-Z0-9]+-(ALPHA|BETA)\.jar"
./4702011-10-21CR719557-R85262-ALPHA.jar
./4702011-10-21CR719557-R85262-BETA.jar

move the jars around, make sure they're still returned

search them with an abs path

root@smgw:/tmp# find /tmp  -regextype posix-awk -regex ".*/[[:digit:]]+-[[:digit:]]+-[A-Z0-9]+-[A-Z0-9]+-(ALPHA|BETA)\.jar"
/tmp/4702011-10-21CR719557-R85262-ALPHA.jar
/tmp/4702011-10-21CR719557-R85262-BETA.jar

modified depth:

root@smgw:/tmp# mkdir -p x/y/z
root@smgw:/tmp# cp *^C
root@smgw:/tmp# mv *.jar x/y/z/
root@smgw:/tmp# find /tmp  -regextype posix-awk -regex ".*/[[:digit:]]+-[[:digit:]]+-[A-Z0-9]+-[A-Z0-9]+-(ALPHA|BETA)\.jar"
/tmp/x/y/z/4702011-10-21CR719557-R85262-ALPHA.jar
/tmp/x/y/z/4702011-10-21CR719557-R85262-BETA.jar
share|improve this answer

A regex using emacs style to match your pattern could be:

".*/[0-9]+-[0-9]+-[0-9A-Z]+-[0-9A-Z]+-\(ALPHA\|BETA\)\.jar$"

Below is a simple test I've made:

abarbosa@SPF-157:~/Temp/test$ ls -1
4702011-10-21CR719557-R85262-ALPHA.jar
4702011-10-21CR719557-R85262-BETA.jar
whatever.jar
abarbosa@SPF-157:~/Temp/test$ find . -regex ".*/[0-9]+-[0-9]+-[0-9A-Z]+-[0-9A-Z]+-\(ALPHA\|BETA\)\.jar$"
./4702011-10-21CR719557-R85262-BETA.jar
./4702011-10-21CR719557-R85262-ALPHA.jar
share|improve this answer

According to the man find, this is a match on the whole path, not a search. So, you must use something like this:

$ find . -regex .*/[0-9]+.*\.jar

You can also change the regular expression type to posix-awk, posix-basic, posix-extended, ... with the -regextype option.

share|improve this answer
    
That regex gives me jars that don't start with digits. Imagine there are dozens of jars in the directory, I only want to match the ones that start with 470 something. –  KaizenSoze Nov 9 '11 at 16:50
    
Just insert a leading slash before the 470... Updated my answer. –  quanta Nov 9 '11 at 16:52

Your Answer

 
discard

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.