You are here: Sysadmins of the North » GNU Linux » Recursive scp and symlinks

Recursive scp and symlinks

Recursive scp without following symbolic links. TL;DR: when using recursive scp, symbolic links aren’t preserved and are copied as if they are normal directories. So you have to look for another solution to recursively transfer symlinks over ssh. Here is one: Tar over ssh!

Tar over ssh

Imagine the situation where you have to recursive transfer files and directories from station A to station B. While using recursive scp (scp -r), you notice all symbolic links are transferred as normal directories, multiplying the amount of data. When I was in this situation, and googled for a solution, I found this blog post: recursive scp and symlinks.

Unfortunately, using rsync was not an option for me, because:

  1. rsync wasn’t available on the system
  2. the broken file system was mounted read only (RO), thus making it impossible to yum install rsync :-(

Luckily I noticed the comment made by “John Mitchell”:

Didn’t have rsync installed so I used tar over ssh

Hé, that sounds like my situation :-) He suggests using tar with ssh instead of recursive scp to transfer symlinks:

# On the source server:
# cd /path/to/content
tar cf - * | ssh user@destination '(cd /path/to/content; tar xf - )'Code language: Bash (bash)

The files are compressed and piped into the ssh stream, and uncompressed to /path/to/content on the destination server.

Use the tar -p parameter if you want to preserve file permissions:

tar cfp - * | ssh user@destination '(cd /path/to/content; tar xfp - )'Code language: Bash (bash)

The blog posts Using tar and ssh to efficiently copy files preserving permissions on meinit.nl and Howto: Use tar Command Through Network Over SSH Session on nixCraft provide more nifty tar over ssh options.

Excluding hidden files / folders in tar

You can exclude hidden files and folders in tar by using --exclude=".*" as first parameter. For example:

tar --exclude=".*" -czvf downloads.tgz Downloads/
Downloads/
Downloads/ISO

tar -czvf downloads.tgz Downloads/
Downloads/
Downloads/.secret
Downloads/ISO
Downloads/.hidden1
Code language: Bash (bash)

Want to manually install OpenSSH in Windows Server?

Show Your Support

donate with Paypal

If you want to step in to help me cover the costs for running this website, that would be awesome. Just use this link to donate a cup of coffee ☕($10 USD or €10 EUR for example). And please share the love and help others make use of this website. Thank you very much! <3 ❤️

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top