The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Auto copy for files from folder to folder upon instant writing Bashar UNIX for Advanced & Expert Users 2 08-21-2008 02:44 PM
foreach loop ROOZ Shell Programming and Scripting 3 06-05-2008 03:20 PM
foreach loop abch624 Shell Programming and Scripting 1 03-19-2008 09:34 PM
Parse the .txt file for folder name and FTP to the corrsponding folder. MeganP Shell Programming and Scripting 3 07-03-2007 01:54 PM
foreach command ?! geoquest UNIX for Dummies Questions & Answers 5 05-20-2002 05:11 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-10-2007
eltinator eltinator is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 63
foreach folder

Hi,

I'm having a small issue here and I can't get it to work. I'm programming a script for bash and I need to do something to all the folder in a directory. So I'm in the directory and I want to use the foreach statement but I dont know how to reference all the folders of that directory. To make things simplistic here's my code:

Code:
foreach instance (.)
cd instance
mkdir test
end
basically for each folder in that directory, I want to cd into it and create a folder called test. Any ideas?
  #2 (permalink)  
Old 08-11-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Something like this:
Code:
for file in *; do
   if [ -d $file ]; then
      cd $file; mkdir test
   fi
done
-EDIT
You can skip the testing for directory as well. 'cd' won't work on anything except directories (but there might be links that point to directories and you'd end up creating "test" where you don't want to).
  #3 (permalink)  
Old 08-11-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
A small correction blowtorch.

Code:
for file in *; do
   if [ -d $file ]; then
      mkdir $file/test; 
   fi
done
Since you had issued a cd command it would create the sub-dir only for the first directory. So the result would not be as expected.

Last edited by lorcan; 08-11-2007 at 07:51 AM..
  #4 (permalink)  
Old 08-11-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,190
Quote:
Originally Posted by eltinator View Post
Hi,
Code:
foreach instance (.)
cd instance
mkdir test
end
Your main problem here was not logic, but that you were using the syntax from the wrong shell. This is csh syntax.
  #5 (permalink)  
Old 08-11-2007
eltinator eltinator is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 63
Thanks guys. So basically there is no foreach command in bash?
  #6 (permalink)  
Old 08-11-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,190
No, there is not.

The equivalent is

Code:
for instance in * ; do
...
done
  #7 (permalink)  
Old 08-13-2007
eltinator eltinator is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 63
Quote:
Originally Posted by lorcan View Post
A small correction blowtorch.

Code:
for file in *; do
   if [ -d $file ]; then
      mkdir $file/test; 
   fi
done
Since you had issued a cd command it would create the sub-dir only for the first directory. So the result would not be as expected.
Hi, I had a question about the cd thing, how come it doesn't work? I'm modifying the code to go into certain subfolders in each folder and then execute a command so I wasn't sure how to do that. Basically I'm trying to do something like this now...

current directory:
a b c

Inside of each directory there is a file here (using a as an example):
a/test/folder/script.sh

Sorry for the generic names. Now what I want my script to do is access that script.sh in each folder and run it. I was gonna do the for statement and then cd into that path and then do an execute command but I guess it's not that simple. Any ideas? thanks!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0