Welcome to TechForums.info!   


Bash: Copying A Set Of Files To Multiple Locations?

Linux Discussion

Bash: Copying A Set Of Files To Multiple Locations?

Postby Ahren » Thu Oct 12, 2017 8:02 pm

In Linux scripting, I have a folder called "master" and "UNIX" with 5 subfolders of different users. There are 3 files in the master folder and the script must copy those files into the 5 user accounts simultaneously. Here is some code that I have tried to work on but I can't seem to get that bit to work. Thanks in advance


while read -r line; do

mkdir "UNIX/$line"

done < usernames.lnk



cat $MasterFile | #Copying the files from the MasterFile List.

while read FILENAME #"FILENAME" refers to each line of the MasterFile list.


cp $MasterDir/$FILENAME $USERHOME/$USER # Copies the files from the Master directory with the FILENAME specified when read to

echo "Files copied to $USER"
Posts: 24
Joined: Fri Jan 10, 2014 7:37 am

Bash: Copying A Set Of Files To Multiple Locations?

Postby Anmcha » Mon Oct 16, 2017 12:03 am

First off, you can't do this simultaneously. If the files are small then it'll all happen very quickly but it won't be simultaneous within the strict meaning of the word.

There are a couple of options, here. You can either copy the files or you can link to them (a bit like a Windows Shortcut). Linking has the advantage that it's really fast and efficient. It has the disadvantage that all users are sharing the same file, so if you allow them to edit the files this is not a viable solution.

Looking at your script, you need to be careful using the variable USER as this is (often) reserved.

I'm not entirely sure how you intend to identify the target directories - is the script just supposed to copy the files into all the subdirectories it can find?



while read FILE


    for UDIR in *


        test -d "$UDIR" || continue

        echo "Copying $FILE to $UDIR..." >&2

        cp -p "$FILE" "$UDIR/"


done < MasterFile
Posts: 20
Joined: Mon Apr 07, 2014 12:52 pm

Return to Linux


  • Related topics
    Last post