SnapOverflow

SnapOverflow Logo SnapOverflow Logo

SnapOverflow Navigation

  • Home
  • Server
  • Ubuntu

Mobile menu

Close
  • Home
  • System Administrators
    • Hot Questions
    • New Questions
    • Tags
  • Ubuntu
    • Hot Questions
    • New Questions
    • Tags
  • Help
Home / server / Questions / 264595
Accepted
kernel
kernel
Asked: 2011-04-29 20:24:45 +0800 CST2011-04-29 20:24:45 +0800 CST 2011-04-29 20:24:45 +0800 CST

Can scp copy directories recursively?

  • 772

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

linux scp
  • 9 9 Answers
  • 992982 Views

9 Answers

  • Voted
  1. Best Answer
    dmourati
    2011-04-29T20:28:41+08:002011-04-29T20:28:41+08:00

    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

    • 1212
  2. Phil Hollenback
    2011-04-29T21:11:23+08:002011-04-29T21:11:23+08:00

    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.

    • 208
  3. HedgeMage
    2011-04-29T20:27:57+08:002011-04-29T20:27:57+08:00

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

    See the scp man page for more info if needed.

    • 38
  4. Tarun
    2013-09-12T09:53:51+08:002013-09-12T09:53:51+08:00

    Recursive Copy Option '-r' (lower case)

    scp -r
    

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

    cp -R
    
    • 20
  5. mick
    2015-11-06T10:33:20+08:002015-11-06T10:33:20+08:00

    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
    • 14
  6. user9869932
    2015-06-07T10:00:04+08:002015-06-07T10:00:04+08:00

    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

    • 8
  7. Anubioz
    2016-01-06T23:20:21+08:002016-01-06T23:20:21+08:00

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

    • 4
  8. Franck Dernoncourt
    2016-01-06T22:49:03+08:002016-01-06T22:49:03+08:00

    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
    
    • 3
  9. atthik
    2019-04-23T06:00:06+08:002019-04-23T06:00:06+08:00

    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.

    • 2

Sidebar

Stats

  • Questions 681965
  • Answers 980273
  • Best Answers 280204
  • Users 287326
  • Popular
  • Answers
  • Marko Smith

    Ping a Specific Port

    • 18 Answers
  • Marko Smith

    Check if port is open or closed on a Linux server?

    • 7 Answers
  • Marko Smith

    How to automate SSH login with password?

    • 10 Answers
  • Marko Smith

    How do I tell Git for Windows where to find my private RSA key?

    • 30 Answers
  • Marko Smith

    What's the default superuser username/password for postgres after a new install?

    • 5 Answers
  • Marko Smith

    What port does SFTP use?

    • 6 Answers
  • Marko Smith

    Resolve host name from IP address

    • 8 Answers
  • Marko Smith

    Command line to list users in a Windows Active Directory group?

    • 9 Answers
  • Marko Smith

    What is a Pem file and how does it differ from other OpenSSL Generated Key File Formats?

    • 3 Answers
  • Marko Smith

    How to determine if a bash variable is empty?

    • 15 Answers
  • Martin Hope
    Davie Ping a Specific Port 2009-10-09 01:57:50 +0800 CST
  • Martin Hope
    Smudge Our security auditor is an idiot. How do I give him the information he wants? 2011-07-23 14:44:34 +0800 CST
  • Martin Hope
    kernel Can scp copy directories recursively? 2011-04-29 20:24:45 +0800 CST
  • Martin Hope
    Robert ssh returns "Bad owner or permissions on ~/.ssh/config" 2011-03-30 10:15:48 +0800 CST
  • Martin Hope
    Eonil How to automate SSH login with password? 2011-03-02 03:07:12 +0800 CST
  • Martin Hope
    gunwin How do I deal with a compromised server? 2011-01-03 13:31:27 +0800 CST
  • Martin Hope
    Tom Feiner How can I sort du -h output by size 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich What is a Pem file and how does it differ from other OpenSSL Generated Key File Formats? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent How to determine if a bash variable is empty? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus How do you find what process is holding a file open in Windows? 2009-05-01 16:47:16 +0800 CST

Related Questions

Trending Tags

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • Home
  • Questions
    • Hot Questions
    • New Questions
  • Tags
  • Help

Footer

SnapOverflow

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Help

© 2022 SOF-TR. All Rights Reserve