Search code examples
linuxbashunixdirectorydirectory-structure

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?


Solution

  • 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 {} \;