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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
01-21-2010, 10:13 AM
|
#1
|
Senior Member
Registered: Jun 2009
Posts: 1,795
Rep:
|
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?
|
|
|
01-21-2010, 10:27 AM
|
#2
|
Member
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778
Rep:
|
for loop will help you. some docs: http://tldp.org/LDP/abs/html/
|
|
|
01-21-2010, 10:30 AM
|
#3
|
Senior Member
Registered: Jun 2009
Posts: 1,795
Original Poster
Rep:
|
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?
|
|
|
01-21-2010, 11:10 AM
|
#4
|
Member
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778
Rep:
|
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.
|
|
|
01-21-2010, 11:15 AM
|
#5
|
Senior Member
Registered: Jun 2009
Posts: 1,795
Original Poster
Rep:
|
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.
|
|
|
01-21-2010, 11:33 AM
|
#6
|
Member
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778
Rep:
|
it's easier than you think but scripting specially bash scripting is annoying thing to learn.
|
|
|
01-21-2010, 11:34 AM
|
#7
|
Senior Member
Registered: Jun 2009
Posts: 1,795
Original Poster
Rep:
|
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.
|
|
|
01-21-2010, 11:35 AM
|
#8
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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.
|
|
|
01-21-2010, 11:36 AM
|
#9
|
Senior Member
Registered: Jun 2009
Posts: 1,795
Original Poster
Rep:
|
Thanks, i'll use it as an example once i read up on for loops
|
|
|
01-21-2010, 11:53 AM
|
#10
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Code:
find -iname [m-z]\* -type d
|
|
|
01-21-2010, 11:56 AM
|
#11
|
Senior Member
Registered: Jun 2009
Posts: 1,795
Original Poster
Rep:
|
Quote:
Originally Posted by Tinkster
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] ?
|
|
|
01-21-2010, 12:11 PM
|
#12
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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
|
|
|
01-21-2010, 12:43 PM
|
#13
|
Senior Member
Registered: Jun 2009
Posts: 1,795
Original Poster
Rep:
|
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!
|
|
|
01-21-2010, 02:34 PM
|
#14
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Most welcome. Just trying to live up to the statement in my sig :}
|
|
|
01-21-2010, 05:00 PM
|
#15
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,398
|
@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 09:10 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|