Skip to main content

Sync Files | Basic rsync Command

To sync one directory to another in your local Linux system, you can use the rsync command. rsync is a powerful and efficient tool for copying and synchronizing files and directories. Here's a simple guide to do that:

  1. Open your terminal.
  2. Run the rsync command with the appropriate options.

Basic rsync Command

The basic syntax for rsync is:

rsync -avh --delete /path/to/source/ /path/to/destination/
  • -a: Archive mode. This option preserves the permissions, timestamps, symbolic links, etc.
  • -v: Verbose mode. This option shows the progress during the transfer.
  • -h: Human-readable format. This option makes the output more readable.
  • --delete: This option deletes files in the destination directory that are not in the source directory.

Example

Suppose you want to sync the contents of /home/user/source_directory to /home/user/destination_directory. You would run:

rsync -avh --delete /home/user/source_directory/ /home/user/destination_directory/

Additional Options

  • Dry Run: To see what changes will be made without actually making any changes, you can use the --dry-run option.

    rsync -avh --delete --dry-run /path/to/source/ /path/to/destination/
  • Progress: To show the progress of each file transfer, you can add the --progress option.

    rsync -avh --delete --progress /path/to/source/ /path/to/destination/

Scheduling the Sync

If you want to schedule this sync to run at regular intervals, you can use cron.

  1. Edit the cron table using crontab -e.
  2. Add a cron job to run rsync at your desired interval. For example, to run it every day at 2 AM, you would add:
0 2 * * * rsync -avh --delete /path/to/source/ /path/to/destination/

This will ensure that the directories are kept in sync automatically.