How to copy a file to multiple directories using t

2019-01-09 22:10发布

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/
cp file1 {/foo/,/bar}

I know it's possible using a for loop, or find. But is it possible using the gnu cp command?

标签: linux bash shell
21条回答
做个烂人
2楼-- · 2019-01-09 22:35

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
    
查看更多
等我变得足够好
3楼-- · 2019-01-09 22:36

Wildcards also work with Roberts code

echo ./fs*/* | xargs -n 1 cp test 
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-09 22:38

For example if you are in the parent directory of you destination folders you can do:

for i in $(ls); do cp sourcefile $i; done

查看更多
登录 后发表回答