To exclude specific files while copying in Linux, you can use the rsync command, which provides more flexibility than cp. The rsync command allows you to specify patterns for files you want to exclude. Here’s how to do it:
Using rsync to Exclude Files
-
Basic Syntax:
The basic syntax for usingrsyncwith exclusion is:rsync -av --exclude='pattern' source/ destination/ -
Example:
Suppose you want to copy all files from a directory but exclude all.txtfiles. You would use:rsync -av --exclude='*.txt' ~/source_directory/ ~/destination_directory/In this command:
-a: This option stands for "archive" and preserves permissions, timestamps, etc.-v: This option enables verbose output, so you can see what is being copied.--exclude='*.txt': This tellsrsyncto skip all files that match the pattern*.txt.
-
Excluding Multiple Patterns:
You can exclude multiple patterns by using multiple--excludeoptions. For example:rsync -av --exclude='*.txt' --exclude='*.log' ~/source_directory/ ~/destination_directory/ -
Excluding Directories:
If you want to exclude an entire directory, you can specify it like this:rsync -av --exclude='directory_name/' ~/source_directory/ ~/destination_directory/
Summary
- Use
rsyncwith the--excludeoption to skip specific files or directories during the copy process. - You can specify patterns to exclude multiple files or directories.
This method is very effective for managing file copies while avoiding unwanted files. If you have any more questions or need further assistance, feel free to ask!
