LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to copy files using command line in Linux (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-copy-files-using-command-line-in-linux-919780/)

jeyachandran1988 12-21-2011 01:28 AM

How to copy files using command line in Linux
 
Hi

I need to copy files in linux server to another Linux server using command line but i need to one file alone but i need to copy rest of the files in one command

Thanks in advance

EricTRA 12-21-2011 01:32 AM

Hi,

I'm having a hard time understanding what you mean. Do you need to copy one file or all files? Whatever you need to copy over from one Linux server to another, assuming SSH is installed and running, you could use scp which is part of the openssh package.
Code:

scp file(s)/wildcard user@host:/path
Kind regards,

Eric

evo2 12-21-2011 01:32 AM

Hi,

I guess you've already discovered how to use scp for a single file. scp can also copy recursively (ie a whole directory tree). Have a look at the -r option in the scp man page. You might also like to have a look at the rsync command. Finally you can simply list multiple files on the local file system.
Eg
Code:

me@mymachine% scp file1.foo file2.bar file3.baz alsome@myothermache:
HTH,

Evo2.

jeyachandran1988 12-21-2011 01:39 AM

Hi EricTRA

Thanks for your reply

Actually I need to copy folder or files b/w two linux server. For example i need to copy 95 folders or files out of 100 in command line

Please do the needful

Thanks

EricTRA 12-21-2011 01:46 AM

Hello,

Nobody here will 'do the needful' for you my friend. You're the one that needs to put in the work. As indicated by Evo2, read the man pages for scp and rsync. Either of those commands can do the job. By reading the man pages and trying for yourself you'll learn a lot more then when we give you a ready made solution, which by the way is not the way LQ works. Normal processing would be that you try it yourself and if you fail and don't find an answer, then you turn to us to point you in the right direction.

Kind regards,

Eric

salasi 12-21-2011 11:18 AM

Quote:

Originally Posted by jeyachandran1988 (Post 4555041)

Actually I need to copy folder or files b/w two linux server. For example i need to copy 95 folders or files out of 100 in command line

Well, in schematic (overview), you have a choice of about two ways, and a number of sub-variants.

One of them is to set up some kind of regular expression that matches only the files that you want to copy.

The other is to copy all of the files, and delete the ones that you don't want.

The second of those two is often regarded as unsophisticated, but may well be massively easier to set up, in some particular circumstance, and in that context, people, me included, can find sophistication to be an overrated virtue. You must make your own determination on what you think about this, and how it applies to your particular situation.

Regarding the first method, it is almost entirely dependant on the patterns in the names whether this is easy to achieve, because you have to be able to write patterns which match one set of files and not the other set. As you have told us nothing about the 'patterns' (assuming some patterns exist) aspect, it is not something that we can do anything about. (Oh, and if you have made the mistake of allowing spaces and strange characters into the filenames, that will increase the degree of difficulty, a little.)

One minor variant that you might want to consider is to copy all of the files that you want to copy from one machine to another into a new sub-directory. This way, you can easily check that the correct files are queued up for processing. And, if you do employ the 'copy all, delete a few' method (method two), then doing the deletion on a dedicated sub-directory is a tactic that can make it less likely that you accidentally delete a wanted file. assuming that you always keep track of which directory you are in, of course...

Satyaveer Arya 12-21-2011 12:19 PM

Hello Jeyachandran,

Using this command for an example in my case you can copy all files excluding .FOO and .BAR files:

Quote:

[root@stg02 ~]# rsync -avze ssh --exclude='*.FOO' --exclude='*.BAR' /var/www/sarg/weekly/* <dest._IPAdd.>:/logs/Linux_Server_Logs/sarg/Dec-2011/weekly/
This above command is for copying and then after copying you can delete particular files and can keep one whichever you want...

theNbomr 12-21-2011 01:01 PM

It sounds like this is something you want to do repeatedly. If so, create a text file containing the names of each file that you need to copy, one filename per line. Then, you can use it as input to a looping script that copies iteratively.
Code:

while read sourcefile; do scp "$sourcefile" someuser@destinationServer; done < yourFileList.txt
Use SSL shared keys to avoid password re-entry.

--- rod.


All times are GMT -5. The time now is 07:53 AM.