Can scp copy directories recursively?



Currently I can only copy a single .tar file. But how can I copy directories recursively with scp?


Posted 2011-04-29T04:24:45.713

Reputation: 2 903



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


Posted 2011-04-29T04:24:45.713

Reputation: 19 239

10Note 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 – 2017-02-03T14:49:01.837

For some reason /path appears to be relative to the users $HOME -- how do I make it absolute without being root@? – Jonathan – 2017-08-25T21:50:22.290

Yeah, SCP like never puts the files where I want. Often when trying to replace a directory, I somehow end up with another directory inside it with the same name instead. I'm extra careful with this. – sudo – 2018-01-24T03:00:46.590

8However bear in mind that this won't preserve symlinks. – CpnCrunch – 2015-07-24T00:30:52.323


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.

Phil Hollenback

Posted 2011-04-29T04:24:45.713

Reputation: 12 499

Using the sugestions above: scp -rp sourcedirectory user@dest:/path, I do this and works fine to me: scp -rp /mypath/ myuser@ – Rafael Martins – 2017-06-26T16:11:14.767

1I agree rsync is more efficient. One thing it doesn't currently do that scp does is allow copying between remote hosts (at least without running the rsync client on one of them). – Cedric Knight – 2017-08-21T08:54:08.033

See @mick's answer for the two flags a and v. – user3123159 – 2017-12-19T08:46:19.747

13"no data will be transferred" except, of course, the data required to determine exactly what has or has not changed. – thsutton – 2011-04-29T05:15:22.103

14Ha yeah I glossed that over a bit obviously. We're not talking quantum entanglement here. – Phil Hollenback – 2011-04-29T06:01:31.053

2it's so sad that osx, win7 and not even ubuntu uses something like rsync for their GUI yet. – cregox – 2011-04-29T10:24:47.673

While rsync is certainly a nice command and useful utility all around, you didn't answer his question. – dmourati – 2013-01-09T16:03:07.330

11dmourati I did answer his question. I told him a better way to do it. – Phil Hollenback – 2013-01-29T21:23:34.377

1Both systems require rsync. – Simon Hartcher – 2013-09-27T11:22:56.253

1@PhilHollenback sorry to revive an old thread, but what if I delete files from the local directory? Would it also delete files from the remote directory? – raphnguyen – 2015-10-25T06:33:50.167

2@raphnguyen: no, deleting files from the local directory will not delete them from the remote directory. You can turn this behavior on with the --delete rsync option. – Phil Hollenback – 2015-10-26T20:19:03.087

2It's better to use rsync -av /local/dir/ server:/remote/dir/ because it's idempotent: It will create server:/remote/dir if it's missing, and it won't create server:/remote/dir/dir if server:/remote/dir is already present. – mc0e – 2016-05-01T18:16:26.870


That is what the -r option is for. :)

See the scp man page for more info if needed.


Posted 2011-04-29T04:24:45.713

Reputation: 413


Recursive Copy Option '-r' (lower case)

scp -r

Which I confuse with the regular local recursive copy option '-R' (upper case)

cp -R


Posted 2011-04-29T04:24:45.713

Reputation: 191


I just wanted to point out the difference between cp and scp as -r and -R are not the same.

– Tarun – 2013-09-23T14:54:19.940


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


Posted 2011-04-29T04:24:45.713

Reputation: 560

All versions of rsync that I have used would use ssh by default, so -essh is unlikely to be needed. And the choice of command used to connect to the remote host is really unrelated to copying recursively. – kasperd – 2015-11-05T19:03:58.137


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


Posted 2011-04-29T04:24:45.713

Reputation: 143

the file extension should be either .tar.gz or .tgz since the file is a gzipped tar archive (since the -z flag is used). – anthonybell – 2018-03-26T22:07:04.373


You can recursively copy a directory into a compressed archive with this simple command:

ssh -p 22  'cd /parent/directory && tar zcvf - directory_to_copy' > /destination/on/your/machine/archive_name.tgz

For example, to copy contents of /var/log from to ~/logs.tgz you run:

ssh -p 22  '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 to ~/destination/log on your system:

ssh -p 22  'cd /var && tar zcvf - log' | tar xzf - -C ~/destination

Though to mirror a directory, you probably should use rsync...


Posted 2011-04-29T04:24:45.713

Reputation: 2 526


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).


sshpass -p 'remote_password' scp -rp /src/folder myremoteusername@

Franck Dernoncourt

Posted 2011-04-29T04:24:45.713

Reputation: 382


Another (likely better for repeated use) option is to use NFS - check out nfs-kernel-server and how to setup NFS shares.

Andrei Pokrovsky

Posted 2011-04-29T04:24:45.713

Reputation: 99

2This is complete nonsense. There are endless cases where you would use scp but NFS is not an option. – Sven – 2016-08-11T23:24:42.630