简体   繁体   中英

how to copy file to multiple sub directories linux

I have a file needs to copy unique directory call test directory structure as below

/contentroot/path/a/x/test
/contentroot/path/a/y/test
/contentroot/path/a/z/test
--------------------------

as above I have more then 250 combination test directory

I have try below command ( by using asterisk) but it's only copy one test directly only and giving issue (cp: omitting directory )

cp myfile.txt /contentroot/path/a/*/test

any Help

Perhaps a for loop?

for FOLDER in /contentroot/path/a/*/test; do
    cp myfile.txt $FOLDER
done

You can feed the output of an echo as an input to xargs. xargs will then run the cp command three times, appending the next directory path piped to it from the echo.

The -n 1 option on the xargs command is so it only appends one of those arguments at a time to the cp each time it runs.

echo /contentroot/path/a/x/test /contentroot/path/a/y/test /contentroot/path/a/z/test | xargs -n 1 cp myfile.txt

Warnings! Firstly this will over-write files (if they exist) and secondlt any bash command should be tested and used at the runners risk! ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM