Accessing multiple directories in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Accessing multiple directories in loop
# 1  
Old 09-08-2014
Accessing multiple directories in loop

Hi Guys,

I need to access multiple directories whcih is following similar structure and need to copy those files in desitination path.

for eg :

Code:
if [[ -e ${DIR}/Mon/loaded ]]
then
cd ${DIR}/Mon/loaded
echo "copying files to $GRS_DIR"
cp * ${DIR}/Mon/
echo "Files of Monday are Copied"
fi

if [[ -e ${DIR}/Tue/loaded ]]
then
cd ${DIR}/Tue/loaded
echo "copying files to $GRS_DIR"
cp * ${DIR}/Mon/
echo "Files of Monday are Copied"
fi

like wise i am having common directory like {DIR}/Mon . Tue. Wed. Thu . Fri . Sat will it be possible to achive in single loop
# 2  
Old 09-08-2014
Hello Rohit,

Following may help you, but try the same in non live environment before using this into live environment as this is not tested.

Code:
cat test.ksh
check_directories() {
for i in $1
do
        if [[ -e $i ]]
        then
                cd ${DIR}/Mon/loaded
                echo "copying files to $GRS_DIR"
                cp * ${DIR}/Mon/
                echo "Files of Monday are Copied"
        fi
done
}
for i in $DIR/Mon/*
do
        check_directories $i
done

This is a function based script. Please let us know if you have any queries.


Thanks,
R. Singh
# 3  
Old 09-08-2014
Hi Singh,

But this would work in $DIR/Mon/loaded folder alone i need to acces other directoreis as well
$DIR/tue/loaded
$DIR/wed/loaded
.
.


and in your cat test.ksh you want me to add these details?
# 4  
Old 09-08-2014
try
Code:
for subdir in mon tue wed thu fri sat
do
if [[ -e ${DIR}/${subdir}/loaded ]]
then
cd ${DIR}/${subdir}/loaded
cp *.* ${DIR}/${subdir}
echo "Files of ${subdir}day are Copied"
cd ${DIR}
fi
done

This User Gave Thanks to Makarand Dodmis For This Post:
# 5  
Old 09-08-2014
Hello Rohit,

You can use following.

Code:
cat test.ksh
check_directories() {
for i in $1
do
        if [[ -e $i ]]
        then
                cd ${DIR}/Mon/loaded
                echo "copying files to $GRS_DIR"
                cp * ${DIR}/Mon/
                echo "Files of Monday are Copied"
        fi
done
}
for i in $DIR/*
do
        check_directories $i
done

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 09-08-2014
How about
Code:
for SUBDIR in Mon Tue Wed Thu Fri Sat
do cp -v ${DIR}/$SUBDIR/loaded/* ${DIR]/$SUBDIR
done

and react on the messages?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to make faster loop in multiple directories?

Hello, I am under Ubuntu 18.04 Bionic. I have one shell script run.sh (which is out of my topic) to run files under multiple directories and one file to control all processes running under those directories (control.sh). I set a cronjob task to check each of them with two minutes of intervals.... (3 Replies)
Discussion started by: baris35
3 Replies

2. Programming

Accessing directories in Linux

I'm new to Linux and trying to port a c++ program from windows. what I'm trying to do is copy a file to a directory off the root of the drive First off the program is located and running from Drive:\Base\Web\Today\Program.exe And trying to copy to: Drive:\Base\cpics windows... (10 Replies)
Discussion started by: pinbot
10 Replies

3. Shell Programming and Scripting

Issue with accessing value inside while loop, outside it

Hi, GetName() { if then echo " Please enter the name: " read Name tempvar=0 while read line do if then tempvar=`expr $tempvar + 1` echo $tempvar ... (10 Replies)
Discussion started by: rituparna_gupta
10 Replies

4. Shell Programming and Scripting

accessing variable from while loop

Hi all, Here is an outline of the problem: #variable declared at start of script x=0; #a function that increments x by 1 every 10 seconds incrementX(){ increments x every 10 seconds; } #i want this to output the value of x every second. The problem is that x is always reported... (3 Replies)
Discussion started by: free2rhyme2k
3 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Accessing Multiple files using for loop

Hi All, I have some files in my directory, and i want to pull all data using for loop....I am using following code but getting error..! for file in {file1, file2, file3, ..... filen} do L="$(tail -1 $file)";NUM=${L%%|*};DAT=${L##*|} echo $NUM>>filedata.txt done Error: tail:... (3 Replies)
Discussion started by: fidelis
3 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. SuSE

Regarding accessing multiple servers using single public ip address

Hello, Currently we are having different linux servers (for example: let's assume audio server, video server and text server) to handle requests from outside users. Suppose the outside users in different LAN (Local Area Network), other than the servers. For example user is in 20 series LAN and... (5 Replies)
Discussion started by: navneet_2009
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