One file in to one directory using a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting One file in to one directory using a for loop
# 1  
Old 04-08-2013
One file in to one directory using a for loop

So I have several files:

Code:
1.txt 2.txt 3.txt 4.txt

and several directories:
Code:
one two three four

I want to put one file in one directory, thusly:

Code:
1.txt to /one
2.txt to /two
3.txt to /three
4.txt to /four

but I want to use a for loop to do something like this:

Code:
!#/usr/bin/bash
file=(1.txt 2.txt 3.txt 4.txt)
dir=(one two three four)

for i in ${file[@]};do
        cp $i ${dir[@]}
        done

This will obviously attempt to put EVERY file in EVERY directory. How would I limit it as stated above?

Thanks,
# 2  
Old 04-08-2013
You could do something like below using a counter:
Code:
file=( 1.txt 2.txt 3.txt 4.txt )
dir=( one two three four )
c=0

for i in ${file[@]};
do
        cp $i ${dir[$c]}
        (( ++c ))
done

This User Gave Thanks to Yoda For This Post:
# 3  
Old 04-08-2013
In Solaris?

Quote:
Originally Posted by Yoda
You could do something like below using a counter:
Code:
file=( 1.txt 2.txt 3.txt 4.txt )
dir=( one two three four )
c=0

for i in ${file[@]};
do
        cp $i ${dir[$c]}
        (( ++c ))
done

I always for get to include the environment...
# 4  
Old 04-08-2013
Quote:
Originally Posted by os2mac
I always for get to include the environment...
It should work in Solaris. I tested using GNU bash, version 3.2.51(1)-release (sparc-sun-solaris2.10) and it works.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 04-09-2013
Thanks so much.
# 6  
Old 04-09-2013
Why not
Code:
$ file=(1.txt 2.txt 3.txt 4.txt)
$ dir=(one two three four)
$ echo ${#dir[*]}
4
$ for ((i=0; i<${#file[@]}; i++)); do echo cp ${file[i]} ${dir[i]}; done
cp 1.txt one
cp 2.txt two
cp 3.txt three
cp 4.txt four

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop through files in directory

I am trying to loop through files in a directory, and sort each file. No matter what changes I make to the code, I get the following errors: 'aunch.sh: line 4: syntax error near unexpected token `do 'aunch.sh: line 4: `for f in ${FILES}/*; do #!/bin/bash FILES=$(pwd) for f in ${FILES}/*;... (6 Replies)
Discussion started by: ldorsey
6 Replies

2. Shell Programming and Scripting

Loop through files in directory

I am trying to loop through files in a directory, and sort each file. No matter what changes I make to the code, I get the following errors: 'aunch.sh: line 4: syntax error near unexpected token `do 'aunch.sh: line 4: `for f in ${FILES}/*; do #!/bin/bash FILES=$(pwd) for f in ${FILES}/*;... (1 Reply)
Discussion started by: ldorsey
1 Replies

3. Shell Programming and Scripting

Loop multiple directory, find a file and send email

Hello ALL, need a BASH script who find file and send email with attachment. I have 50 folders without sub directories in each generated files of different sizes but with a similar name Rp01.txt Rp02.txt Rp03.txt ...etc. Each directors bound by mail group, I need a script that goes as... (1 Reply)
Discussion started by: penchev
1 Replies

4. Shell Programming and Scripting

Loop through files in a directory

Hello How do I loop through files in a specific directory ? This script is not working! #! /bin/bash FILES=/usr/desktop/input/* For f in $FILES; do awk '-v A="$a" -v B="$b" {$6=($1-64)/2 ;$7=((10^($6/10))/A)^(1/B) ; print}' OFS="\t" $f > /root/Desktop/output/$f.txt; done ... (7 Replies)
Discussion started by: ali.seifaddini
7 Replies

5. UNIX for Dummies Questions & Answers

Loop through directory names

Some guidance is highly appreciated. I have 10 directories with names ending with 'xyz', each of them have about 30000 files. I want to loop through the contents of each directory and produce a single output per directory. So I want to have 10 output files named 'directory_name'_out. With... (1 Reply)
Discussion started by: newbie83
1 Replies

6. UNIX for Dummies Questions & Answers

Loop through directory and extract sub directory names

I am trying to loop through folders and extract the name of the lowest level subfolder I was running the script below, it returns /bb/bin/prd/newyork /bb/bin/prd/london /bb/bin/prd/tokyo I really want newyork london tokyo I couldn't find a standard variable for the lowest level... (1 Reply)
Discussion started by: personalt
1 Replies

7. Shell Programming and Scripting

loop through files in directory

hi all i have some files present in a directory i want to loop through all the files in the directory each time i loop i should change the in_file parameter in the control file and load it into a table using sql loader there is only one table where i have to load alll the files ... (3 Replies)
Discussion started by: rajesh_tns
3 Replies

8. Shell Programming and Scripting

Directory sizes loop optimization

I have the following script: #!/usr/bin/ksh export MDIR=/datafiles NAME=$1 SERVER=$2 DIRECTORY=$3 DATABASE=$4 ID=$5 export dirlist=`/usr/bin/ssh -q $ID@$SERVER find $DIRECTORY -type d -print` for dir in $dirlist do SIZE=`</dev/null /usr/bin/ssh -q $ID@$SERVER du -ks $dir` echo... (6 Replies)
Discussion started by: la_womn
6 Replies

9. AIX

loop through the directory for files and sort by date and process the first file

hello i have a requirement where i have a direcotry in which i get files in the format STOCKS.20080114.dat STOCKS.20080115.dat STOCKS.20080117.dat STOCKS.20080118.dat i need to loop through the directory and sort by create date descending order and i need to process the first file. ... (1 Reply)
Discussion started by: dsdev_123
1 Replies

10. Shell Programming and Scripting

Loop through files in a directory

Hi, I want to write bash script that will keep on looking for files in a directory and if any file exists, it processes them. I want it to be a background process, which keeps looking for files in a directory. Is there any way to do that in bash script? I can loop through all the files like... (4 Replies)
Discussion started by: rladda
4 Replies
Login or Register to Ask a Question