foreach folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting foreach folder
# 1  
Old 08-10-2007
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  
Old 08-11-2007
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  
Old 08-11-2007
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 08:51 AM..
# 4  
Old 08-11-2007
Quote:
Originally Posted by eltinator
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  
Old 08-11-2007
Thanks guys. So basically there is no foreach command in bash?
# 6  
Old 08-11-2007
No, there is not.

The equivalent is

Code:
for instance in * ; do
...
done

# 7  
Old 08-13-2007
Quote:
Originally Posted by lorcan
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete oldest folder based on folder named as date

Hi, I have a script doing backup to synology server, the script create new folder each day with the date as being folder name i.e. 2018-07-30. Just before creating the new folder I want the script to find the oldest folder from the list and delete it including its content. for example... (3 Replies)
Discussion started by: humble_learner
3 Replies

2. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

3. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

4. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

5. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

6. UNIX for Dummies Questions & Answers

Jar/Tar to a diffent folder/same folder w/ filename

Hi, I want to extract myfile.war to a folder which is in the same folder with war file.I did this as normal: jar -xvf myfile.war But it exploded all the content of file to the same level folder instead of that I was expecting to create a folder called myfile. This works with tar: ... (0 Replies)
Discussion started by: reis3k
0 Replies

7. Windows & DOS: Issues & Discussions

How can I upload a zip folder on a unix path from my windows folder?

Hello, I am an amature at UNIX commands and functionality. Please could you all assist me by replying to my below mentioned querry : How can I upload a zip folder on a unix path from my windows folder? Thanks guys Cheers (2 Replies)
Discussion started by: ajit.yadav83
2 Replies

8. Shell Programming and Scripting

script for Finding files in a folder and copying to another folder

Hi all, I have a folder '/samplefolder' in which i have some files like data0.txt, data1.txt and data2.txt. I have to search the folder for existence of the file data0.txt first and if found have to copy it to some other file; next i have to search the folder for existence of file... (5 Replies)
Discussion started by: satish2712
5 Replies

9. UNIX for Advanced & Expert Users

Auto copy for files from folder to folder upon instant writing

Hello all, I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written. I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)
Discussion started by: Bashar
2 Replies

10. Shell Programming and Scripting

Parse the .txt file for folder name and FTP to the corrsponding folder.

Oracle procedure create files on UNIX folder on a regular basis. I need to FTP files onto windows server and place the files, based on their name, in the corresponding folders. File name is as follows: ccyymmddfoldernamefile.txt; Folder Name length could be of any size; however, the prefix and... (3 Replies)
Discussion started by: MeganP
3 Replies
Login or Register to Ask a Question