• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to copy directories recursively using rsync while excluding specific files

by admin

Question: How to copy folders recursively while excluding specific folders/files whhen using rsync?

In general, we use ‘cp’ command to copy files, but unfortunately, ‘cp’ command doesn’t have the “exclude” feature, so we need to write some scripts to exclude the unwanted files, or we can use another tool ‘rsync’.

From the man page of rsync command:

$ man rsync
...
  -r, --recursive             recurse into directories
  --exclude=PATTERN           exclude files matching PATTERN

The syntax of rsync command to copy directories while excluding specific file/directories is as shown below:

$ rsync -r --exclude 'file_path' 'SRC' 'DEST'

For example, if we don’t want to copy all files of oracle folder to oraclebak except ‘oracle/oradata/rmanbackup’:

$ du -sh oracle/*
40K oracle/extapi
1.1G oracle/oradata                    ------> sub directory of oracle
4.0K oracle/test1
4.0K oracle/test2
4.0K oracle/test3
4.0K oracle/test4
4.0K oracle/test5
# du -sh oracle/oradata/*
4.0K oracle/oradata/date1
4.0K oracle/oradata/date2
4.0K oracle/oradata/date3
4.0K oracle/oradata/date4
4.0K oracle/oradata/date5
1.1G oracle/oradata/rmanbackup  -------> we need to exclude this directory

The exclude path is the relative path of the file under ‘SRC’, e.g. we want to exclude “oracle/oradata/rmanbackup”, and the parent direcotory is oracle, so the exclude path is ‘oradata/rmanbackup’:

# rsync -r --exclude 'oradata/rmanbackup' oracle/ oraclebak
# du -sh oraclebak/oracle/*
40K oraclebak/oracle/extapi
24K oraclebak/oracle/oradata
4.0K oraclebak/oracle/test1
4.0K oraclebak/oracle/test2
4.0K oraclebak/oracle/test3
4.0K oraclebak/oracle/test4
4.0K oraclebak/oracle/test5

You can also use the option “–exclude-from=FILE ” if you have more files/directories to be excluded. In that case, you just have to list out the files/directories to be excluded in a file and provide the file path to the option “–exclude-from=”

# man rsync
--exclude-from=FILE     read exclude patterns from FILE

For example:

# rsync -r --exclude-file=/tmp/exclude_list oracle/ oraclebak

Here,
/tmp/exclude_list contains a list of files/directories to be excluded while copying the others.

Filed Under: Linux

Some more articles you might also be interested in …

  1. How to Setup a sudo Switch to Another User That Has no Password or ssh Key Set in Linux
  2. killall Command Examples in Linux
  3. How to Use rpm2cpio Command in Linux
  4. CentOS / RHEL 6 : How to Disable / Enable direct root login via telnet
  5. mailq Command Examples in Linux
  6. CentOS / RHEL 6 : How to completely remove device mapper multipath (dm-multipath)
  7. Basics of Ethernet Bonding in Linux
  8. kvm-img: command not found
  9. Understanding SELinux Booleans
  10. How to Re-Create the Yum Cache and/or Force a Fetch of the Package List of the Enabled Repositories

You May Also Like

Primary Sidebar

Recent Posts

  • grpck command – Remove corrupt or duplicate entries in the /etc/group and /etc/gshadow files.
  • xxd command – Expressed in hexadecimal form
  • sesearch: command not found
  • macof: command not found

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright