How to copy a file to multiple directories using the gnu cp command

前端 未结 22 1959
终归单人心
终归单人心 2020-12-02 03:25

Is it possible to copy a single file to multiple directories using the cp command ?

I tried the following , which did not work:

cp file1 /foo/ /bar         


        
相关标签:
22条回答
  • 2020-12-02 04:07

    I would use cat and tee based on the answers I saw at https://superuser.com/questions/32630/parallel-file-copy-from-single-source-to-multiple-targets instead of cp.

    For example:

    cat inputfile | tee outfile1 outfile2 > /dev/null
    
    0 讨论(0)
  • 2020-12-02 04:07

    This will copy to the immediate sub-directories, if you want to go deeper, adjust the -maxdepth parameter.

    find . -mindepth 1 -maxdepth 1 -type d| xargs -n 1 cp -i index.html
    

    If you don't want to copy to all directories, hopefully you can filter the directories you are not interested in. Example copying to all folders starting with a

    find . -mindepth 1 -maxdepth 1 -type d| grep \/a |xargs -n 1 cp -i index.html
    

    If copying to a arbitrary/disjoint set of directories you'll need Robert Gamble's suggestion.

    0 讨论(0)
  • 2020-12-02 04:08

    Using a bash script

    DESTINATIONPATH[0]="xxx/yyy"
    DESTINATIONPATH[1]="aaa/bbb"
                    ..
    DESTINATIONPATH[5]="MainLine/USER"
    NumberOfDestinations=6
    
    for (( i=0; i<NumberOfDestinations; i++))
        do
            cp  SourcePath/fileName.ext ${DESTINATIONPATH[$i]}
    
        done
    exit
    
    0 讨论(0)
  • 2020-12-02 04:10

    ls -db di*/subdir | xargs -n 1 cp File

    -b in case there is a space in directory name otherwise it will be broken as a different item by xargs, had this problem with the echo version

    0 讨论(0)
  • 2020-12-02 04:14
    ls -d */ | xargs -iA cp file.txt A
    
    0 讨论(0)
  • 2020-12-02 04:14

    if you want to copy multiple folders to multiple folders one can do something like this:

    echo dir1 dir2 dir3 | xargs -n 1 cp -r /path/toyourdir/{subdir1,subdir2,subdir3}

    0 讨论(0)
提交回复
热议问题