Copying data from files to directories

Tags
shell scripts

 
Thread Tools Search this Thread
# 1  
Old 05-16-2013
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  
Old 05-16-2013
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:
newbie2010 (05-16-2013)
# 3  
Old 05-17-2013
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 01:14 PM..

|
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 Shell Programming and Scripting 9 06-16-2015 10:47 AM
Archiving and moving files into directories, creating directories, etc. wyclef Shell Programming and Scripting 5 03-25-2015 07:30 PM
Copying Directories from one server to another arunkesi UNIX for Dummies Questions & Answers 3 09-03-2014 02:43 AM
Error check for copying growing directories ningy Shell Programming and Scripting 11 06-03-2013 01:07 PM
How to list all the files, directories and sub-directories in the current path except one directory? Manjunath B Shell Programming and Scripting 7 09-13-2012 11:00 AM
SQL: copying data down figaro Programming 8 12-08-2011 02:35 PM
Copying all directories while ignoring certain filetypes pasc Shell Programming and Scripting 8 10-17-2011 03:28 PM
List directories and sub directories recursively excluding files pointers UNIX for Dummies Questions & Answers 3 02-23-2011 09:39 PM
Copying multiplies files into multiplies directories Kossy Shell Programming and Scripting 8 02-14-2011 05:34 AM
Copying a files from a filter list and creating their associated parent directories hunter55 Shell Programming and Scripting 4 05-05-2010 05:22 PM
Backup script: Copying and removing directories based on list dotancohen Shell Programming and Scripting 4 02-04-2010 05:30 PM
Using find -d and copying to the found directories Dean Rotherham UNIX for Dummies Questions & Answers 6 09-07-2009 02:24 PM
copying to multiple directories using wildcard logic0 UNIX for Dummies Questions & Answers 4 05-22-2007 09:09 AM
Copying multiple directories at the same time using Unix JPigford UNIX for Dummies Questions & Answers 9 01-17-2005 05:16 PM