• 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. DHCP configuration file /etc/dhcp/dhcpd.conf explained
  2. legit Command Examples in Linux
  3. How to Run SCP Without Password Prompt Interruption in Linux
  4. CentOS / RHEL : How to convert volume group metadata between LVM1 and LVM2
  5. How to use shell expansions for generating shell tokens under Linux
  6. Linux / UNIX : How to create extended partition using fdisk
  7. Shell/Bash Script to Find Prime Numbers in Linux
  8. ln Command Examples in Linux
  9. Understanding rsyslog Actions
  10. How to extract RPM package without installing it

You May Also Like

Primary Sidebar

Recent Posts

  • nixos-rebuild Command Examples in Linux
  • nixos-option: Command Examples in Linux
  • nixos-container : Command Examples in Linux
  • nitrogen Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright