Looping for multiple directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping for multiple directories
# 1  
Old 11-07-2011
Looping for multiple directories

Hi experts,
I am totally stuck with this.
I run a looping "for" command for multiple directories, manually, I have done this :

vfor dir in A B; do
cp -p $dir/X.txt X-${dir}.txt
done[/CODE]

where A and B is directory name.
However, I need to run for many directories.
So I have tried this :

Code:
H=directory
for dir in ${directory} ; do
   cp -p $dir/X.txt X-${dir}.txt
done

where H is a file which inside is (A) and (B)
But somehow it did not work out. Smilie
Any suggestion please.
# 2  
Old 11-07-2011
Code:
dirs=("`cat file_with_dirs`")
for dir in ${dirs}; do
  echo $dir
  ....
done

# 3  
Old 11-07-2011
Why I got this error : Syntax error: "(" unexpected
# 4  
Old 11-07-2011
Are you trying to loop along the subdirectories of $directory ? If so, try:
Code:
for subdir in "$directory"/*/ ; do
  echo "$subdir"
done

# 5  
Old 11-07-2011
What shell are You working on?
# 6  
Old 11-07-2011
I am using : #!/bin/sh
Probably I did not explain clearly. Sorry.
In file directory.txt (sorry, previously, I did not put .txt)
Code:
A
B

So far I haven`t succed yet.
# 7  
Old 11-07-2011
Try:

Code:
while read dir; do
  echo "$dir"
done < directory.txt

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Looping through multiple arrays in C.

Not sure if this is possible, but I've tried this about a thousand ways now. I am making something with a lot of arrays. I thought I could put the array names into a separate array and then loop through them to call all of their elements. This is the best I've got so far: #include <stdio.h>... (4 Replies)
Discussion started by: Azrael
4 Replies

2. Shell Programming and Scripting

Looping all subdierctories in multiple pipes

Hello friends, I want to run this code on every document in every sub-directory. tr -d '\n' < MulitpleInput.txt | awk '{gsub(/\. /,".\n");print}' | grep “\ I tried several looping techniques but couldn't get it to run in this example. Any ideas? Thank you (2 Replies)
Discussion started by: danbroz
2 Replies

3. Shell Programming and Scripting

Looping inside directories based on a file which contains file directory list

Hi All, Please help. I have got a file which contains a listing of a file and some directories after it, one by one. I am calling this file xyz.txt here file1 dir1 dir2 dir3 dir4 file2 dir5 dir6 dir7 dir8 file3 dir9 dir10 dir11 dir12 (6 Replies)
Discussion started by: Piyush Jakra
6 Replies

4. UNIX for Dummies Questions & Answers

Deleting multiple directories inside multiple directories

Hi, Very unfamiliar with unix/linux stuff. Our admin is on vacation so, need help very quickly. I have directories (eg 40001, 40002, etc) that each have one subdirectory (01). Each subdir 01 has multiple subdirs (001, 002, 003, etc). They are same in each dir. I need to keep the top and... (7 Replies)
Discussion started by: kkouraus1
7 Replies

5. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

6. Shell Programming and Scripting

Recursive looping through files and directories

hi; i need a script which will go to all directories and subdirectories and print the filenames as follow; here i m printing only files listing in current directory reason i m doing this is coz i want to perform some operations according to filename achieved so cant use find command;... (4 Replies)
Discussion started by: ajaypadvi
4 Replies

7. UNIX for Dummies Questions & Answers

Help in Array looping and creating multiple lines

hi Gurus, I'm a newbie in scripting please check my script if this is correct. I think there's something wrong with it but I;m not sure. I'm trying to create multiple lines using awk from external xml files but i want to add additonal info in the data manually Since i don't knwo how to... (0 Replies)
Discussion started by: sexyTrojan
0 Replies

8. Shell Programming and Scripting

check if multiple directories exist else create missing directories

Hi , I 'm trying to check if multiple directories exist on a server, if not create the missing ones and print " creating missing directory. how to write this in a simple script, I have made my code complex if ; then taskStatus="Schema extract directory exists, checking if SQL,Count and... (7 Replies)
Discussion started by: ramky79
7 Replies

9. Shell Programming and Scripting

Problem with looping the directories

Hi all, I have a directory which has many sub-directories. Now, I want to check the space of each dir and redirect the output in a file if space exceeds the limit. I have already done it, but the way I did is not very good. I just listed the directories and awked the last column to get the... (5 Replies)
Discussion started by: naw_deepak
5 Replies

10. Shell Programming and Scripting

moving directories to new directories on multiple servers

Hi - I am new to unix scripts...I need to move several directories on multiple servers to new directories. (0 Replies)
Discussion started by: mackdaddy07
0 Replies
Login or Register to Ask a Question