Scp to copy directory in linux

Remote Linux server to remote linux server dir copy. How? [closed]

What is the best way to copy a directory (with sub-dirs and files) from one remote Linux server to another remote Linux server? I have connected to both using SSH client (like Putty). I have root access to both.

If you has access of the ftp of the remote server, we can also use wget to download like $wget -r —level=9 —no-parent —reject «index.html*» ftp://:@/path/to Reference1 Reference2

16 Answers 16

There are two ways I usually do this, both use ssh:

scp -r sourcedir/ user@dest.com:/dest/dir/ 

or, the more robust and faster (in terms of transfer speed) method:

rsync -auv -e ssh --progress sourcedir/ user@dest.com:/dest/dir/ 

Read the man pages for each command if you want more details about how they work.

add -L to follow symlinks and copy them as regular directories. This link has plenty of useful examples: tecmint.com/rsync-local-remote-file-synchronization-commands

I would modify a previously suggested reply:

rsync -avlzp /path/to/sfolder name@remote.server:/path/to/remote/dfolder 

-a (for archive) implies -rlptgoD so the l and p above are superfluous. I also like to include -H, which copies hard links. It is not part of -a by default because it’s expensive. So now we have this:

rsync -aHvz /path/to/sfolder name@remote.server:/path/to/remote/dfolder 

You also have to be careful about trailing slashes. You probably want

rsync -aHvz /path/to/sfolder/ name@remote.server:/path/to/remote/dfolder 

if the desire is for the contents of the source «sfolder» to appear in the destination «dfolder». Without the trailing slash, an «sfolder» subdirectory would be created in the destination «dfolder».

This answer deserves more upvotes and should be the accepted one. Only thing to add were —progress if you’re fond of it.

rsync -avlzp /path/to/folder name@remote.server:/path/to/remote/folder

$ scp -r /path/to/top/directory user@server:/path/to/copy

Use rsync so that you can continue if the connection gets broken. And if something changes you can copy them much faster too!

Rsync works with SSH so your copy operation is secure.

I used rdiffbackup http://www.nongnu.org/rdiff-backup/index.html because it does all you need without any fancy options. It’s based on the rsync algorithm. If you only need to copy one time, you can later remove the rdiff-backup-data directory on the destination host.

rdiff-backup user1@host1::/source-dir user2@host2::/dest-dir 

rdiff-backup also preserves subdirectories, hard links, dev files, permissions, uid/gid ownership, modification times, extended attributes, acls, and resource forks.

which is an bonus to the scp -p proposals as the -p option does not preserve all (e.g. rights on directories are set badly)

sudo apt-get install rdiff-backup 

Check out scp or rsync, man scp man rsync

scp file1 file2 dir3 user@remotehost:path 

Well, quick answer would to take a look at the ‘scp’ manpage, or perhaps rsync — depending exactly on what you need to copy. If you had to, you could even do tar-over-ssh:

tar cvf - | ssh server tar xf - 
rsync -azvu -e ssh user@host1:/directory/ user@host2:/directory2/ 

(and I assume you are on host0 and you want to copy from host1 to host2 directly)

Читайте также:  How to install minecraft server on linux

If the above does not work, you could try:

ssh user@host1 "/usr/bin/rsync -azvu -e ssh /directory/ user@host2:/directory2/" 

in the this, it would work, if you already have setup passwordless SSH login from host1 to host2

Rsync does not work this way. If you try it you get the error message «The source and destination cannot both be remote.»

scp will do the job, but there is one wrinkle: the connection to the second remote destination will use the configuration on the first remote destination, so if you use .ssh/config on the local environment, and you expect rsa and dsa keys to work, you have to forward your agent to the first remote host.

If you already some of the content on $host consider using rsync with ssh as a tunnel.

If you are serious about wanting an exact copy, you probably also want to use the -p switch to scp, if you’re using that. I’ve found that scp reads from devices, and I’ve had problems with cpio, so I personally always use tar, like this:

cd /origin; find . -xdev -depth -not -path ./lost+found -print0 \ | tar --create --atime-preserve=system --null --files-from=- --format=posix \ --no-recursion --sparse | ssh targethost 'cd /target; tar --extract \ --overwrite --preserve-permissions --sparse' 

I keep this incantation around in a file with various other means of copying files around. This one is for copying over SSH; the other ones are for copying to a compressed archive, for copying within the same computer, and for copying over an unencrypted TCP socket when SSH is too slow.

Источник

How Do I Use SCP to Transfer a Directory in Linux?

This tutorial explains how to easily transfer directories using the Linux scp (Secure Copy Protocol) command.

Despite the Linux scp command being deprecated and replaced by SFTP and RSYNC, its use is widely adopted. Even after being deprecated and replaced, probably SCP is the most common method to transfer files. Yet, its use isn’t professionally recommended. That’s why after the scp instructions to download and upload directories, I added instructions to do the same using the sftp command.

Читайте также:  Linux top sort by cpu

Download and Upload Directories Using scp

Uploading or fetching directories with scp is similar to uploading or downloading regular files. The only difference is the -r flag you need to add for directories to be transferred recursively.

The first example of this tutorial has the following characteristics you need to replace:

  • The username used to log in on the remote device is kali.
  • The remote IP address is 168.1.100.
  • The directory to download is named linuxhintdir.

You will need to replace the username, IP address, and directory names according to your scenario.

The example below invokes the scp command with the -r (Recursive) flag to specify we want to download a directory and not a regular file. The scp command and the -r flag are followed by the username@IP/Host. Then, it is followed by a colon and the path to the directory you want to download. Whatever you want to upload or download a directory, the location where you want to save the directory or regular file is always specified at the end of the command. In this case, the linuxhintdir directory will be stored in the /root directory.

Note: Files and directories used in this tutorial are empty.

As you can see, the file was transferred correctly.

Uploading a directory using SCP requires a similar syntax. The difference, as I said previously, is you need to type the path where to save the directory at the end of the command.

This is because the scp command was designed to be similar as possible to the cp Linux command.

In the following example, a directory named localdir located in the current directory is copied to the remote host’s default location, the kali user home directory. By default, if you don’t specify a patch after the colon, the default path for files to be copied is the home directory of the user you authenticated.

After invoking SCP, add the -r flag to specify you are transferring a directory and not a regular file. Then, specify the directory you want to send followed by : as shown in the example below:

As you can see in the following screenshot taken from the remote host, the directory was copied recursively:

Downloading and Uploading Regular Files Using scp

As said previously, downloading and uploading regular files is almost the same as with directories except for the absence of the -r flag, which isn’t necessary.

To upload a file, use the following syntax:

Читайте также:  Linux advanced routing traffic control howto

Therefore, to upload a file named linuxhintfile to the remote default directory, known as the kali user home directory, I run the following command:

To download a file, the syntax is the following:

The following example shows how to download a file named linuxhintfile, to store in the home directory of the remote user named kali, and save it in the local user’s Downloads directory.

As shown, the file was transferred correctly.

Download and Upload Files and Directories Using sftp

Downloading and uploading files and directories using SFTP can be simple as with SCP.

The following method shows how to download a file located in the remote subdirectory named dir. The file will be locally saved in the /tmp/linuxhint2 directory.

Downloading directories is the same process. The example below shows how to download the dir directory to the current location specified with a dot:

Uploading directories isn’t so simple as with SCP and the syntax changes. This is because you need to connect to the SFTP server to and run the put command to upload the file.

In the example below, the file named linuxhintfile is uploaded to the dir subdirectory on the remote host:

Uploading directories also requires implementing the -r flag for recursive transference. However, when uploading, the -r flag must be placed after the put command, as shown in the example below in which the directory linuxhintdir is uploaded to the remote subdirectory named dir.

As you can see, the directory was uploaded successfully. That’s how you can download and upload files and directories in Linux using scp commands or the more recommended sftp commands.

Conclusion

As you can see, copying directories in Linux remotely using the scp command is pretty simple. The scp command is considered the most user-friendly method to transfer files between devices, and it was once considered secure as its name, Secure Copy Protocol, indicates. Currently, the scp command is obsolete due to vulnerabilities and must not replace safer alternatives like SFTP and RSYNC. The sftp command can also be used in interactive mode, which wasn’t explained in this tutorial, but you can read it at https://linuxhint.com/sftp_linux_command_line/. All systems supporting SCP should support SFTP, so availability should not be a problem. In future tutorials, the RSYNC alternative will be explained, so keep following this blog for additional Linux tips and tutorials.

Thank you for reading this SCP tutorial, I hope it was useful to you.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.

Источник

Оцените статью
Adblock
detector