LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find statement for multiple folder names (https://www.linuxquestions.org/questions/linux-newbie-8/find-statement-for-multiple-folder-names-783767/)

anon091 01-21-2010 10: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 10:27 AM

for loop will help you. some docs: http://tldp.org/LDP/abs/html/

anon091 01-21-2010 10: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 11:10 AM

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 11:15 AM

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 11:33 AM

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

anon091 01-21-2010 11:34 AM

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 11:35 AM

Code:

bash-3.1# find / -type d -regextype posix-awk -iregex .*/x*
/
/var/cache/packages/slackware64/x
/usr/lib64/python2.6/site-packages/pynche/X
/usr/share/terminfo/x
/usr/share/terminfo/X
/usr/share/texmf/texconfig/x
/usr/share/apps/ksgmltools2/customization/xx
bash-3.1#

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 11:36 AM

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

Tinkster 01-21-2010 11:53 AM

Code:

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

anon091 01-21-2010 11:56 AM

Quote:

Originally Posted by Tinkster (Post 3835305)
Code:

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 12:11 PM

Quote:

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
[m-z]*.

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

$ 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
./os2/graphics/se
./os2/graphics/tw
./os2/graphics/sbcs
./sr21
./test
./te_agent_7.0.0_en_linux
./slackware-12.0
./slackware-12.0/testing
./slackware-12.0/source
./slackware-12.0/slackware
./slackware-12.0/patches/source
./slackware-12.0/patches/slackware-12.0

$ 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
./test


anon091 01-21-2010 12: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 02:34 PM

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

chrism01 01-21-2010 05:00 PM

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


All times are GMT -5. The time now is 07:44 AM.