Results 1 to 4 of 4

Thread: How to copy one file to multiple sibling folders using the terminal?

  1. #1
    Join Date
    Dec 2015
    Beans
    12

    How to copy one file to multiple sibling folders using the terminal?

    I have folders: a b c d and e and in folder a I have a file I want to replicate to b c d and e . Example: 1.txt is the file, so I want to have a/1.txt b/1.txt c/1.txt d/1.txt e/1.txt . How do I achieve that with one command? I tried cp -R 1.txt ../*/1.txt but it doesn't work. Any solution? Thanks!!

  2. #2
    Join Date
    Dec 2014
    Beans
    1,363

    Re: How to copy one file to multiple sibling folders using the terminal?

    'cp' accepts multiple sources but only one target. This can lead to ... interesting ... problems if you give multiple sources by using a glob and forget the target ...
    In your example you'd have to do something like 'cp a/1.txt b;cp a/1.txt c;cp a/1.txt d;cp a/1.txt e' or perhaps 'for i in b c d e ; do cp a/1.txt "$i"; done'. A discussion of other ways to do this -- some of them rather involved but offering some gains in speed when copying multiple large source-files to multiple targets -- can be found in this thread on superuser.

    Holger

  3. #3
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,531
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: How to copy one file to multiple sibling folders using the terminal?

    Code:
    for dir in b c d e ; do  cp a/1.txt "$dir" ; done

  4. #4
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    6,898
    Distro
    Xubuntu 19.10 Eoan Ermine

    Re: How to copy one file to multiple sibling folders using the terminal?

    Or if you want to use symlinks instead of making actual file copies - retain one copy of the file and link to it from many places - whether you want to do this depends on your application:
    Code:
    for dir in b c d e ; do  ln -s a/1.txt "$dir/1.txt" ; done

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •