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

前端 未结 22 1956
终归单人心
终归单人心 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:01

    If you want to do it without a forked command:

    tee <inputfile file2 file3 file4 ... >/dev/null

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

    No - you cannot.

    I've found on multiple occasions that I could use this functionality so I've made my own tool to do this for me.

    http://github.com/ddavison/branch

    pretty simple -
    branch myfile dir1 dir2 dir3

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

    As far as I can see it you can use the following:

    ls | xargs -n 1 cp -i file.dat
    

    The -i option of cp command means that you will be asked whether to overwrite a file in the current directory with the file.dat. Though it is not a completely automatic solution it worked out for me.

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

    If all your target directories match a path expression — like they're all subdirectories of path/to — then just use find in combination with cp like this:

    find ./path/to/* -type d -exec cp [file name] {} \;
    

    That's it.

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

    You can't do this with cp alone but you can combine cp with xargs:

    echo dir1 dir2 dir3 | xargs -n 1 cp file1
    

    Will copy file1 to dir1, dir2, and dir3. xargs will call cp 3 times to do this, see the man page for xargs for details.

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

    These answers all seem more complicated than the obvious:

    for i in /foo /bar; do cp "$file1" "$i"; done
    
    0 讨论(0)
提交回复
热议问题