# The Linux Rsync Command: Usage and Practical Examples

> In this guide, we show you how to will transfer files with the Linux Rsync command, a very flexible tool for your file syncing needs

- Author: [CJ Saathoff](https://www.namehero.com/blog/author/cj-saathoff/)
- Published: 2024-06-04
- Updated: 2024-06-04
- Category: [Linux Command Line](https://www.namehero.com/blog/category/linux-command-line/)
- Web page: https://www.namehero.com/blog/the-linux-rsync-command-usage-and-practical-examples/

[![](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/04/22091024/rsync-1024x576.jpg)](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/04/22091024/rsync.jpg)

Unix Like systems including Linux are composed of many files and folders. So it isn’t surprising that there are many times when synchronizing files and directories is needed. This is where the rsync command in Linux shines as it’s used to transfer data between two locations. It supports transferring files between local and remote directories. The rsync command is my go-to command line tool for transferring many files.

In this blog post, we will transfer files with this extremely flexible tool, having just the right options for your file sync needs.

## Installing Rsync

On most Linux Distributions the package is rsync so for example with dnf and apt the install command would be.

```

dnf install rsync

```

```

apt install rsync

```

## Basic Syntax

The basic syntax for rsync is as follows:

```

rsync [options] source destination

```

If local it will be the path of the file or directory you want to sync.

If transferring from or to a remote location this will be in the format user@host:/path/to/destination.

## Flags And Options

The options for rsync are vast, after going through them here are the ones I believe everyone should know.

>

**–bwlimit=RATE** allows you to set a bandwidth limit on transfers

**–human-readable**, **-h** output numbers in a human-readable format

**–progress** show progress during the transfer

**–max-size=SIZE** the maximum size of a file you want to transfer

**–min-size=SIZE** the minimum size of a file you want to transfer

**–remove-source-files** sender removes synchronized files (non-dir)

**–dry-run**, **-n** perform a trial run with no changes made

**–compress**, **-z** compress data during the transfer

**–perms**, **-p** preserve source permissions

**–quiet**, **-q** suppress non-error messages

**–checksum**, **-c** skip based on checksum, not mod-time & size

**–archive**, **-a** archive mode is equivalent of -rlptgoD

**–recursive**, **-r** recurse into directories

**–update**, **-u** skip files that are newer on the destination

**–ignore-existing** skip updating files that exist on the destination

**–verbose**, **-v** increases the verbosity of the output

**–stats** when the command finishes transfer statistics will be displayed

**–delete** delete extraneous files from the destination

**–chmod=CHMOD** allows for setting permissions during the transfer

**–chown=USER:GROUP** allows for setting ownership during the transfer

If you don’t see a particular option in this section review the man page using the bellow command or refer to an [online](https://man.archlinux.org/man/rsync.1) version.

```

man rsync

```

## Examples

In the below examples, the flags discussed are shown in practical examples. All the examples include multiple flags, most commonly the v (verbose output) and h (human-readable format) only affect the output and I recommend using them.

### Local To Local

```

rsync -ahv /source/path/ /destination/path

```

If you need to copy files and directories from one place to another locally use this command.

### Setting Ownership And Permissions

```

rsync -vh --chown=user:group --chmod=755 /source/path/ /destination/path

```

Instead of using the archive flag, we set the desired ownership and file permissions manually.

### Do A Dry Run

```

rsync -avh --dry-run /source/path/ /destination/path

```

In this example, nothing is being transferred but the output can be used to confirm the command is structed correctly.

### Local To Remote

```

rsync -avhz /source/path/ username@198.51.100.54:/destination/path

```

Transfers from local to remote machine using compression.

### Local To Remote With Bandwidth Limit

```

rsync -avhz --bwlimit=100M /source/path/ username@198.51.100.54:/destination/path

```

Transfers from remote to local using compression and a bandwidth limit.

### Remote To Local

```

rsync -avhz username@198.51.100.54:/source/path/ /destination/path

```

Remote sync from remote to local using compression.

### Remote To Local Using Custom Port

```

rsync -avhz -e 'ssh -p &lt;port-number>' user@198.51.100.54:/source/path/ /destination/path

```

Transfers from remote to local using compression over a non-standard ssh port.

### Show Stats After Transfer

```

rsync --stats -avh /path/to/source/ /path/to/destination/

```

Update files that are newer and show transfer stats

### Sync Files Smaller Than 1GB And Larger Than 1MB

```

rsync --max-size=1G --min-size=1M -avh /path/to/source/ /path/to/backup/

```

This example sets a maximum and minimum file size for the files to transfer.

### Update Files And Directories

```

rsync -avhu /path/to/source/ /path/to/destination/

```

Files that are newer in the source will overwrite the older versions, in addition, files that don’t exist on the destination will be copied over.

### Ignore Existing Files

```

rsync --ignore-existing -avh /path/to/source/ /path/to/destination/

```

All the files that already exist are skipped and any missing files from the source are copied over.

### Delete Files After Transfer

```

rsync -ahv --remove-source-files /source/path/ /destination/path

```

In this example, the command removes only the files from the source after transfer.

## Tips And Tricks

- Always do a dry run using the verbose flag first to confirm everything works as expected.
- If transferring between a remote system consider using a bandwidth limit.
- If transferring between a remote system consider using compression.
- If using a delete or remove be certain you understand the flag before using.
- Unless you know the permission needs to be different use archive mode to preserve them.
- If you need to filter files consider using rsync together with the find command.

  - Although rsync does have filtering capabilities my recommendation is to couple it with the find command like the example below:`find source -type f -exec rsync -avh --progress {} destination ;`

In this example, the find command is being used to filter out only the files and transfer them

## Conclusion

In conclusion, we started with installing the rsync command and touched on syntax and options. From there, I have included many different examples using the options which I feel everyone should know. Whether you need remote syncor just need to sync directories on your local machine rsync has got you covered.

## Additional Links

Done reading and want additional resources, why not check out these?

- [Other Command Posts](https://www.namehero.com/blog/?s=command)
- [Rsync Offical Site](https://rsync.samba.org/)
- [Rsync Wikipedia Page](https://en.wikipedia.org/wiki/Rsync)[](https://rsync.samba.org/)

## Related posts

- [How to Use the Tee Command in Linux](https://www.namehero.com/blog/how-to-use-the-tee-command-in-linux/)
- [How to Use the xargs Command in Linux](https://www.namehero.com/blog/how-to-use-the-xargs-command-in-linux/)
- [Understanding The Mv Command In Linux](https://www.namehero.com/blog/understanding-the-mv-command-in-linux/)
- [How to Use the xxd Command in Linux](https://www.namehero.com/blog/how-to-use-the-xxd-command-in-linux/)
