Currently I can only copy a single .tar file. But how can I copy directories recursively with scp?
9 Answers
Yup, use -r:
scp -rp sourcedirectory user@dest:/path - -r means recursive
- -p preserves modification times, access times, and modes from the original file.
Note: This creates the sourcedirectory inside /path thus the files will be in /path/sourcedirectory
- 12However bear in mind that this won't preserve symlinks.CpnCrunch– CpnCrunch2015-07-24 00:30:52 +00:00Commented Jul 24, 2015 at 0:30
- 40Note that
-pr(options in reversed order) won't copy the folders, bur rather their content to the target directory (apparently, the order of options matters).pms– pms2017-02-03 14:49:01 +00:00Commented Feb 3, 2017 at 14:49 - 1If I do a ./ to do the current directory it says Error: unexpected file name. I know I can go to the parent directory and do it that way, or use globing with *. However, I am curious why ./ would not work for the source directory?Andrew S– Andrew S2021-02-09 19:17:26 +00:00Commented Feb 9, 2021 at 19:17
While the previous answers are technically correct, you should also consider using rsync instead. rsync compares the data on the sending and receiving sides with a diff mechanism so it doesn't have to resend data that was already previously sent.
If you are going to copy something to a remote machine more than once, use rsync. Actually, it's good to use rsync every time because it has more controls for things like copying file permissions and ownership and excluding certain files or directories. In general:
$ rsync -av /local/dir/ server:/remote/dir/ will synchronize a local directory with a remote directory. If you run it a second time and the contents of the local directory haven't changed, no data will be transferred - much more efficient than running scp and copying everything every time.
Also, rsync allows you to recover from interrupted transfers very easily, unlike scp.
Finally, modern versions of rsync by default run over ssh, so if scp is already working, rsync should pretty much be a drop-in replacement.
- 1I agree
rsyncis more efficient. One thing it doesn't currently do thatscpdoes is allow copying between remote hosts (at least without running the rsync client on one of them).Cedric Knight– Cedric Knight2017-08-21 08:54:08 +00:00Commented Aug 21, 2017 at 8:54 - 5
-av:v is for verbose, a for archive and is a shortcut to -rlptgoD which implies recursive , preserve rights owner dates and links. If you only want recursive use-rpdem– pdem2019-09-26 12:57:40 +00:00Commented Sep 26, 2019 at 12:57 - 1rsync does not encrpyt traffic, so you need to use extra "-e ssh" to encrypt the traffic like scpPozzo-Balbi– Pozzo-Balbi2020-07-22 23:09:45 +00:00Commented Jul 22, 2020 at 23:09
That is what the -r option is for. :)
See the scp man page for more info if needed.
Recursive Copy Option '-r' (lower case)
scp -r Which I confuse with the regular local recursive copy option '-R' (upper case)
cp -R - 5I just wanted to point out the difference between cp and scp as -r and -R are not the same. unix.stackexchange.com/questions/18712/…Tarun– Tarun2013-09-23 14:54:19 +00:00Commented Sep 23, 2013 at 14:54
The best way is to use rsync over SSH
rsync -a -essh /source/ user@dest-server:/dest/ rsync -a -essh user@source-server:/source/ /dest/ My favorites options are -Pazvessh --delete :
- -a : archive mode (include a lot of default common options, including preserving symlinks)
- -z : compress
- -v : verbose : show files
- -P : show progess as files done/remaining files
- -e ssh : do rsync in ssh protocol
- --delete : delete files in the destination that are not anymore in the source
- All versions of
rsyncthat I have used would usesshby default, so-esshis unlikely to be needed. And the choice of command used to connect to the remote host is really unrelated to copying recursively.kasperd– kasperd2015-11-05 19:03:58 +00:00Commented Nov 5, 2015 at 19:03
After looking for the recursive copy flag, and successfully used it thanks to this post, I would like to post just a suggestion.
If the case is that you are copying (recursively) a directory. Maybe if the files are sent compressed you could save time in the transfer
What I did in the end was:
local$ tar -czvf local.tar.gz directory/ local$ scp local.tar.gz user@remote:/directory ssh user@remote remote$ tar -xzvf local.tar.gz Hope this helps
- the file extension should be either
.tar.gzor.tgzsince the file is a gzipped tar archive (since the-zflag is used).anthonybell– anthonybell2018-03-26 22:07:04 +00:00Commented Mar 26, 2018 at 22:07
You can recursively copy a directory into a compressed archive with this simple command:
ssh -p 22 [email protected] 'cd /parent/directory && tar zcvf - directory_to_copy' > /destination/on/your/machine/archive_name.tgz For example, to copy contents of /var/log from domain.com to ~/logs.tgz you run:
ssh -p 22 [email protected] 'cd /var && tar zcvf - log' > ~/logs.tgz You can also extract files on target system by using pipes. This command will copy contents of /var/log at domain.com to ~/destination/log on your system:
ssh -p 22 [email protected] 'cd /var && tar zcvf - log' | tar xzf - -C ~/destination Though to mirror a directory, you probably should use rsync...
If you prefer to pass the user's password as a parameter rather than inputting it interactively, you can use sshpass (sudo apt-get install -y sshpass).
Example:
sshpass -p 'remote_password' scp -rp /src/folder [email protected]:/dest/folder You can use -r option with scp command to copy directories recursively on any system. If you need anything else refer scp command tutorial. -r option stands for recursive operation in most of the Linux commands.
- 4While true, the
-roption has already been suggested years ago and is the accepted answer.RalfFriedl– RalfFriedl2019-04-22 16:05:04 +00:00Commented Apr 22, 2019 at 16:05