Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Rsync include and exclude options to include directory and file by pattern

I'm having problems getting my rsync syntax right and I'm wondering if my scenario can actually be handled with rsync. First, I've confirmed that rsync is working just fine between my local host and my remote host. Doing a straight sync on a directory is successful.

Here's what my filesystem looks like:

uploads/   1260000000/     file_11_00.jpg     file_11_01.jpg     file_12_00.jpg   1270000000/     file_11_00.jpg     file_11_01.jpg     file_12_00.jpg   1280000000/     file_11_00.jpg     file_11_01.jpg     file_12_00.jpg 

What I want to do is run rsync only on files that begin with "file_11_" in the subdirectories and I want to be able to run just one rsync job to sync all of these files in the subdirectories.

Here's the command that I'm trying:

rsync -nrv --include="**/file_11*.jpg" --exclude="*" /Storage/uploads/ /website/uploads/

This results in 0 files being marked for transfer in my dry run. I've tried various other combinations of --include and --exclude statements, but either continued to get no results or got everything as if no include or exclude options were set.

Anyone have any idea how to do this?

like image 507
Sheldon Chang Avatar asked Mar 31 '12 00:03

Sheldon Chang


People also ask

How do I exclude in rsync?

When you need to exclude a large number of different files and directories, you can use the rsync --exclude-from flag. To do so, create a text file with the name of the files and directories you want to exclude. Then, pass the name of the file to the --exlude-from option.

Does rsync go both ways?

rsync works in one direction, so we need to run it twice to sync directories in both directions.

How do I rsync a specific file?

Copy a single file locally If you want to copy a file from one location to another within your system, you can do so by typing rsync followed by the source file name and the destination directory. Note: Instead of “/home/tin/file1. txt”, we can also type “file1” as we are currently working in the home directory.


1 Answers

The problem is that --exclude="*" says to exclude (for example) the 1260000000/ directory, so rsync never examines the contents of that directory, so never notices that the directory contains files that would have been matched by your --include.

I think the closest thing to what you want is this:

rsync -nrv --include="*/" --include="file_11*.jpg" --exclude="*" /Storage/uploads/ /website/uploads/ 

(which will include all directories, and all files matching file_11*.jpg, but no other files), or maybe this:

rsync -nrv --include="/[0-9][0-9][0-9]0000000/" --include="file_11*.jpg" --exclude="*" /Storage/uploads/ /website/uploads/ 

(same concept, but much pickier about the directories it will include).

like image 128
ruakh Avatar answered Oct 14 '22 01:10

ruakh