You are on page 1of 15

17/12/2016

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

Menu
Home
Free eBook
Start Here
Contact
About

6 rsync Examples to Exclude Multiple Files


and Directories using exclude-from
by Ramesh Natarajan on January 25, 2011
57

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

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

source
- file1.txt
- file2.txt
- dir1
- dir2
- file3.txt
- dir3
- file4.txt

1. Exclude a specific directory


If you dont want to sync the dir1 (including all its subdirectories) from the source to the
destination folder, use the rsync exclude option as shown below.

$ 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. Exclude multiple directories that matches a pattern


The following example will exclude any directory (or subdirectories) under source/ that matches
the pattern dir*
$ rm -rf destination
$ rsync -avz --exclude 'dir*' source/ destination/
building file list ... done
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner

2/15

17/12/2016

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

created directory destination


./
file1.txt
file2.txt

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

3. Exclude a specific file


To exclude a specific file, use the relative path of the file in the exclude option as shown below.
$ rm -rf destination
$ rsync -avz --exclude 'dir1/dir2/file3.txt' source/ destination/
building file list ... done
created directory destination
./
file1.txt
file2.txt
dir1/
dir1/dir2/
dir3/
dir3/file4.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

4. Exclude path is always relative


If you are not careful, you might make this mistake.
In the following example, the exclude option seems to have a full path (i.e /dir1/dir2/file3.txt).
But, from rsync point of view, exclude path is always relative, and it will be treated as
dir1/dir2/file3.txt. In the example below, rsync will look for dir1 under source directory (and not
under / root directory).
$ rsync -avz --exclude '/dir1/dir2/file3.txt' source/ destination/

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

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

$ rsync -avz --exclude 'dir1/dir2/file3.txt' source/ destination/

5. Exclude a specific file type


To exclude a specific file type that has a specific extension, use the appropriate pattern. For
example, to exclude all the files that contains .txt as extension, do the following.
$ rsync -avz --exclude '*.txt' source/ destination/
building file list ... done
created directory destination
./
dir1/
dir1/dir2/
dir3/

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.

6. Exclude multiple files and directories at the same time


When you want to exclude multiple files and directories, you can always specify multiple rsync
exclude options in the command line as shown below.
$ rsync -avz --exclude file1.txt --exclude dir3/file4.txt source/ destination/

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

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

./
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

> Add your comment

If you enjoyed this article, you might also like..


1. 50 Linux Sysadmin Tutorials
2. 50 Most Frequently Used Linux Commands
(With Examples)
3. Top 25 Best Linux Performance Monitoring
and Debugging Tools
4. Mommy, I found it! 15 Practical Linux Find
Command Examples
5. Linux 101 Hacks 2nd Edition eBook

Awk Introduction 7 Awk Print


Examples
Advanced Sed Substitution Examples
8 Essential Vim Editor Navigation
Fundamentals
25 Most Frequently Used Linux
IPTables Rules Examples
Turbocharge PuTTY with 12 Powerful
Add-Ons

{ 32 comments add one }


ashwin January 25, 2011, 5:03 am
Thanks for this information, very useful.
Small typo error at point 6, I think it should be files and directories (Instead of are).
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner

5/15

17/12/2016

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

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

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

Draget July 11, 2011, 1:48 pm


As Felix pointed out, the leading / is of importance. So point 4 should be corrected!
Link
sandeep krishnan August 8, 2011, 3:11 am
Hello Ramesh sir i enjoy reading this article it would be nice if you specify how to use rsync
with ssh on different port
Link
Noel September 8, 2011, 11:12 am
Thanks for the post. There is one case you did not specify, which is also very handy. I found
from here.
Excluding all files but a specific match or directory and its subfiles.
rsync include=*/ include=Dir/* include=*.jpg exclude=* -m
From what I can tell the */ includes all directories you need all parent directories of
desired files included and the -m then prunes all empty directories that had no matching
files.
Link
woo September 21, 2011, 5:36 pm
The exclude option works like a pattern, so:
rsync -av delete /home/me/.mozilla exclude=Cache /media/hddext/backup
will exclude any folder which has a name of Cache.
Link
miguelwill October 30, 2011, 7:36 pm
remember exclude-from option:
/tmp/exclude-files:
*.mp3
*.avi
*.iso
*.mpg
rsync -hva exclude-from=/tmp/exclude-files /source /destination
Link
joe March 30, 2012, 9:39 am
great post.
thanks
Link
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner

7/15

17/12/2016

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

Wehibe June 27, 2012, 11:09 am


Thanks for taking the time to post this. It is handy and very helpful
Cheers
Link
Arthur N. Ketcham July 25, 2012, 2:49 pm
Using the exclude option, exclude dir* will not work correctly. With globbing, you need
to use double quotes. So that should be corrected to:
excludedir*
Link
JD December 5, 2012, 9:24 pm
IMPORTANT NOTE:
NO SPACES AT THE END OF EACH LINE for your exclude-from file
AND
Exclude path is always relative is NOT TRUE exclude path is only relative to the source
directory if you begin your patterns with / otherwise the pattern will match ANYWHERE
in the source directory
Link
iBro December 12, 2012, 8:42 pm
Yes, I agree with JD.
I tried with the following structure and I tried to exlude only dir1/dir3 , but when I use
exclude dir3, dir1/dir2/dir3 and dir1/dir4/dir3 also get excluded.
/tmp $ mkdir -p dir1/dir{2,3,4}/dir{1,2,3}
/tmp $ find dir1
dir1
dir1/dir3
dir1/dir3/dir1
dir1/dir3/dir3
dir1/dir3/dir2
dir1/dir2
dir1/dir2/dir1
dir1/dir2/dir3
dir1/dir2/dir2
dir1/dir4
dir1/dir4/dir1
dir1/dir4/dir3
dir1/dir4/dir2
/tmp $ rsync -av exclude dir3 dir1/ dir2/
sending incremental file list
./
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner

8/15

17/12/2016

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

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

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

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:

Here are some examples of exclude/include matching:


o - /foo would exclude a file (or directory) named foo in the transfer-root directory
o - foo/ would exclude any directory named foo
Kind regards,
Mark
Link
ramy May 23, 2015, 1:15 pm
very useful post. adding an update- on OS X Yosemite 10.10.3 bash, rsync does not
recognize excludes that start with e.g. exclude *.MOV is ignored by rsync. Instead
replace with e.g. exclude *.MOV or exclude-from exclude-list.txt
http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner

10/15

17/12/2016

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

Link
Patrick Goetz June 18, 2015, 4:50 am
This statement:

$ rsync -avz exclude /dir1/dir2/file3.txt source/ destination/


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.
$ rsync -avz exclude dir1/dir2/file3.txt source/ destination/

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

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

Really great post.


I stumbledn on it with the question: Can I exclude from exclude?, e.g
and then rsync exclude-from /dir /dir1/sub1
Another open question: does rsyn support complete regex, if so: which one?
like: rsync -exclude dir/[^a]*
Regards
J.
Link
Seth Sanchez March 15, 2016, 10:06 am
Patrick Goetz wrote:
$ 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.

Patrick, youre thinking of tar.


Link
Leave a Comment
Name
Email
Website

Comment
Submit

Notify me of followup comments via e-mail


Next post: IPTables Flush: Delete / Remove All Rules On RedHat and CentOS Linux
Previous post: Linux Firewall Tutorial: IPTables Tables, Chains, Rules Fundamentals
RSS | Email | Twitter | Facebook | Google+
Search

http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner

12/15

17/12/2016

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

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

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

Can You Top This? 15 Practical Linux Top Command Examples


Top 5 Best System Monitoring Tools
Top 5 Best Linux OS Distributions
How To Monitor Remote Linux Host using Nagios 3.0
Awk Introduction Tutorial 7 Awk Print Examples
How to Backup Linux? 15 rsync Command Examples
The Ultimate Wget Download Guide With 15 Awesome Examples
Top 5 Best Linux Text Editors
Packet Analyzer: 15 TCPDUMP Command Examples
The Ultimate Bash Array Tutorial with 15 Examples
3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id
Unix Sed Tutorial: Advanced Sed Substitution Examples
UNIX / Linux: 10 Netstat Command Examples
The Ultimate Guide for Creating Strong Passwords
6 Steps to Secure Your Home Wireless Network
Turbocharge PuTTY with 12 Powerful Add-Ons
CATEGORIES
Linux Tutorials
Vim Editor
Sed Scripting
Awk Scripting
Bash Shell Scripting
Nagios Monitoring
OpenSSH
IPTables Firewall
Apache Web Server
MySQL Database
Perl Programming
Google Tutorials
Ubuntu Tutorials
PostgreSQL DB
Hello World Examples
C Programming
C++ Programming
DELL Server Tutorials
Oracle Database
VMware Tutorials
Ramesh Natarajan
Follow

About The Geek Stuff

http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner

14/15

17/12/2016

6 rsync Examples to Exclude Multiple Files and Directories using exclude-from

My name is Ramesh Natarajan. I will be posting instruction guides, how-to,


troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to
write articles that will either teach you or help you resolve a problem. Read more about Ramesh
Natarajan and the blog.
Contact Us
Email Me : Use this Contact Form to get in touch me with your comments, questions or
suggestions about this site. You can also simply drop me a line to say hello!.
Follow us on Google+
Follow us on Twitter
Become a fan on Facebook
Support Us
Support this blog by purchasing one of my ebooks.
Bash 101 Hacks eBook
Sed and Awk 101 Hacks eBook
Vim 101 Hacks eBook
Nagios Core 3 eBook
Copyright 20082015 Ramesh Natarajan. All rights reserved | Terms of Service

http://www.thegeekstu.com/2011/01/rsync-exclude-les-and-folders/?utm_source=feedburner

15/15

You might also like