Rsync Copy Over Only Certain Types of Files Using Include Option

rsync copy over only certain types of files using include option

I think --include is used to include a subset of files that are otherwise excluded by --exclude, rather than including only those files.
In other words: you have to think about include meaning don't exclude.

Try instead:

rsync -zarv  --include "*/" --exclude="*" --include="*.sh" "$from" "$to"

For rsync version 3.0.6 or higher, the order needs to be modified as follows (see comments):

rsync -zarv --include="*/" --include="*.sh" --exclude="*" "$from" "$to"

Adding the -m flag will avoid creating empty directory structures in the destination. Tested in version 3.1.2.

So if we only want *.sh files we have to exclude all files --exclude="*", include all directories --include="*/" and include all *.sh files --include="*.sh".

You can find some good examples in the section Include/Exclude Pattern Rules of the man page

rsync only certain types of files

I think you are looking for the -m option. From the man page:

-m, --prune-empty-dirs
This option tells the receiving rsync to get rid of empty directories from the file-list, including nested directories that
have no non-directory children. This is useful for avoiding the creation of a bunch of useless directories when the sending
rsync is recursively scanning a hierarchy of files using include/exclude/filter rules.

Note that the use of transfer rules, such as the --min-size option, does not affect what goes into the file list, and thus
does not leave directories empty, even if none of the files in a directory match the transfer rule.

Because the file-list is actually being pruned, this option also affects what directories get deleted when a delete is active.
However, keep in mind that excluded files and directories can prevent existing items from being deleted due to an exclude both
hiding source files and protecting destination files. See the perishable filter-rule option for how to avoid this.

You can prevent the pruning of certain empty directories from the file-list by using a global "protect" filter. For instance,
this option would ensure that the directory "emptydir" was kept in the file-list:

--filter ’protect emptydir/’


Here’s an example that copies all .pdf files in a hierarchy, only creating the necessary destination directories to hold the
.pdf files, and ensures that any superfluous files and directories in the destination are removed (note the hide filter of
non-directories being used instead of an exclude):

rsync -avm --del --include=’*.pdf’ -f ’hide,! */’ src/ dest


If you didn’t want to remove superfluous destination files, the more time-honored options of "--include='*/' --exclude='*'"
would work fine in place of the hide-filter (if that is more natural to you).

How to rsync only a specific list of files?

Edit: atp's answer below is better. Please use that one!

You might have an easier time, if you're looking for a specific list of files, putting them directly on the command line instead:

# rsync -avP -e ssh `cat deploy/rsync_include.txt` root@0.0.0.0:/var/www/

This is assuming, however, that your list isn't so long that the command line length will be a problem and that the rsync_include.txt file contains just real paths (i.e. no comments, and no regexps).

rsync copy only specific files from specific subfolders, without creating empty folders

It turns out the answer is simple. Just adding the --prune-empty-dirs flag does the trick.

rsync -avz --include=*/ --include='*/0/*.txt' --exclude=* --prune-empty-dirs test1/ test2/

That is what I wanted.

rsync copy for a lots of different types of extensions

I didnt want to post an answer to my question but just wanted share and hope there shouldn't be no more stupid like me.

I found the reason. I was running the source path as absolute path. I needed to cd to the root, which is Macintosh HD by

cd /

and then run the rsync command.
In my case, I'm copying all of my OS disk specifying a lots of extensions to the external disk so I ticked -aPcx which means
archive,skip checksum, exclude other disks.

especially 'x' option is important otherwise you'll get a copy of USB disk itself to the destination folder.

rsync -aPcx --prune-empty-dirs --log-file=/Volumes/SSD/Company_Dept_username.log --include=*/ --include=*.{hwp,doc,ppt,xls,docx,pptx,xlsx,pdf,gul,hst,db,dbf,dzw,dzi,dbs,mdb,accdb,pst,eml,msg,edb,dbx,ost,mbox,zip,ace,arj,cab,egg,7z,enc,alz} --exclude=* . /Volumes/SSD/Company/Dept/username

rsync include specific files and exclude directory

You need to put --exclude='build' before --include '*/'. Both of these rules could apply to the "build" directory, and whichever is given first takes precedence, so to get the --exclude rule to override the --include rule, you need to list it first.

From the rsync man page, in the FILTER RULES section (with my emphasis):

As the list of files/directories to transfer is built, rsync checks
each name to be transferred against the list of include/exclude
patterns in turn, and the first matching pattern is acted on: if it is
an exclude pattern, then that file is skipped; if it is an include
pattern then that filename is not skipped; if no matching pattern is
found, then the filename is not skipped.

Rsync include a specific file under a certain structure

Is there a reason you're using --include? If it's a single file then rsync /one/two/file.txt /target/ would work.

Have a look at this answer:
How to RSYNC a single file?

If you're trying to send more than one file then you can use this, based on a pattern:
Using Rsync include and exclude options to include directory and file by pattern



Related Topics



Leave a reply



Submit