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

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

    Wildcards also work with Roberts code

    echo ./fs*/* | xargs -n 1 cp test 
    
    0 讨论(0)
  • 2020-12-02 04:24

    Suppose you want to copy fileName.txt to all sub-directories within present working directory.

    1. Get all sub-directories names through ls and save them to some temporary file say, allFolders.txt

      ls > allFolders.txt
      
    2. Print the list and pass it to command xargs.

      cat allFolders.txt | xargs -n 1 cp fileName.txt
      
    0 讨论(0)
  • 2020-12-02 04:25

    To use copying with xargs to directories using wildcards on Mac OS, the only solution that worked for me with spaces in the directory name is:

    find ./fs*/* -type d -print0 | xargs -0 -n 1 cp test 
    

    Where test is the file to copy
    And ./fs*/* the directories to copy to

    The problem is that xargs sees spaces as a new argument, the solutions to change the delimiter character using -d or -E is unfortunately not properly working on Mac OS.

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

    If you need to be specific on into which folders to copy the file you can combine find with one or more greps. For example to replace any occurences of favicon.ico in any subfolder you can use:

    find . | grep favicon\.ico | xargs -n 1 cp -f /root/favicon.ico
    
    0 讨论(0)
提交回复
热议问题