Can scp copy directories recursively?

Solution 1:

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

Solution 2:

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.


Solution 3:

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

See the scp man page for more info if needed.


Solution 4:

Recursive Copy Option '-r' (lower case)

scp -r

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

cp -R

Solution 5:

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

Tags:

Linux

Scp