• Recent Articles

    9 Practical Examples of Rsync Command in Linux


    Rsync (Remote Sync) is a powerful command-line tool used in Linux systems to synchronize and transfer files between local and remote directories. It is widely used for backup, mirroring, and incremental file transfer purposes. Rsync efficiently copies only the differences between the source and destination files, resulting in faster and more efficient file synchronization. In this blog, we will explore ten practical examples of using the Rsync command in Linux.

    1. Basic File Transfer:

    The simplest use of Rsync is to transfer files from a source directory to a destination directory. The basic syntax is:

    rsync source_directory/ destination_directory/

    This command copies all the files from the source directory to the destination directory.

    • -v : verbose
    • -r : copies data recursively (but don’t preserve timestamps and permission while transferring data.
    • -a : archive mode, which allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships, and timestamps.
    • -z : compress file data.
    • -h : human-readable, output numbers in a human-readable format.

    2. Remote File Transfer:

    Rsync can be used to transfer files between local and remote systems over SSH. To copy files from a local system to a remote system, use the following syntax:

    rsync -avz source_directory/ user@remote_host:destination_directory/

    The `-avz` options enable archive mode, preserve permissions and ownership, and compress the data during transfer.

    3. Remote File Transfer with SSH Key:

    If you want to use SSH key-based authentication for remote file transfer, you can specify the path to your private key using the `-e` option:

    rsync -avz -e 'ssh -i /path/to/private_key' source_directory/ user@remote_host:destination_directory/

    Replace `/path/to/private_key` with the actual path to your private key file.

    4. Exclude Files and Directories:

    Rsync allows you to exclude specific files and directories during synchronization. Use the `--exclude` option followed by the pattern of the files or directories you want to exclude. For example:

    rsync -avz --exclude 'file.txt' source_directory/ destination_directory/

    This command excludes the file named "file.txt" from the synchronization process.

    5. Synchronize Two Directories:

    To synchronize two directories, making sure that the destination directory matches the source directory, use the `--delete` option:

    rsync -avz --delete source_directory/ destination_directory/

    This command ensures that any extra files or directories in the destination directory are removed during synchronization.

    6. Preserve File Permissions and Ownership:

    When synchronizing files, it is often necessary to preserve file permissions and ownership. Rsync allows you to achieve this using the `-a` option, which stands for archive mode:

    rsync -avz source_directory/ destination_directory/

    The archive mode preserves permissions, ownership, timestamps, and recursively copies directories.

    7. Show Progress during Transfer:

    Rsync provides a progress bar to track the transfer progress. Use the `--progress` option to display detailed information about the files being transferred:

    rsync -avz --progress source_directory/ destination_directory/

    This command shows the progress, speed, and estimated time remaining for each file being transferred.

    8. Perform Dry Run:

    Before executing an actual file transfer, it's helpful to perform a dry run to see what files will be affected. Use the `--dry-run` option:

    rsync -avz --dry-run source_directory/ destination_directory/

    This command displays a list of files that would be transferred without actually performing the transfer.

    9. Bandwidth Limitation:

    If you want to limit the bandwidth used by Rsync during file transfer, you can specify it using the `--bwlimit` option. For example, to limit the transfer speed to 1MB/s:

    rsync -avz --bwlimit=1000 source_directory/ destination_directory/

    Thanks for reading. Please share

    No comments