Linux commands to copy one file to many files

后端 未结 9 1023
失恋的感觉
失恋的感觉 2020-12-23 11:10

Is there a one-line command/script to copy one file to many files on Linux?

cp file1 file2 file3

copies the first two files into the third.

相关标签:
9条回答
  • 2020-12-23 11:52

    The simplest/quickest solution I can think of is a for loop:

    for target in file2 file3 do; cp file1 "$target"; done
    

    A dirty hack would be the following (I strongly advise against it, and only works in bash anyway):

    eval 'cp file1 '{file2,file3}';'
    
    0 讨论(0)
  • 2020-12-23 11:53

    Does

    cp file1 file2 ; cp file1 file3
    

    count as a "one-line command/script"? How about

    for file in file2 file3 ; do cp file1 "$file" ; done
    

    ?

    Or, for a slightly looser sense of "copy":

    tee <file1 file2 file3 >/dev/null
    
    0 讨论(0)
  • 2020-12-23 12:01

    just for fun, if you need a big list of files:

    tee <sourcefile.jpg targetfiles{01-50}.jpg >/dev/null- Kelvin Feb 12 at 19:52

    But there's a little typo. Should be:

    tee <sourcefile.jpg targetfiles{01..50}.jpg >/dev/null

    And as mentioned above, that doesn't copy permissions.

    0 讨论(0)
  • 2020-12-23 12:07
    for FILE in "file2" "file3"; do cp file1 $FILE; done
    
    0 讨论(0)
  • 2020-12-23 12:10

    You can improve/simplify the for approach (answered by @ruakh) of copying by using ranges from bash brace expansion:

    for f in file{1..10}; do cp file $f; done
    

    This copies file into file1, file2, ..., file10.

    Resource to check:

    • http://wiki.bash-hackers.org/syntax/expansion/brace#ranges
    0 讨论(0)
  • 2020-12-23 12:11

    You can use standard scripting commands for that instead:

    Bash:

     for i in file2 file3 ; do cp file1 $i ; done
    
    0 讨论(0)
提交回复
热议问题