To exclude subfolders when copying files between directories using rsync in Arch Linux, utilize the --exclude flag followed by the directory name. This command filters out entire subtrees while preserving the specified top-level files, ensuring efficient and targeted data synchronization without transferring unnecessary nested content.
Understanding the rsync Exclude Mechanism
The
rsync utility is a powerful file transfer tool available in Arch Linux that synchronizes data between locations. By default, it copies all files and directories recursively. However, users often need to omit specific subdirectories to save bandwidth or storage space. The primary mechanism for this is the
--exclude option.
When you specify an exclusion pattern,
rsync evaluates each file and directory against the provided rules. If a match is found, that item and all its contents are skipped during the transfer process. This is particularly useful when dealing with large project structures containing temporary files, logs, or build artifacts that do not need to be mirrored on the destination server.
It is important to distinguish between excluding a single file and excluding a directory tree. Excluding a directory removes the folder itself and everything inside it from the operation. In contrast, excluding a file only skips that specific file, leaving the rest of the directory structure intact. Understanding this distinction prevents accidental data loss during synchronization tasks.
Basic Syntax for Excluding Subfolders
The fundamental command structure for excluding a subfolder involves placing the
--exclude flag before the source and destination arguments. The syntax is straightforward:
rsync -av --exclude 'dirname' source/ destination/. The
-a flag ensures archive mode, which preserves permissions, timestamps, and symbolic links, while
-v provides verbose output so you can monitor the progress.
For example, if you have a source directory named
project containing a subfolder named
node_modules, and you want to copy everything except that subfolder, the command would look like this:
rsync -av --exclude 'node_modules' /path/to/project/ /path/to/destination/
Notice the trailing slash on the source directory
/path/to/project/. This tells
rsync to copy the contents of the directory rather than the directory itself. Without the trailing slash, the parent directory might be created at the destination, potentially altering your desired structure. Always verify your path syntax to ensure the correct hierarchy is maintained.
You can also use wildcards within the exclude pattern. For instance,
--exclude '*.log' will skip all files ending in
.log, while
--exclude 'temp/*' excludes everything inside a folder named
temp. These patterns provide flexibility for complex filtering requirements.
Multiple Exclusions and Pattern Matching
In many scenarios, a single exclusion is insufficient. You may need to filter out multiple types of directories or files simultaneously.
rsync allows you to chain multiple
--exclude flags in a single command line. Each flag is evaluated independently, allowing for granular control over what gets transferred.
Consider a scenario where you are syncing a web development project. You likely want to exclude both the
.git directory and the
vendor folder. The command would include both exclusions:
rsync -av --exclude '.git' --exclude 'vendor' /src/webapp/ /dest/webapp/
This approach keeps your command clean and readable. If you have numerous exclusions, managing them inline can become cumbersome. In such cases, consider using a filter file as described in the next section. Additionally, remember that order matters in some advanced filtering contexts, though for simple exclusions, the sequence generally does not impact the outcome.
For more information on refining your transfers, you might find it helpful to review how to handle ownership and permissions during the transfer process, as detailed in
How to Use rsync to Transfer Files to a Server and Set Ownership and Permissions Using sudo. This ensures that even with excluded folders, the remaining files retain their correct attributes.
Using Filter Rules for Complex Scenarios
While individual
--exclude flags work well for simple cases, complex projects may require a more robust solution.
rsync supports filter rules via the
--filter or
-f option, or by using a dedicated filter file. This method is ideal when you have dozens of exclusions or need to combine includes and excludes logically.
To use a filter file, create a text file (e.g.,
excludes.txt) where each line contains a pattern to exclude. Then, invoke
rsync with the
--filter='merge /path/to/excludes.txt' option. This reads the patterns sequentially, applying them to the file list.
Filter rules allow for more sophisticated logic, such as excluding a directory only if it matches certain criteria elsewhere in the path. This level of control is essential for system administrators managing large-scale deployments across multiple servers. It reduces the risk of human error associated with long command lines.
If you encounter issues where permissions are not being set correctly after your transfer, refer to
How to Fix Rsync Not Setting File Permissions Correctly when Transferring Local to Remote to troubleshoot common permission-related pitfalls.
Frequently Asked Questions
Does excluding a folder delete it from the destination?
No,
rsync with the
--exclude flag only prevents new or updated files from being copied. It does not delete existing files in the destination directory. To remove files that exist in the destination but not in the source, you must use the
--delete flag carefully.
Can I exclude hidden directories using rsync?
Yes, you can exclude hidden directories just like any other folder. Use the exact name of the hidden directory, including the leading dot, in your exclude pattern. For example,
--exclude '.cache' will successfully ignore the .cache directory during the transfer.
What happens if I forget the trailing slash on the source path?
Forgetting the trailing slash causes
rsync to create a duplicate copy of the source directory inside the destination. Instead of merging contents, it creates a new folder with the same name as the source. Always double-check your paths to avoid unintended directory nesting.
Is there a way to dry-run exclusions before executing?
Absolutely. Adding the
-n or
--dry-run flag simulates the transfer without making any changes. This allows you to verify that your exclusion patterns are working as expected and that no critical files are being inadvertently skipped.
How do I exclude a directory based on a regular expression?
Standard
--exclude patterns support basic shell globbing. For regular expressions, you should use the
--include-from and
--exclude-from options combined with a filter file, or use the
--filter option with Perl-compatible regular expressions. This provides greater precision for complex naming conventions.