DEV Community

Zaki Arrozi Arsyad
Zaki Arrozi Arsyad

Posted on • Updated on

linux : rsync vs scp

rsync is a command for move and synchronizing files. This command help us to manage files or directories effectively when backing up data on a regular basis. rsync compares any differences and only transfer those differences.

scp is used for copy files and directories in secure way. Basically, It is a plain linear copy. scp reads the source file and writes it to the destination.

rsync

Basic usage :

$ rsync OPTION SOURCE DESTINATION
Enter fullscreen mode Exit fullscreen mode

We need to check if we have rsync, because some machines might not have this command.

  • Not passing the OPTION

    It will sync between two directories, but only files in the main directory, not include files in the subdirectories.

# Sync all files in original directory to duplicate directory
$ rsync SOURCE/* DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • -r or -recursive

    Sync all files in all the main directory and subdirectories

# Sync all files in original directory and its subdirectories
$ rsync -r SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • -a or -archive

    This option allow us to copy almost everything, including file permissions, user & group ownership and timestamps.

  • -v or -verbose

    It gives us a summary about the data transferred.

  • -h or --human-readable format

    We will get the result in human readable format.

  • -z or -compress

    Compress file data during the transfer

  • -e

    To specify the type of protocol we use. We can choose using ssh or rsh

# Sync using ssh protocol
$ rsync -avhze ssh SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • --progress

    To show the progress while transferring the data

# Show the progress during transfer
$ rsync -avhze ssh --progress SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • --update

    To updated more recently on the local filesystem. Files that don't exist are copied. Files that already exist, but have a newer timestamp are also copied.

# Update only if there is a newer version
$ rsync -avhze ssh --progress --update SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • --remove-source-files

    To remove files that successfully transferred.

# Sync using ssh protocol
$ rsync -avhze ssh --remove-source-files SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • --delete

    Delete files that have been deleted in the original directory

# Update duplicate directory if there are files deleted in original directory
$ rsync -avhze ssh --delete SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • --include

    This option will include files that we specify in the parameter

# Include files that matched with include paramaeter
$ rsync -avhze ssh --include 'KEYWORD' SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • --exclude

    This option will exclude files that we specify in the parameter

# Exclude files that matched with exclude paramaeter
$ rsync -avhze ssh --exclude 'KEYWORD' SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • --chown

    Change file permission in the destination directory

# Copied files permisson are changed form user to group
$ rsync -avhe ssh --chown=USER:GROUP SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • --dry-run

    This option perform a trial run and will not make any changes, but gives us the same result as a real run. If the results are as expected, then we can remove the --dry-run

# Perfom dry run
$ rsync --dry-run -avhze ssh --delete SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • --max-size

    Set the maximum files to transfer. This option will only transfer files that are equal or smaller than the max size.

# Set max size 200k
$ rsync -avhze ssh --max-size='200k' SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • --bwlimit

    We can set the bandwidth limit when we sync data from a machine to another machine.

# Set bandwidth rate 5000KBytes per second
$ rsync -avhze ssh --bwlimit=5000 SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode

scp

Basic usage :

$ scp OPTION SOURCE DESTINATION
Enter fullscreen mode Exit fullscreen mode
  • Copy a single file
# Copy a single file
$ scp SOURCE_FILE.txt DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • Copy multiple files
# Copy a multiple file
$ scp FILE1.txt FILE2.txt FILE3.txt DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • Copy .pem file
# Copy aws key to vm 
$ scp -i file.pem file.pem DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • -r to copy a directory recursively
# Copy all files in a directory
$ scp -r SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • -P to specify the port

    By default, scp will use port 22, but we can use another port for security reason

# Use port 2249
$ scp -P 2249 SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • -v for verbose

    To show detailed information of the scp process

# Use port 2249
$ scp -v SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • -l limit the bandwidth usage
# Set the bandwidth limit
$ scp -l 400 SOURCE/ DESTINATION/
Enter fullscreen mode Exit fullscreen mode
  • -q for quite mode

    Run the command in quite mode, and only display critical errors

  • -p for preserves modification times, access times, and modes from the original file

  • -u to delete source files after the transfer is successful


Both of the commands are similar, lets choose what we use based on our case

Top comments (1)

Collapse
 
bam92 profile image
Abel Lifaefi Mbula

The two commands are not similar at all, except that they all do copy from A to B. scp is limited:

  • not so many options compared to rsync
  • not optimized
  • can't resume if copy fails
  • etc.

The only thing that scp has over rsync is that it's secure by default. But if you're concerned about security (I think everyone should be), you can use rsync --rsh=ssh to secure rsync.

You can learn more here