What is the source and destination in the following scp command? - linux

I am trying to copy a file from one Linux system to another Linux system. Am I correct in my attempt with scp? What are source and destination in this command
scp -r ~/setup ashok#192.168.5.223: ~/
?

Basic Syntax
scp source_file_name username#destination_host:destination_folder
In your case
~/setup : is the source [~ : root directory and setup is a folder which you want to copy to a destination]
ashok#192.168.5.223: is the destination
~/: location in a destination [in this case which is a root directory]
To understand more hit below link
http://www.hypexr.org/linux_scp_help.php

This questions suits better in unix.stackexchange.
Here, a file named setup in your home directory (in the machine you're executing this command on) is the source and home directory on machine ashok#192.168.5.223 is the destination. It's a command to copy, it won't move (you'll retain the original copy), so if that's what you want, then you're right with scp command.

Related

Bash, copy files from folder to another

I have a problem with an Uni OS Course assignment.
Basically the task says:
Deliver now a file for assessment. The content of the file is: one line, containing a command that
copies all files with prefix "2016", from directory "ExercisesOS" to directory "OSLab".
Consider the current directory to be "~" when writing such command.
I have already tried with that code:
cp /ExercisesOS/2016* /OSLab
but it performs me two error.
How can I write the correct command?
You probably want to copy from the directory you are working.
To check where you are working:
$ pwd
/home/userdir
To copy from your working directory:
$ cp ExerciseOS/2016* OSLab/
mkdir OSLab && cp /ExercisesOS/2016* OSLab
This solution would assume that the directory 'OSLab' isn't already created.

I am trying to copy entire directories from one Linux machine to another, including permissions

I am trying to copy entire directories from one Linux machine to another, including permissions, and including the called directory. For example,
from the parent directory of the destination, I tried
rsync -rul root#mail3.domain.com/usr/sites/4my.com
but that does not work.
On the calling machine the destination is /home/sites
What should that command be?
Thanks!
try rsync -a username#remote_host:/home/username/dir1 place_to_sync_on_local_machine
Try
sudo cp -rp /home/source /tmp/dest

copy directory from another computer on Linux

On a computer with IP address like 10.11.12.123, I have a folder document. I want to copy that folder to my local folder /home/my-pc/doc/ using the shell.
I tried like this:
scp -r smb:10.11.12.123/other-pc/document /home/my-pc/doc/
but it's not working.
So you can use below command to copy your files.
scp -r <source> <destination>
(-r: Recursively copy entire directories)
eg:
scp -r user#10.11.12.123:/other-pc/document /home/my-pc/doc
To identify the location you can use the pwd command, eg:
kasun#kasunr:~/Downloads$ pwd
/home/kasun/Downloads
If you want to copy from B to A if you are logged into B: then
scp /source username#a:/destination
If you want to copy from B to A if you are logged into A: then
scp username#b:/source /destination
In addition to the comment, when you look at your host-to-host copy options on Linux today, rsync is by far, the most robust solution around. It is brought to you by the SAMBA team[1] and continues to enjoy active development. Most distributions include the rsync package by default. (if not, you should find an easily installable package for your distro or you can download it from rsync.samba.org ).
The basic use of rsync for host-to-host directory copy is:
$ rsync -uav srchost:/path/to/dir /path/to/dest
-uav simply recursively copies -ua only new or changed files preserving file & directory times and permissions while providing -v verbose output. You will be prompted for the username/password on 10.11.12.123 unless your have setup ssh-keys to allow public/private key authentication (see: ssh-keygen for key generation)
If you notice, the syntax is basically the same as that for scp with a slight difference in the options: (e.g. scp -rv srchost:/path/to/dir /path/to/dest). rsync will use ssh for secure transport by default, so you will want to insure sshd is running on your srchost (10.11.12.123 in your case). If you have name resolution working (or a simple entry in /etc/hosts for 10.11.12.123) you can use the hostname for the remote host instead of the remote IP. Regardless, you can always transfer the files you are interested in with:
$ rsync -uav 10.11.12.123:/other-pc/document /home/my-pc/doc/
Note: do NOT include a trailing / after document if you want to copy the directory itself. If you do include a trailing / after document (i.e. 10.11.12.123:/other-pc/document/) you are telling rsync to copy the contents, (i.e. the files and directories under) document to 10.11.12.123:/other-pc/ without also copying the document directory.
The reason rsync is far superior to other copy apps is it provides options to truly synchronize filesystems and directory trees both locally and between your local machine and remote host. Meaning, in your case, if you have used rsync to transfer files to /home/my-pc/doc/ and then make changes to the files or delete files on 10.11.12.123, you can simply call rsync again and have the changes/deletions reflected in /home/my-pc/doc/. (look at the several flavors of the --delete option for details in rsync --help or in man 1 rsync)
For these, and many more reasons, it is well worth the time to familiarize yourself with rsync. It is an invaluable tool in any Linux user's hip pocket. Hopefully this will solve your problem and get you started.
Footnotes
[1] the same folks that "Opened Windows to a Wider World" allowing seemless connection between windows/Linux hosts via the native windows server message block (smb) protocol. samba.org
If the two directories (document and /home/my-pc/doc/) you mentioned are on the same 10.11.12.123 machine.
then:
cp -ai document /home/my-pc/doc/
else:
scp -r document/ root#10.11.12.123:/home/my-pc/doc/

scp + Avoid copy if the same file name located on remote machine?

Is there any option to tell scp command - not copy file from current machine in case file exists on remote machine
For example
On my machine I have the file -
/etc/secret-pw.txt
On Remote machine I have also the file -
/etc/secret-pw.txt
So
scp /etc/secret-pw.txt $remote_machine:/etc
Will destroy the secret-pw.txt, and scp will not ask questions about: overwrite the target file
Is there any option to avoid copy if file exist on target machine by scp?
Update: I can't install rsync or any other program.
You should be using rsync instead of scp. It will give you what you need.
If you can't install rsync (as you mentioned in the comments) you need to run a script beforehand to check if file exists and run it with ssh.
SCP does not offer any option, unfortunately.
But you can resort standard tools, like this:
ssh $remote_machine -- cp --no-clobber /dev/stdin /etc/secret-pw.txt < /etc/secret-pw.txt
Note that with this trick you gain all the functionalities of cp.

Is this the correct syntax to scp a downloaded file into Ubuntu?

I downloaded the latest version of swig that I need to move to Ubuntu. I was hoping someone could help identify what part of my syntax is incorrect. I've tried a number of variations but I can't quite seem to get it write.
I've tried
scp swig-3.0.2 user_name#111.111.11.111: swig-3.0.2
swig-3.0.2 is a directory (not copied)
scp swig-3.0.2 drubio#192.168.56.101: /home/drubio/swig-3.0.2
No such file or directory
I've tried implementing directions that I've found on askubuntu.com reproduced here:
# copy a file from local machine to server1.com
user#local-machine# scp ./somefile.txt user1#server1.com:/home/user2
# copy a file from server1.com to server2.com
user#local-machine# ssh user1#server1.com
user1#server1# scp ./somefile.txt user2#server2.com:/home/user2
user#server1# logout
# copy a file from server2.com to server1.com
user#local-machine# ssh user2#server2.com
user2#server2# ls
somefile.txt otherfile.txt
user2#server2# scp ./otherfile.txt user1#server1.com:/home/user1
user2#server2# logout
# can't copy a file TO local-machine because it's not accessible from internet
All I'm trying to do is to copy the downloaded swig-3.03 located on my local machine's Desktop on to Ubuntu. I've checked on my local machine where I'm at and I've verified that I am in the Desktop directory. My username is correct and the path is right. I'm assuming that the mistake is the destination point. Am I wrong to assume this?
The correct syntax is:
scp swig-3.0.2 user_name#111.111.11.111:swig-3.0.2
scp swig-3.0.2 drubio#192.168.56.101:/home/drubio/swig-3.0.2
Without the spaces!
The usage is for copy local to remote:
scp /path/to/my_local_file user#host:/path/to/my_copy_file
or for copy remote to local:
scp user#host:/path/to/my_remote_file /path/to/my_copy_file

Resources