YouTube Icon

Code Playground.

How to Exclude Files and Directories with Rsync


How to Exclude Files and Directories with Rsync

Rsync is a quick and flexible order line utility that synchronizes records and envelopes between two areas over a distant shell. 

With Rsync you can reflect information, make gradual reinforcements and duplicate documents between frameworks. When duplicating information, you might need to avoid at least one documents or catalogs dependent on their name or area. 

In this instructional exercise, we will tell you the best way to prohibit documents and indexes with rsync. 

Before You Begin

You ought to have an essential information on how rsync functions . 

In the models underneath, we will utilize rsync with the - a, choice. This advises rsync to adjusts catalogs recursively, move extraordinary and block gadgets and protect representative connections, alteration times, gathering, proprietorship, and authorizations. 

When barring documents or registries you have to utilize their general ways to the source catalog. 

There are two alternatives to indicate the documents and indexes you need to reject: 

From an order line, utilizing the - bar choice. 

From a document, utilizing the - avoid from alternative. 

Exclude a Specific File

To bar a particular record, pass the relative way to the document to the - reject alternative. 

In the accompanying model the record src_directory/file.txt won't be moved: 

rsync -a --exclude 'file.txt' src_directory/ dst_directory/

Exclude a Specific Directory

Barring a particular catalog is same as barring a document, simply pass the relative way to the index to the - reject choice as demonstrated as follows: 

rsync -a --exclude 'dir1' src_directory/ dst_directory/

On the off chance that you need to prohibit the registry content however not simply the catalog use dir1/* rather than dir1: 

rsync -a --exclude 'dir1/*' src_directory/ dst_directory/

Exclude Multiple Files or Directories

To prohibit numerous documents or indexes just indicate different - bar choices: 

rsync -a --exclude 'file1.txt' --exclude 'dir1/*' --exclude 'dir2' src_directory/ dst_directory/

In the event that you want to utilize a solitary - bar alternative you can list the records and catalogs you need to reject in wavy supports {} isolated by a comma as demonstrated as follows: 

rsync -a --exclude={'file1.txt','dir1/*','dir2'} src_directory/ dst_directory/

On the off chance that the quantity of the records or potentially indexes you need to prohibit is enormous, rather than utilizing numerous - avoid choices you can determine the documents and catalogs you need to bar in a document and pass the document to the - bar from alternative. 

The order underneath does precisely equivalent to the one above: 

rsync -a --exclude-from='exclude-file.txt' src_directory/ dst_directory/



Prohibit Multiple Files or Directories Based on a Pattern 

With rsync you can likewise prohibit documents and indexes dependent on an example that coordinates the record or catalog name. 

For instance, to reject all .jpg records you would run: 

rsync -a --exclude '*.jpg*' src_directory/ dst_directory/

It is minimal trickier to prohibit every other document and catalogs aside from those that coordinate a specific example. Suppose you need to prohibit every other document and registries aside from the records finishing with .jpg. 

One alternative is to utilize the accompanying order: 

rsync -a -m --include='*.jpg' --include='*/' --exclude='*' src_directory/ dst_directory/

When utilizing different incorporate/bar alternative, the main coordinating guideline applies. 

- include='*.jpg' - First we are including all .jpg documents. 

- include='*/' - Then we are including all indexes inside the in src_directory registry. Without this rsync will just duplicate *.jpg records in the high level catalog. 

- m - Removes the vacant catalogs. 

Another choice is pipe the yield of the discover order to rsync: 

find src_directory/ -name "*.jpg" -printf %P\\0\\n | rsync -a --files-from=- src_directory/ dst_directory/

- printf %P\\0\\n - will eliminate the src_directory/from the record way. 

- records from=- - implies incorporate just the documents from the standard information (documents passed from the discover order). 


In this instructional exercise, you figured out how to avoid documents and indexes while moving information with Rsync. There's parts more to find out about Rsync at Rsync User's Manual page.