-   Linux - Newbie (
-   -   Find statement for multiple folder names (

anon091 01-21-2010 11:13 AM

Find statement for multiple folder names
Through other posts, I think I'll finally be able to knock out a problem I'm having if someone can help me tweak a find statement, as I only know how to do very simple ones.

right now i'm doing a

find . -type d -iname "z*"

to find all folders who's name starts with z or Z.

Is there a way I could with one command find all folders who's name starts with the letters M through Z, without having to do the same command over and over and just changing the letter each time?

ozanbaba 01-21-2010 11:27 AM

for loop will help you. some docs:

anon091 01-21-2010 11:30 AM

I kind get what the for loop would do, like automate the switching of the letters. But can I do it right in that find command somehow too?

ozanbaba 01-21-2010 12:10 PM

i don't remember if you can do it in the find command, and don't forget for is command, too. actually for ia a command, find is an awesome program.

anon091 01-21-2010 12:15 PM

I tried reading the for loop stuff, and I definitely think that's over my head. Perhaps I'd be better just manually doing it for each letter.

ozanbaba 01-21-2010 12:33 PM

it's easier than you think but scripting specially bash scripting is annoying thing to learn.

anon091 01-21-2010 12:34 PM

I'll have to read up on it when I get some time. good to know that such a thing exists, luckily there aren't that many letter in the alphabet so I'll do this one by hand for now.

GrapefruiTgirl 01-21-2010 12:35 PM


bash-3.1# find / -type d -regextype posix-awk -iregex .*/x*

You might like to fiddle with the above a bit, altering the regex. I don't know why it returns the "/" (root folder) but otherwise it *seems* to work. And, change the 'x' to a 'z' to suit your requirements.

anon091 01-21-2010 12:36 PM

Thanks, i'll use it as an example once i read up on for loops

Tinkster 01-21-2010 12:53 PM


find -iname [m-z]\* -type d

anon091 01-21-2010 12:56 PM


Originally Posted by Tinkster (Post 3835305)

find -iname [m-z]\* -type d

Wow, is it really that straightforward? I'd see [] before but never understood what it meant, but i think i do now! its for ranges it appears. Also, what does the \ do after the [m-z] ?

Tinkster 01-21-2010 01:11 PM


Also, what does the \ do after the [m-z] ?
It escapes the *
Otherwise the shell would interpret it, and you may end up with
fewer hits than you were after, or an error message, depending
on how many directories in the current working directory match

Here's an example from a tmp-directory ...

$ ls -p
Makefile  cleaned_up          os2/            sr21/
a.out    clusternodes.awk    pam.d/          te_agent_7.0.0_en_linux/
back.tgz  course_extended.lyx  slackware-12.0/  test/

$ find -iname [st]\* -type d

$ find -iname [st]* -type d
find: paths must precede expression: sr21
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

$ rm -rf ./sr21 ./te_agent_7.0.0_en_linux ./slackware-12.0

$ find -iname [st]* -type d

anon091 01-21-2010 01:43 PM

Thanks for providing the examples, it makes sense now, and even shows why I need to do [m-z] rather than [mz]. I'd rather learn with guidance than just get an answer than no feedback as to why that solves my problem, you would never learn that way. Appreciate the time Tinkster!

Tinkster 01-21-2010 03:34 PM

Most welcome. Just trying to live up to the statement in my sig :}

chrism01 01-21-2010 06:00 PM

@GrapefruiTgirl: that's probably because '*' in most regex engines means zero or more instances of the preceding character ...

Tinkster 01-21-2010 08:29 PM


Originally Posted by chrism01 (Post 3835704)
@GrapefruiTgirl: that's probably because '*' in most regex engines means zero or more instances of the preceding character ...

I'll hazard a guess and say what she meant was

find / -type d -regextype posix-awk -iregex '.+/x[^/]*'

All times are GMT -5. The time now is 05:31 AM.