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.
Related
I have directory(/usr/share/hub-bucket/GameImages/) which has sub directories which contains files. And I want to transfer the sub-directories to machine to location /usr/share/hub-bucket/GameImages/. Both are remote machine, I can access remote destination using SSH private key and passphrase. And in future I will need to sync both the remote source and remote destination folder/files. How can this be implemented ?? I have used SCP for file transfer but haven't used for folder/sub-folder.
You can use the flag -r to copy files recursively with scp.
scp -r /usr/share/hub-bucket/GameImages/ user#remotehost:/usr/share/hub-bucket/GameImages/
A better and often faster option is to use rsync, which is usually more efficient since it only transfers files that differ between the two hosts.
if you use scp you can use -r option, like this
scp -r /usr/share/hub-bucket/GameImages/ user#remote-host:/usr/share/hub-bucket/GameImages/
you can also use rsync command
rsync -avz /usr/share/hub-bucket/GameImages/ user#remote-host:/usr/share/hub-bucket/GameImages/
I log into server_a and run .sh file, which has the following script:
scp user#server_b:/my_folder/my_file.xml user#server_b:/my_new_folder/
to copy files from my_folder to my_new_folder at server_b. It doesn't throw an error, but no files are copied.
Notes:
server_b is accessed by the pre-set rsa_keys.
server_a: unix
server_b: ubuntu
can SCP files from/to these servers without any issues
The end goal is to move or copy/remove files.
There are two possibilities:
Connect from server_a to server_b and do local copy:
ssh user#server_b "cp /my_folder/my_file.xml /my_new_folder/"
Do copy over the server_a. Your method would require the server_b to be able to authenticate to itself, which is probably not the case:
scp -3 user#server_b:/my_folder/my_file.xml user#server_b:/my_new_folder/
Also note that your code copies only one file and not files as you write in the title.
If you are logged on to the server, why are you authenticating again:
scp user#server_b:/my_folder/my_file.xml user#server_b:/my_new_folder/
You should be in the directory of file or simply use scp and use -v parameter to see the debug information.
Run as follows:
scp -v /my_folder/my_file.xml user#server_b:/my_new_folder/
It is not a directory nor it is recursive, so you do not need to -r parameter.
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/
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
I am trying to find a way to create and update a tar archive of files on a remote system where we don't have write permissions (the remote file system is read only) over ssh. I've figured out that the way to create a archive is,
ssh user#remoteServer "tar cvpjf - /" > backup.tgz
However, I would like to know if there is some way of performing only incremental backups from this point on (of only files that have actually changed?). Any help with this is much appreciated.
You can try using the --listed-incremental option of tar:
http://www.gnu.org/software/tar/manual/html_node/Incremental-Dumps.html
The main problem is that you have no option to pipe the snar file through the stdout because you are already piping backup.tgz so the best option to store it would be to create the file in the /tmp directory where you should have write permissions and then download it at the end of the backup session.
For example:
ssh user#remoteServer "tar --listed-incremental=/tmp/backup-1.snar -cvpjf - /" > backup-1.tgz
scp user#remoteServer:/tmp/backup-1.snar
And in the following session you will use that .snar file to avoid copying the same files:
scp backup-1.snar user#remoteServer:/tmp/backup-1.snar
ssh user#remoteServer "tar --listed-incremental=/tmp/backup-1.snar -cvpjf - /" > backup-2.tgz