LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-14-2011, 10:19 AM   #1
xibeimunan
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Rep: Reputation: 0
How to get into multiple directories in Linux


Hey everyone,
I have a directory list, say, test{0..9}. Now I'd like to get into each one and run some commands and then go to the next directory. Could anyone help me write a simple script to do that? Thanks in advance.

Nan
 
Old 07-14-2011, 11:05 AM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Hello xibeimunan,
Code:
for i in {1..9}; do cd test$i; yourcommandhere; cd ..; done
Markus
 
1 members found this post helpful.
Old 07-14-2011, 11:07 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
for dir in test{0..9} ; do
   (
    cd "$dir"
    <run commands>
   )
done
Putting the commands inside (...) makes them run inside a subshell. this means that the the cd only affects that subshell environment, and you're back to where you started when it exits, ready for the next iteration.

However this won't do if you need to set any variables that are to be read outside of the loop. So you could also do this:

Code:
for dir in test{0..9} ; do
    cd "$dir"
    <run commands>
    cd -
done
Where the final cd command returns you to the previous directory.
 
1 members found this post helpful.
Old 07-14-2011, 11:55 AM   #4
xibeimunan
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks a lot for your comments!
 
  


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
Change text in multiple files in multiple directories vivo2341 Linux - General 5 11-27-2006 09:16 PM
Searching multiple directories and sub directories for a file jeep99899 Linux - Newbie 2 10-13-2005 01:23 PM
Help I need help tarring multiple files in multiple directories VisionZ Linux - Newbie 28 03-25-2004 06:25 PM


All times are GMT -5. The time now is 02:07 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration