skuul
skuul

Reputation: 115

Copying a folder's children to multiple folders

Is there any sh script to copy all child elements in a folder to other multiple folders. What i have in mind is something like this. Folder Below will be copied.

/Parent/ChildDir1
/Parent/ChildDir2
/Parent/ChildFile1
/Parent/ChildFile2

Destinations would be like this

/X/a/
/X/b/
/X/c/

Overwriting is necessary.

Thanks

Upvotes: 0

Views: 146

Answers (2)

dogbane
dogbane

Reputation: 274532

You can use find to find all directories in X i.e. X/a, X/b, X/c etc and then recursively copy your source directory into them:

find /path/to/X -type d -maxdepth 1 -exec cp -r /path/to/Parent {} \;

If your directory list is in a file you can just read each line in the file and execute cp, like this:

while IFS= read -r dir
do
    cp -r /path/to/Parent "$dir"
done < dirs.txt

Upvotes: 0

carlpett
carlpett

Reputation: 12583

As I understand you, you want to copy everything from /Parent/ to several destination folders? So each of /X/a/, /X/b/ and /X/c/ have their own, independent copies. In that case, you could just loop over all destinations like this:

DESTS = "/X/a/:/X/b/:/X/c/:"
SRC = "/Parent/"
while read -d: ddir; do 
    cp -r "$SRC" "$ddir"
done <<< $DESTS

The extra : at the end of DESTS might be removable if you find the right invocation of read, I couldn't :(

Upvotes: 1

Related Questions