Find, regular expression, anyway to simplify this find command?

Hello everyone,
first post here, trying to learn scripting on my own and this forum as been really helpful so far. I made few little scripts working great but I m facing some problems with RE.

I have a bunch of files in many subdirectories called *001.ext *002.ext OR simple *.ext or *01.ext *02.ext And I want to use the find command to only show files *.ext or *01.ext or *001.ext without showing *101.ext *98.ext etc

So the output I want should only show files named like this :

./maindir/blabladir/blabla.ext
./maindir/BlaBLoBdir/BlaBLoBli.ext
./maindir/blablabliblou/blablabliblou.01.ext
./maindir/blabladir/blabla.01.ext
./maindir/blablablouboum/blablablouboum.001.ext

etc

I ve tried many ways for this but I only managed to get it working through the following painful command :

find . -iname '*.ext' ! -regex '.*0[2-9].ext' ! -regex '.*[1-9][0-9].ext' ! -regex '.*0[0-9][2-9].ext' ! -regex '.*1[0-9][0-9].ext'

Is there anyway to simplify this? I m not used to regular expressions but I m sure there must be a cleaner, simpler command for this. I tried to find answers on different websites about how regular expressions work but in this case it's giving me a headache :wall:

For now I m using this command to process my result through "find -exec" it works great but kinda restrictive. Idealy when I ll manage to, I ll export the result from find in a file (find blabla > test.txt) and then make a loop processing each line from test.txt and assign different variables to line 1, then execute a specific command and then process line 2 until there is no more lines to process.

But right now I just want to simplify this find command if anyone got better idea?

Thx

I think you want this

find . -iname '*.ext' | egrep '[^0-9](001|01|[^0-9])\.ext$'

But you should understand
1) that *.ext matches every file with .ext extension and *01.ext matches *201.ext, etc.
2) that file globing and regular expressions are different things.

1 Like

Awww yes, it seems to be working out perfectly thanks man

And yes I m aware of 1) I just didn't figure out a better way to do it. You make it look so simple now.

find . -regex '.*[^0-9](0+1)?.ext'

Problem with Yazu's solution is that i can't use -exec to my find command if I | egrep.
So I made it go through a | while read line loop but i noticed some files were missing aswell.

And binlib it's not working no :slight_smile:

Gotta use globing if i want to be able to use -exec

I want to show any file called *.ext OR *part001.ext OR *part01.ext
and not show *part[0-9][2-9].txt AND NOT *part[0-9][0-9[2-9].ext

Kinda tricky :stuck_out_tongue:
For now i got a shitty work arround => basicly processing some files manually

I was experimenting different regexes and accidentally left out the escapes of the parentheses (or you can add -regextype with the type of your favorite regexes.)

find . -regex '.*[^0-9]\(0+1\)?.sql'

This will match b.ext, b01.ext, b001.ext and b0001.ext, etc. If you don't want to match beyond 001, then change to

find . -regex '.*[^0-9]\(00?1\)?.sql'
1 Like

Mmmh it actualy worked for you?

find . -regex '.*[^0-9]\(0+1\)?.ext'

and

find . -regex '.*[^0-9]\(00?1\)?.ext'

gave me nothing
How do you check the default regextype? and what regextype is being used in your code?
I made many .ext files named randomly to test it out and no success but that's exactly what i need. gonna try arround to test arround if i can get it working.

Edit : Couldn't get it working. I m running this on my NAS which is limited to sh shell (and ash which I didn't really check out yet...) i tried to change regextype but it's not recognized.
Gonna try to install bash and other stuff.
Grumpf...

$ uname -a
Linux hostname 2.6.32-33-generic #70-Ubuntu SMP Thu Jul 7 21:09:46 UTC 2011 i686 GNU/Linux

$ find --version
find (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS() CBO(level=0)
$ ls  *ext
a00101.ext  a00.ext  a02.ext  a10.ext  a12.ext  a20.ext  a22.ext  abc.ext
a001.ext    a01.ext  a03.ext  a11.ext  a13.ext  a21.ext  a23.ext
$ find . -regex '.*[^0-9]\(0+1\)?.ext'
./abc.ext
./a01.ext
./a001.ext