rsync is a widely-used utility to keep copies of a file on two computer systems. It is commonly found on Unix-like systems and functions as both afile synchronization and file transfer program. The rsync algorithm, a type of delta encoding, is used to minimize network usage. Zlib may be used for additional compression, and SSH or stunnel can be used for data security.
How to use ‘cp’ command to exclude a specific directory?
I found rsync when I was trying to copy all files except “x” and “y” files and directories. You are able to do that as following:
rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude
Notice that you can add many –excludes like:
rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude --exclude anotherfoldertoexclude
Found some great samples by Ramesh Natarajan I have copied few below with a link to more samples.
Example 1. Synchronize Two Directories in a Local Server
To sync two directories in a local computer, use the following rsync -zvr command.
$ rsync -zvr /var/opt/installation/inventory/ /root/temp
In the above rsync example:
- -z is to enable compression
- -v verbose
- -r indicates recursive
Example 2. Preserve timestamps during Sync using rsync -a
rsync option -a indicates archive mode. -a option does the following,
- Recursive mode
- Preserves symbolic links
- Preserves permissions
- Preserves timestamp
- Preserves owner and group
Now, executing the same command provided in example 1 (But with the rsync option -a) as shown below:
$ rsync -azv /var/opt/installation/inventory/ /root/temp/
Example 3. Synchronize Only One File
To copy only one file, specify the file name to rsync command, as shown below.
$ rsync -v /var/lib/rpm/Pubkeys /root/temp/
More samples at: