LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 01-21-2010, 10:13 AM   #1
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,398

Rep: Reputation: 36
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?
 
Old 01-21-2010, 10:27 AM   #2
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 671

Rep: Reputation: 94
for loop will help you. some docs: http://tldp.org/LDP/abs/html/
 
Old 01-21-2010, 10:30 AM   #3
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,398

Original Poster
Rep: Reputation: 36
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?
 
Old 01-21-2010, 11:10 AM   #4
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 671

Rep: Reputation: 94
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.
 
Old 01-21-2010, 11:15 AM   #5
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,398

Original Poster
Rep: Reputation: 36
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.
 
Old 01-21-2010, 11:33 AM   #6
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 671

Rep: Reputation: 94
it's easier than you think but scripting specially bash scripting is annoying thing to learn.
 
Old 01-21-2010, 11:34 AM   #7
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,398

Original Poster
Rep: Reputation: 36
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.
 
Old 01-21-2010, 11:35 AM   #8
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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.
 
Old 01-21-2010, 11:36 AM   #9
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,398

Original Poster
Rep: Reputation: 36
Thanks, i'll use it as an example once i read up on for loops
 
Old 01-21-2010, 11:53 AM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Code:
find -iname [m-z]\* -type d
 
Old 01-21-2010, 11:56 AM   #11
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,398

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by Tinkster View Post
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] ?
 
Old 01-21-2010, 12:11 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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

Last edited by Tinkster; 01-21-2010 at 12:13 PM. Reason: typo
 
Old 01-21-2010, 12:43 PM   #13
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,398

Original Poster
Rep: Reputation: 36
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!
 
Old 01-21-2010, 02:34 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Most welcome. Just trying to live up to the statement in my sig :}
 
Old 01-21-2010, 05:00 PM   #15
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
find statement elainelaw Linux - Newbie 14 11-05-2009 03:56 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
How to do multiple comaprison in a IF statement pdklinux79 Linux - Newbie 4 06-11-2008 10:34 PM
find statement tostay2003 Programming 4 12-11-2007 11:44 AM
long folder names opsraja Fedora 2 09-23-2004 01:56 PM


All times are GMT -5. The time now is 11:06 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration