# Exclude Files and Directories from rsync

> URL: https://www.atlantic.net/vps-hosting/exclude-files-and-directories-from-rsync/ | Published: 2024-09-14 | Updated: 2024-10-03 | Author: Hitesh Jethva

**rsync** is a flexible tool for syncing files and directories between different locations. It’s highly efficient because it only transfers the differences between source and destination, saving time and bandwidth. It is commonly used for backup, file transfers, and mirroring. **rsync** supports various options, including the exclusion of specific files and directories during the sync process.

In many scenarios, you may not want to sync every file or directory. For example, temporary files, logs, or large data files might not be necessary for the target system. This is where the**—-exclude** and**—****-exclude-from** options come into play.

In this guide, we’ll explore how to use these options to exclude files and directories from rsync with practical examples.

## Exclude a Single File

The **–exclude** option allows you to specify files and directories that should not be copied during the sync operation. Let’s look at some basic examples of how to use it.

To exclude a single file from the **sync,** use the **–exclude** option followed by the file name or path relative to the source directory:

```bash
rsync -av --exclude 'file.txt' /source/ /destination/
```

Output:

```bash
sending incremental file list
./
file1.txt
file2.txt
file.txt

sent 90 bytes  received 45 bytes  270.00 bytes/sec
total size is 200  speedup is 1.33
```

**Here is the explanation:**

- **-av:** Archive mode and verbose output.
- **–exclude ‘file.txt’:** Excludes the file file.txt from the sync.
- **/source/ /destination/:** The source and destination directories.

In this example, **file.txt** is excluded, but other files (file1.txt, file2.txt) are copied.

## Exclude a Directory

If you want to exclude an entire directory, use the directory name in the **–exclude** option:

```bash
rsync -av --exclude 'dir1/' /source/ /destination/
```

Output:

```bash
sending incremental file list
./
dir2/
file1.txt
file2.txt

sent 140 bytes  received 60 bytes  400.00 bytes/sec
total size is 300  speedup is 1.67
```

Here, **dir1/** is excluded from the sync, and only the remaining files and directories are synced.

## Exclude Files with Specific Patterns

You can also exclude files based on patterns, such as excluding all **.log** files:

```bash
rsync -av --exclude '*.log' /source/ /destination/
```

Output:

```bash
sending incremental file list
./
file1.txt
file2.txt
```

In this case, all **.log** files are excluded, and only files that don’t match the pattern are synced.

## Using the –exclude-from Option

The **–exclude-from** option allows you to specify a file containing a list of exclusions. This file can include multiple lines, each representing a file or directory to exclude.

First, create an exclusion file with the items you want to exclude:

```bash
echo 'file.txt' > exclude-list.txt
echo 'dir1/' >> exclude-list.txt
echo '*.log' >> exclude-list.txt
```

The **exclude-list.txt** file now contains the following:

```bash
file.txt
dir1/
*.log
```

Now, use the **–exclude-from** option to exclude all the patterns in the file:

```bash
rsync -av --exclude-from='exclude-list.txt' /source/ /destination/
```

In this example, all the exclusions from **exclude-list.txt** are applied, and the specified files, directories, and patterns are not synced.

## Conclusion

In this guide, we’ve covered the basics of excluding specific files, directories, and patterns in rsync, along with practical examples of each use case. Excluding files and directories in rsync allows you to fine-tune your sync operations by omitting unnecessary or unwanted files. Try it out by syncing a backup directory on [VPS hosting](https://www.atlantic.net/dedicated-server-hosting/) from Atlantic.Net!
