Copying data from files to directories


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Copying data from files to directories

I have the following that I'd like to do:

1. I have split a file into separate files that I placed into the /tmp directory. These files are named F1 F2 F3 F4.
2. In addition, I have several directories which are alphabetized as dira dirb dirc dird.
3. I'd like to be able to copy F1 F2 F3 F4 to dira drib dirc dirb and so forth. Would this be done by use of an array? The dirnames will change each time but they will always be in alphabetical order. The file names will also be in order as F1 F2 F3 F4.
So the point is I'd like to match the file names to the directories so that F1 will copy into the first directory (one that is earliest in the alphabet).
I really haven't worked much with arrays and am wondering if anyone here has an idea?
I have created an array but if /how the cp command could be used to copy data into these directories, I am not sure.

Code:
dirs=(dira dirb dirc)
for dir in "${dirs[@]}"; do
    cp (not sure what /if this can be done)
done

I am sure this is not appropriate, but need some direction. Any suggestions as to how to approach this would be appreciated.
# 2  
Would this help?

Code:
#! /bin/bash

dirs=( dira dirb dirc dird )
fils=( F1   F2   F3   F4   )

i=0
while [ $i -lt ${#dirs[@]} ]
do
    cp /tmp/${fils[$i]} /path/to/${dirs[$i]}/
    (( i++ ))
done

This User Gave Thanks to balajesuri For This Post:
# 3  
For standard shells:
Code:
#!/bin/sh

echo "\
F1 dira
F2 dirb
F3 dirc
F4 dird" |
while read file dir
do
  cp "/tmp/$file" "/path/to/$dir/"
done

Or, robust against programs that read from stdin:
Code:
#!/bin/sh

while read file dir <&3
do
  cp "/tmp/$file" "/path/to/$dir/"
done 3<< _EOT
F1 dira
F2 dirb
F3 dirc
F4 dird
_EOT


Last edited by MadeInGermany; 05-17-2013 at 12:14 PM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Copying files to directories based on first 6 character
kenshinhimura
guys, i did create a script but its too long, though it function the same. # cat nightlyscan.sh #!/usr/ksh deyt=`date +"%Y-%m-%d"` for i in `ls -lrt|grep $deyt|awk '{print $9}'` do cp -f $i /S1/Sophos/logger/ done # but i did not paste it all. this is the desired.... Shell Programming and Scripting
9
Shell Programming and Scripting
Copying Directories from one server to another
arunkesi
Hi, I have a requirement where I have to connect to another server and copy directories from one server to another Directories on the Source server look like below (YYYY-MM-DD- 1 to 23) drwxr-xr-x 2 test_user dmfmart 422 Sep 1 23:45 2014-09-01-18 drwxr-xr-x 2 test_user dmfmart ...... UNIX for Dummies Questions & Answers
3
UNIX for Dummies Questions & Answers
Copying multiplies files into multiplies directories
Kossy
Hello ! I have a file structure like this: 1_0/file1.tiff 1_1/file2.tiff 1_2/file3.tiff 1_3/file4.tiff 1_4/file5.tiff 1_5/file6.tiff 1_6/file7.tiff 2_0/file8.tiff 2_1/file9.tiff 2_2/file10.tiff 2_3/file11.tiff etc. There is only one file in one directory. I'd like to have files...... Shell Programming and Scripting
8
Shell Programming and Scripting
Copying a files from a filter list and creating their associated parent directories
hunter55
Hello all, I'm trying to copy all files within a specified directory to another location based on a find filter of mtime -1 (Solaris OS). The issue that I'm having is that in the destination directory, I want to retain the source directory structure while copying over only the files that have...... Shell Programming and Scripting
4
Shell Programming and Scripting
Using find -d and copying to the found directories
Dean Rotherham
Hi again All :) After posting my first thread just a few eeks ago and having such a great response (Thank You once again :) ), I thought I'd perhaps ask the experts again. In short I'm trying to achieve a "find" and "copy" where the find needs to find directories: find -d -name outbox and...... UNIX for Dummies Questions & Answers
6
UNIX for Dummies Questions & Answers