Running a command for multiple folders at once | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Running a command for multiple folders at once

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-21-2013
phil_heath phil_heath is offline
Registered User
 
Join Date: May 2009
Last Activity: 11 September 2013, 10:42 PM EDT
Posts: 95
Thanks: 8
Thanked 0 Times in 0 Posts
Running a command for multiple folders at once

Hi I have folders 1 to 24 (24 folders in total) and inside those folders I have the same file names. I have a command that I want to run but rather than do it individually I was wondering if there is a command to run them all at once.

Thanks

Phil
Sponsored Links
    #2  
Old 06-21-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 30 October 2014, 7:23 AM EDT
Location: NM
Posts: 10,252
Thanks: 284
Thanked 803 Times in 747 Posts

Code:
#!/bin/bash
cd /path
for dir in {1..24}
do
   cd $dir
   # command goes here
   cd ..
done

Without bash you have to use something like this for the "for" construct

Code:
for dir in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Sponsored Links
    #3  
Old 06-21-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 30 October 2014, 3:11 AM EDT
Location: San Jose, CA, USA
Posts: 4,927
Thanks: 184
Thanked 1,651 Times in 1,402 Posts
Quote:
Originally Posted by jim mcnamara View Post
Code:
#!/bin/bash
cd /path
for dir in {1..24}
do
   cd $dir
   # command goes here
   cd ..
done

Without bash you have to use something like this for the "for" construct

Code:
for dir in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

The {1..24} also works with recent versions of ksh. And with any shell that recognizes basic Bourne shell syntax, your could also use:

Code:
for dir in [1-9] 1[0-9] 2[0-4]

and with bash and recent versions of ksh you could also use:

Code:
for ((dir=1; dir<=24; dir++))
do      ...
done

    #4  
Old 06-21-2013
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 28 October 2014, 1:56 PM EDT
Location: France
Posts: 318
Thanks: 19
Thanked 81 Times in 76 Posts
The external command seq 24 might come in handy.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Logging in to multiple Linux servers and running the command. jpkumar10 Shell Programming and Scripting 2 02-26-2013 03:55 AM
running multiple command in a single line anuragpgtgerman Shell Programming and Scripting 6 03-29-2012 07:19 AM
running multiple command in same line gubbu Shell Programming and Scripting 2 08-03-2009 05:31 PM
Running a command on multiple selected files in nautilus script Nickbowlingdude Shell Programming and Scripting 2 02-08-2009 10:21 PM
running multiple rsh command in a script lweegp Shell Programming and Scripting 0 10-31-2006 01:37 AM



All times are GMT -4. The time now is 10:26 AM.