Batuhan B
Batuhan B

Reputation: 1865

Copy file into multiple directories - Unix

I want to copy one file into many directory. My path structure is like above.

folder1-> anotherfolder -> **here I want to copy my file**
folder2-> anotherfolder -> **here I want to copy my file**
folder3-> anotherfolder -> **here I want to copy my file**
folder4-> anotherfolder -> **here I want to copy my file**

folder1,2,3,4 is in the same directory. But the names are the folders are not sequential.

I can get the name of folders with this code but after that I don't know how can I get into the folders and copy my file.

    for d in */ ; do
     echo "$d"
    done

This code gives me the folders name in directory. After this step how can I get into folders and copy my file?

Upvotes: 0

Views: 250

Answers (2)

Eugeniu Rosca
Eugeniu Rosca

Reputation: 5315

First try (has limitations)

for d in $(find . -maxdepth 2 -type d | grep "/.*/"); do
    cp file "$d"
done

LIMITATIONS:

  • No spaces/slashes/glob characters in the dir-names

Second try (cleaner, thanks to gniourf_gniourf)

find . -maxdepth 2 -path './*/*' -type d -exec cp file {} \;

Upvotes: 1

Tom Fenech
Tom Fenech

Reputation: 74705

Seems like you want to do something like this:

for d in */; do
    cp file "$d"/anotherfolder
done

Upvotes: 1

Related Questions