linuxrsync

How to select all files with certain postfix in all subdirectories in certain directory?


I want to sync from source to dest, only pass those files with postfix "*.gz"

the tree command of tree root@tt:/source is

/source
├── 20250108
│   └── tt
│       ├── binance
│       │   └── swap
│       │       └── bookticker
│       │           ├── 1000000MOG_USDT.20250108.bin.gz

I tried:

rsync -avP -n -f "+ */*/*/*/*/*.gz" -f "- *" root@tt:/source /dest
rsync -avP -n -f "+ *.gz" -f "- *" root@tt:/source /dest

above two commands matchs none.

could you help on this? ps. i want to keep the directories tree structure, also.


Solution

  • To sync files with the .gz postfix from the source to the destination while preserving the directory structure, you can use the following rsync command:

    rsync -avP --include='*/' --include='*.gz' --exclude='*' root@tt:/source/ /dest
    

    --include='*/' includes all directories, --include='*.gz' includes all .gz files, and --exclude='*' excludes all other files.

    This command ensures that only .gz files are transferred, and the directory structure is preserved.