Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm trying to recursively copy a directory / file structure from one directory to another, keeping only html files. Should be a simple case of include / exclude shouldn't it?

I just want to print out the files first. When I get that right, I'll copy them.

rsync -a --list-only -v SOURCEDIR --exclude='.*' --include='**/*.html' 

Gives me all the files.

rsync -a --list-only -v SOURCEDIR --include='**/*.html' --exclude='*' 

and

rsync -a --list-only -v SOURCEDIR --include='*.html' --exclude='*' 
rsync -a --list-only -v SOURCEDIR --include=*.html --exclude=*

Give me no files.

rsync -a --list-only -v SOURCEDIR --include='*.html' --exclude='*.*'

Looks like it gives me the whole directory structure and only html files. But I don't want empty directories.

Help!

On Mac OS 10.6

share|improve this question

3 Answers 3

up vote 0 down vote accepted

Have you considered using find to do your hard work?

Something along the lines of

find ./ -name "*.html" -exec rsync -R {} /target/base/directory/ \; 

will recreate the directory tree of ./ in which html files are found, and build the same under /target/base/directory

share|improve this answer

Rsync can be confusing about selective copies like this. I use the following to do the task that you're asking for:

rsync -avP \
--filter='+ */' \
--filter='+ **/*.html' \
--filter='- *' \
--prune-empty-dirs \
--delete \
/source/ \
/dest/

Basically you need to include all directories in the search, then add all *.html files to the list, the exclude all other files.

The --prune-empty-dirs option is handy to use as it excludes any directory that doesn't have a *.html file.

share|improve this answer

I'm not 100% sure this is the best[0] way to do it, but you can add a very slight tweak to your last attempt and make it work. Just add the prune directories option (--prune or -m).

rsync -am --list-only -v SOURCEDIR --include='*.html' --exclude='*.*'

[0] By 'best', I mean the cleanest and most efficient way. It seems like there should be a more elegant way of expressing this, but I don't know offhand what it is.

share|improve this answer
    
You forgot to add it. –  Dennis Williamson Jul 1 '10 at 20:38
    
No I didn't. It's at the beginning there, 'rsync -am'. I coupled the '-m' with the '-a'. –  Christopher Cashell Jul 30 '10 at 20:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.