+ Post New Thread
Results 1 to 9 of 9
  1. #1
    Member level 2
    Points: 1,072, Level: 7

    Join Date
    Sep 2015
    Posts
    50
    Helped
    0 / 0
    Points
    1,072
    Level
    7

    Linux Command to Copy Multiple Files?

    In CentOS, I want to copy a rubric.docx file to multiple files with names such as F20161368.docx, F20161981.docx, F20161983.docx... from a list of names in a MS Excel or text file.

    Please help with a command or script to do that.

    •   AltAdvertisement

        
       

  2. #2
    Advanced Member level 4
    Points: 8,022, Level: 21
    Achievements:
    7 years registered

    Join Date
    Jul 2010
    Location
    Sweden
    Posts
    1,077
    Helped
    408 / 408
    Points
    8,022
    Level
    21

    Re: Linux Command to Copy Multiple Files?

    Code:
    cat destination_file_list.txt | xargs -n 1 cp source_file
    Edit: the "-T " option for cp isn't needed, so I removed it.
    Last edited by std_match; 7th February 2020 at 15:39.


    1 members found this post helpful.

  3. #3
    Member level 2
    Points: 1,072, Level: 7

    Join Date
    Sep 2015
    Posts
    50
    Helped
    0 / 0
    Points
    1,072
    Level
    7

    Re: Linux Command to Copy Multiple Files?

    Quote Originally Posted by std_match View Post
    Code:
    cat destination_file_list.txt | xargs -n 1 cp source_file
    Edit: the "-T " option for cp isn't needed, so I removed it.
    Where the files have been copied?... I couldn't find them!
    I used the following command...
    cat /home/user/Desktop/temp/list.txt | xargs -n 1 cp /home/user/Desktop/temp/rubric.docx



  4. #4
    Advanced Member level 4
    Points: 8,022, Level: 21
    Achievements:
    7 years registered

    Join Date
    Jul 2010
    Location
    Sweden
    Posts
    1,077
    Helped
    408 / 408
    Points
    8,022
    Level
    21

    Re: Linux Command to Copy Multiple Files?

    If there is no path in the destination file names, they should go to the current directory.



    •   AltAdvertisement

        
       

  5. #5
    Advanced Member level 5
    Points: 41,418, Level: 49

    Join Date
    Mar 2008
    Location
    USA
    Posts
    6,673
    Helped
    1971 / 1971
    Points
    41,418
    Level
    49

    Re: Linux Command to Copy Multiple Files?

    If it were me, I'd get the file list as plain text from
    "wherever", and edit it into a shell script with vi / vim.
    Then run it.

    :1,$s/^/cp \/home\/user\/Desktop\/temp\/rubric.docx &/

    would make

    F20161983.docx

    into

    cp /home/user/Desktop/temp/rubric.docx F20161983.docx


    and all the other lines in the file, similarly.


    I'm sure there's an elegant way to do it but I'd rather
    get it done that figure out pipes and whatnot. You could
    probably do a foreach x( {list} ) or something. But I find
    it easier to work on a script in a text editor, than to get
    100.00% correct command line typed out, first try.



  6. #6
    Advanced Member level 2
    Points: 5,541, Level: 17
    Achievements:
    7 years registered
    ZASto's Avatar
    Join Date
    Jan 2008
    Location
    Messy Corner, 44°47'31.56"N, 20°28'9.16"E
    Posts
    679
    Helped
    137 / 137
    Points
    5,541
    Level
    17

    Re: Linux Command to Copy Multiple Files?

    Did you invoke cp with --help as parameter
    or
    Code:
    man cp
    Make no mistake between my personality and my attitude.
    My personality is who I am.
    My attitude depends on who you are.



  7. #7
    Member level 2
    Points: 1,072, Level: 7

    Join Date
    Sep 2015
    Posts
    50
    Helped
    0 / 0
    Points
    1,072
    Level
    7

    Re: Linux Command to Copy Multiple Files?

    Quote Originally Posted by std_match View Post
    If there is no path in the destination file names, they should go to the current directory.
    the command worked... thanks. However, the files created have no extension. How to have .docx extension of the copied files?



    •   AltAdvertisement

        
       

  8. #8
    Advanced Member level 4
    Points: 8,022, Level: 21
    Achievements:
    7 years registered

    Join Date
    Jul 2010
    Location
    Sweden
    Posts
    1,077
    Helped
    408 / 408
    Points
    8,022
    Level
    21

    Re: Linux Command to Copy Multiple Files?

    Quote Originally Posted by emaq View Post
    the command worked... thanks. However, the files created have no extension. How to have .docx extension of the copied files?
    The file names will be exactly as in the "destination_file_list.txt".

    If the file names in the list don't have the .docx extension, you can add it with the "sed" command:

    Code:
    cat destination_file_list.txt | sed "s/$/\.docx/" | xargs -n 1 cp source_file



  9. #9
    Advanced Member level 5
    Points: 41,418, Level: 49

    Join Date
    Mar 2008
    Location
    USA
    Posts
    6,673
    Helped
    1971 / 1971
    Points
    41,418
    Level
    49

    Re: Linux Command to Copy Multiple Files?

    foreach x(*)
    mv $x $x.docx
    end



--[[ ]]--