0

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.

  • 2
    Have you tried something so far? How would you know what files to copy? – guillermo chamorro Jun 26 at 19:27
  • 1
    What are the criteria that tell apart files to copy from the rest? – Kamil Maciorowski Jun 26 at 19:28
  • 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? – mywayz Jun 26 at 19:45
  • 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. – guillermo chamorro Jun 26 at 20:06
1

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/

  • Well, since we need an explicit list anyway, why not just cp FILE_1 FILE_2 FILE_3 /destination/directory/? Side note: quote variables. – Kamil Maciorowski Jun 26 at 20:37
  • Yep, that's definitely the fastest and most pragmatic approach. My thought was that perhaps OP would want to expand on the loop their self, perhaps iterating through the contents of a text file containing the file names. This was mostly food for thought. – alex_crow Jun 26 at 20:42
  • 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 – mywayz Jun 27 at 2:02
  • 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. – alex_crow Jun 27 at 14:37
0

If the files have similar names, you could use a wildcard, like cp test* ~/test/ to copy all files starting with 'test' to the ~/test directory.

To copy the visible (non-dot) files in the directory, you could do cp ./* ~/test/ instead.

  • 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? – mywayz Jun 26 at 19:47
0

The simplest method doesn't require any scripting ability on your part. It only requires a text editor. Certainly there are more elegant and more generalized ways to do it, but sometimes one just wants to accomplish an immediate task without having to detour into a lot of learning curve climbing and such, if your scripting skills are at a novice level.

Let's suppose you have a file called myfiles.txt which contains a list of every filename you want to copy, one to a line. I'm going to assume:

  • you have fewer than one million files in the list
  • each line of myfiles.txt contains the correct path and filename of that source file
  • the directory path you're copying the files to is test and it already exists
  • you have no problematic characters in your filenames like quotes, apostrophes, newlines and the like.

Now say:

vi myfiles.txt
:s/^/cp -vp '/999999
1G:s-$-' test/-999999
ZZ

That will edit your list of files to put cp -vp ' at the beginning of every line (every filename), and put ' test/ at the end of every filename.

You now have a list of N cp commands that will copy your N files into the test/ directory. You can execute it by saying:

sh < myfiles.txt

Voila.

  • 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.? – mywayz Jun 27 at 19:58
  • One line at a time should work. – Jim L. Jun 27 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.