How to exclude files during copy?

QuestionsQuestions8 SkillsProBasic Files OperationsNov, 12 2025
0118

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

  1. Basic Syntax:
    The basic syntax for using rsync with exclusion is:

    rsync -av --exclude='pattern' source/ destination/
  2. Example:
    Suppose you want to copy all files from a directory but exclude all .txt files. 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 tells rsync to skip all files that match the pattern *.txt.
  3. Excluding Multiple Patterns:
    You can exclude multiple patterns by using multiple --exclude options. For example:

    rsync -av --exclude='*.txt' --exclude='*.log' ~/source_directory/ ~/destination_directory/
  4. 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 rsync with the --exclude option 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!

0 Comments

no data
Be the first to share your comment!