0

I have the following tree, where f are files while src and d are directories (f could be hidden or not):

src/f1
src/f2
...
src/d1
src/d2
src/d3
...

I want to replicate the same structure in a dst folder, but exclude all files present in the src folder (that is f1, f2, etc. in the tree above, but NOT the content of d1, d2, d3 and the other folders). In other words, I want to copy from src to dst only the folders and all their content (files, folders, link, devices, etc.).

I must use rsync and preferably only a --exclude-from file (but any other way using rsync are welcome).

I tried to put /* in the exclusion's file, since / allows me to anchor at src and * means "match all things except slahes (/)", but it results in an empty dst folder.

Thank you very much

2
  • 2
    Did you try something ? Did you search ? Or is it your school homework ?
    – user4089
    Commented Feb 10, 2023 at 14:55
  • @user4089 yes, I tried something (see above) Commented Feb 10, 2023 at 15:13

2 Answers 2

2

man rsync say :

option --filter=RULE, -f add a file-filtering RULE

So, try

rsync -avr -f"+ */" -f"- *" /src/ /dest/

-f"+ */" will add directories when -f"- *" will remove files everywhere.

As @Kusalananda said, this will avoid any file in all subtree.

rsync -avr -f"+ */" -f"- /*" /src/ /dest/ or

rsync -avr --include="*/" --exclude="/*" /src/ /dest/ will sync files in tree subdirs.

2
  • 2
    Wouldn't that exclusion rule also exclude files in any other subdirectory, not just files from /src? Using the ordinay filtering options, I think you should be able to work around it with --include='*/' and --exclude='/*' (i.e., include all subdiretories, but exclude anyting from the top /src directory not previously included).
    – Kusalananda
    Commented Feb 10, 2023 at 15:30
  • 1
    @Kusalananda You're right ! I didn't pay attention to the "but NOT the content of d1, ...". I update my answer.
    – user4089
    Commented Feb 10, 2023 at 15:42
1

Excluding everything in the source directory with /* works, you only need to include the directories again:

sudo rsync -av --include='/*/' --exclude='/*' src/ dest
  • --exclude='/*' excludes all files/directories in the source directory

  • --include='/*/' overrides the exclude and includes directories in the source directory again, must be written before the exclude

  • -a archive mode, shortcut for -rlptgoD

    • -r recurse into directories
    • -l copy symlinks as symlinks
    • -p preserve permissions
    • -t preserve modification times
    • -g preserve group
    • -o preserve owner (super-user only)
    • -D preserve device files (super-user only) / special files
  • -v increase verbosity

You must log in to answer this question.

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