Copying a list of files

6,243

Given your comment on user79914's response, it sounds like you'll have to explicitly list the files you want copied in a regular old cp command. Something like the following should do the job:

cp FILE_1 FILE_2 FILE_3 /destination/directory

If, for instance, your copy operation is one you'll be doing more than once, you could do something like the following:

for FILE in $(cat ./list_of_files.txt)
do
    cp ${FILE} /destination/directory
done

This example assumes that you've added the list of desired files to the text file list_of_files.txt. The benefit of this approach, especially if you're having to perform the copy more than once, is that you can just add any new files you need copied to your list_of_files.txt file.

For more examples like this one, check out this link:

https://www.cyberciti.biz/faq/bash-for-loop/

Share:
6,243

Related videos on Youtube

mywayz
Author by

mywayz

Updated on September 18, 2022

Comments

  • mywayz
    mywayz almost 2 years

    I have a directory which has plenty of files which need to be copied over to another directory. Remember, not all files need to be copied, only some of them. Is there a way I can come up with a script to copy all these needed files from one directory to another instead of doing one by one.

    • schrodingerscatcuriosity
      schrodingerscatcuriosity almost 5 years
      Have you tried something so far? How would you know what files to copy?
    • Kamil Maciorowski
      Kamil Maciorowski almost 5 years
      What are the criteria that tell apart files to copy from the rest?
    • mywayz
      mywayz almost 5 years
      I did not try because I have about 100 files totally different names. How about if I put a script and list all those file names in one. and then run that script?
    • schrodingerscatcuriosity
      schrodingerscatcuriosity almost 5 years
      To make a reasonable script you need some pattern to follow when parsing the files. There is some pattern, a common characteristic in your files names? Check the answer below: If the files have similar names... do.... If there's no discernible pattern... well, there's not much to do.
  • mywayz
    mywayz almost 5 years
    Unfortunately, the files do not have similar names. Is there a way I can put all the needed files in one script file and then run that script which could copy all those files?
  • Kamil Maciorowski
    Kamil Maciorowski almost 5 years
    Well, since we need an explicit list anyway, why not just cp FILE_1 FILE_2 FILE_3 /destination/directory/? Side note: quote variables.
  • mywayz
    mywayz almost 5 years
    Hello Alex, so I am trying to understand this script file, sorry I am not good at writing script files. So looking at your script where you say for FILE in FILE_1 FILE_2 FILE_3 do cp ${FILE} /destination/directory done In this, I need replace just the FILE_1 FILE_2 FILE_3 and so on with my file names, is that correct? The next line where you ay cp $FILE, that remains as is? Thanks
  • alex_crow
    alex_crow almost 5 years
    Hey mywayz, yes, you're right. I've gone ahead and edited my initial reply though. I think it may be easier to understand. It also addresses Kamil Maciorowski's feedback.
  • mywayz
    mywayz almost 5 years
    Hello Jim, when I do vi my file.txt and the file opens, then copy pasting the whole command from colon to ZZ closes the file with cp -vp just visible on the last row, basically with just the last file name. So unlike you said that it puts cp -vp at beginning of every file, its not doing that. Should I enter it separately instead of copy pasting.?
  • Jim L.
    Jim L. almost 5 years
    One line at a time should work.