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

Would this help?

#! /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
1 Like

For standard shells:

#!/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:

#!/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