Professional Documents
Culture Documents
Menu
Home
Free eBook
Start Here
Contact
About
Like 19
Tweet
Rsync is very powerful tool to take backups, or sync files and directories between two different
locations (or servers).
You know this already, as we presented you with practical examples on rsync earlier.
In a typical backup situation, you might want to exclude one or more files (or directories) from the
backup. You might also want to exclude a specific file type from rsync.
This article explains how to ignore multiple files and/or directories during rsync with examples.
First, create a sample directory structure as shown below (with some empty files) that can be used
for testing purpose.
$
$
$
$
$
$
$
cd ~
mkdir
mkdir
touch
touch
touch
touch
-p source/dir1/dir2
-p source/dir3
source/file1.txt
source/file2.txt
source/dir1/dir2/file3.txt
source/dir3/file4.txt
The above command will create a source directory (under your home directory) with the following
structure.
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner
1/15
17/12/2016
source
- file1.txt
- file2.txt
- dir1
- dir2
- file3.txt
- dir3
- file4.txt
$ rm -rf destination
$ rsync -avz --exclude 'dir1' source/ destination/
building file list ... done
created directory dest
./
file1.txt
file2.txt
dir3/
dir3/file4.txt
Verify to make sure dir1 is not copied from source directory to destination directory.
$ find destination
destination
destination/file2.txt
destination/file1.txt
destination/dir3
destination/dir3/file4.txt
2/15
17/12/2016
Verify the destination directory to make sure it didnt copy any directories that has the keyword
dir in it.
$ find destination
destination
destination/file2.txt
destination/file1.txt
Verify the destination directory to make sure it didnt copy the specific file ( dir1/dir2/file3.txt in
this example).
$ find destination
destination
destination/file2.txt
destination/file1.txt
destination/dir1
destination/dir1/dir2
destination/dir3
destination/dir3/file4.txt
So, the above command is exactly same as the following. Just to avoid confusion (and to make it
easy to read), dont give / in front of the exclude path.
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner
3/15
17/12/2016
Verify the destination directory to make sure it didnt copy the *.txt files.
$ find destination
destination
destination/dir1
destination/dir1/dir2
destination/dir3
Note: The above is very helpful, when you want to backup your home directory, but exclude all
those huge image and video files that has a specific file extension.
Wait. What if I had tons of files that I want to exclude from rsync?
I cant keep adding them in the command line using multiple exclude, which is hard to read, and
hard to re-use the rsync command for later.
So, the better way is to use rsync exclude-from option as shown below, where you can list all the
files (and directories) you want to exclude in a file.
First, create a text file with a list of all the files and directories you dont want to backup. This is
the list of files and directories you want to exclude from the rsync.
$ vim exclude-list.txt
file1.txt
dir3/file4.txt
Next, execute the rsync using exclude-from option with the exclude-list.txt as shown below.
$ rm -rf destination
$ rsync -avz --exclude-from 'exclude-list.txt' source/ destination/
building file list ... done
created directory destination
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner
4/15
17/12/2016
./
file2.txt
dir1/
dir1/dir2/
dir1/dir2/file3.txt
dir3/
Verify the desitination directory to make sure the files and directories listed in the exclude-list.txt
file is not backed-up.
$ find destination
destination
destination/file2.txt
destination/dir1
destination/dir1/dir2
destination/dir1/dir2/file3.txt
destination/dir3
57
Tweet
Like 19
5/15
17/12/2016
Link
carlos January 25, 2011, 8:01 am
Good post,its very usefull. thank you very much!!! from argentina.
Link
Felix Frank January 25, 2011, 10:32 am
As always, I feel compelled to amend
The leading slash / in the exclude is of great importance. It means match only at the base
of the source tree. Assume the following:
tools/
src/
program.c
tools/
So there is the directory tools/ and src/tools/. exclude tools/ should exclude both of those
directories named tools, whereas exclude /tools/ will only exclude the former (which is
probably more often intended).
Link
Ramesh Natarajan January 25, 2011, 10:01 pm
@Ashwin,
Thanks for pointing out the typo. It is fixed.
@Felix,
Your amendment are always most welcome, and they are helpful. Thanks.
Link
Jonathan Lumb February 16, 2011, 4:35 pm
Thank you for taking the time to write such a systematic guide to understanding and using
the exclude option in Rsync very useful and cleared up some points I was uncertain about!
Jonathan
Link
sathiya March 28, 2011, 1:47 am
It saved my time !!!
Thanks.
Link
Sumesh April 13, 2011, 9:35 am
Really helpful doc. Thanks for sharing.
Link
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner
6/15
17/12/2016
7/15
17/12/2016
8/15
17/12/2016
dir2/
dir2/dir1/
dir2/dir2/
dir4/
dir4/dir1/
dir4/dir2/
sent 146 bytes received 39 bytes 370.00 bytes/sec
total size is 0 speedup is 0.00
/tmp $ find dir2
dir2
dir2/dir2
dir2/dir2/dir1
dir2/dir2/dir2
dir2/dir4
dir2/dir4/dir1
dir2/dir4/dir2
Link
Owen March 19, 2013, 11:51 pm
So am i correct in assuming that
the excluded files path is relative to the source path iff the source path is absolute
?
Link
everge48 March 21, 2013, 1:17 pm
Here are some examples of exclude/include matching:
exclude *.o would exclude all filenames matching *.o
exclude /foo would exclude a file called foo in the transfer-root directory
exclude foo/ would exclude any directory called foo
exclude /foo/*/bar would exclude any file called bar two levels below a directory called
foo in the transfer-root directory
exclude /foo/**/bar would exclude any file called bar two or more levels below a
directory called foo in the transfer-root directory
include */ include *.c exclude * would include all directories and C source files
include foo/ include foo/bar.c exclude * would include only foo/bar.c (the foo/
directory must be explicitly included or it would be excluded by the *)
Link
Jordan April 17, 2013, 12:54 pm
Hi Ramesh
One more time your tip was so helpfull.
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner
9/15
17/12/2016
Keep writting!
Tks!
Jordan
Link
Seb May 3, 2013, 7:35 am
Great stuff, thanks! Always forget the exclude syntax all the common options in one place
Link
ronak June 6, 2013, 11:23 am
Hey Guys thnxs for sharing the information.
I need one help:
Say i have the directory log located at two locations as:
/a/b/log and
/a/b/c/log
i dont want to exclued the first one in /a/b/log but only /a/b/c/log.
Thanks
Link
Leila October 28, 2014, 12:40 am
Thank you soooooooooooo much. The instructions were very clear and very helpful!
Link
Mark March 27, 2015, 2:33 am
Hi! Youve made a mistake in section 4 Exclude path is always relative.
Its true that it is always relative but its NOT the same using the leading slash or not to do
so. The leading slash indicates the full qualified path beginning at your given transfer-root
directory.
See also the man page of rsync:
10/15
17/12/2016
Link
Patrick Goetz June 18, 2015, 4:50 am
This statement:
is flat out incorrect. With a slash at the beginning of the exclude value, the pattern is
anchored to the top of the directory tree. If you remove the slash then that pattern is matched
throughout the entire directory tree (i..e any dir1/dir2/file3.txt further down the tree will be
excluded as well).
Furthermore, it makes a difference if the source ends with a slash or not. Remove the slash
from the end of source/ and the search pattern goes back to the parent directory; i.e.
$ rsync -avz exclude /dir1/dir2/file3.txt source destination/
will exclude nothing. Please try and understand how these tools work before writing a blog
post like this.
Link
nda August 24, 2015, 4:26 am
Exclude specify file doesnt work
rsync -avz exclude dir1/dir2/file3.txt source/ destination/
please help
Link
Akshay Chakre October 30, 2015, 1:02 am
Thanks, this is very easy to understand.
Link
i332095 November 7, 2015, 3:13 pm
Enabling compression (-z) on local transfers only results in higher CPU usage.
Link
Sharjeel March 3, 2016, 1:54 am
How do you exclude dot files and directories ?
Link
J. Mueller March 6, 2016, 4:14 am
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner
11/15
17/12/2016
Comment
Submit
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner
12/15
17/12/2016
EBOOKS
Linux 101 Hacks 2nd Edition eBook - Practical Examples to Build a Strong Foundation
in Linux
Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting
Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk
Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim Editor
Nagios Core 3 eBook - Monitor Everything, Be Proactive, and Sleep Well
POPULAR POSTS
12 Amazing and Essential Linux Books To Enrich Your Brain and Library
50 UNIX / Linux Sysadmin Tutorials
50 Most Frequently Used UNIX / Linux Commands (With Examples)
How To Be Productive and Get Things Done Using GTD
30 Things To Do When you are Bored and have a Computer
Linux Directory Structure (File System Structure) Explained with Examples
Linux Crontab: 15 Awesome Cron Job Examples
Get a Grip on the Grep! 15 Practical Grep Command Examples
Unix LS Command: 15 Practical Examples
15 Examples To Master Linux Command Line History
Top 10 Open Source Bug Tracking System
Vi and Vim Macro Tutorial: How To Record and Play
Mommy, I found it! -- 15 Practical Linux Find Command Examples
15 Awesome Gmail Tips and Tricks
15 Awesome Google Search Tips and Tricks
RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner
13/15
17/12/2016
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner
14/15
17/12/2016
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner
15/15