I need to copy directory structure from one place to another. I need to copy only folders and exclude files.

Main problem is that i have link-folders ! enter image description here

when I type

rsync -avz -H  /tmp/test/ /tmp/copy/

it copy folders and links-folder properly, also it copy files

when I type

rsync -avz -H -f"+ */" -f"- *" /tmp/test/ /tmp/copy/

it include all folders, exclude files, but it also exclude link-folders.

when I type

rsync -avz -H -f"+ */" -f"- *.txt" /tmp/test/ /tmp/copy/

it create folders and link folders properly but exclude o nly txt files

when I type

rsync -vzrptgoD -L -H -f"+ */" -f"- *" /tmp/test/ /tmp/copy/

it exclude files but converts link-folders in real folder,just concatenate paths.

There are many types of file and i cant just add them like -f"- *.txt"

What should i do? can i add link-folder syntax in -f"+ */" ?

If not is there any other options?

1 Answer 1


You can't do this directly in rsync, since as you've noticed its filter rules only differentiate directories from "everything else".

However, you can construct a find command to pick the items you want (l for symbolic links, d for directories), and then pass that list to rsync for copying

find src/ \( -type l -o -type d \) -printf "%P\0" |
    rsync --files-from - --from0 -av src/ dst/


mkdir /tmp/608951
cd /tmp/608951
mkdir -p src/{test1,test2} dst; ln -s test1 test/link

Demonstration, copying directories and symbolic links from src to dst

find src/ \( -type l -o -type d \) -printf "%P\0" | rsync --files-from - --from0 -av src/ dst/


building file list ... done
link -> test1

sent 134 bytes  received 25 bytes  318.00 bytes/sec
total size is 5  speedup is 0.03

Evidence, using ls -R src dst, showing that dst contains only directories and the symbolic link, whereas src also contains files

link  test1  test2



link  test1  test2



If you don't have GNU find, change its -printf "%P\n" to plain -print and also remove --from0 from rsync. The downside is that you won't be able to copy files whose names contain strange characters such as newline.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .