0

I am trying to create an rsync job that syncs src /foo/bar/.jpg to dest /foo/.jpg, skipping mid level directories but preserving the top level. i'm sure there is a way to do this with --include --exclude options but i haven't been able to to figure it out.

This is the src structure;

   /Volumes/ExtHDD
└── YYMMDD
    ├── foo1
    │   └── bar
    ├── foo2
    │   └── bar
    └── foo3
        ├── image1.jpg
        ├── image2.jpg
        ├── image3.jpg
        └── image4.jpg

This is what I'm aiming for at the dest;

   /Volumes/OS/Users/user/Dropbox
└── YYMMDD
        ├── image1.jpg
        ├── image2.jpg
        ├── image3.jpg
        └── image4.jpg

I have tried;

rsync -avhW --include='*.jpg' --exclude='*' src dest

but it's not even finding the .jpg files so i guess the exclude option is overriding it, however i thought there was an order of operation to the filters.

  • is it mandatory to use rsync? – D'Arcy Nader Feb 19 '21 at 16:54
  • ideally yes, not included above but i have added options that allow delete files from src and have this update at dest. unless this can also be done using your method? – Harry Bennett-Snewin Feb 19 '21 at 16:59
  • perhaps you can add that to the question or i would have suggested a simple `cp` – D'Arcy Nader Feb 19 '21 at 17:10
  • If you want to copy all `*.jpg` files anywhere in `YYMMDD/`, have a look at [unix.stackexchange.com/questions/2161](https://unix.stackexchange.com/questions/2161), but AFAIK there's no way to flatten the target directory. The simple solution would be: `rsync -avhW src/YYMMDD/foo3/ dest/YYMMDD`. – Freddy Feb 19 '21 at 18:51

1 Answers1

0

You could just copy up the files.

src='/Volumes/ExtHDD'
dst='/Volumes/OS/Users/user/Dropbox'
dir='YYMMDD'

mkdir -p "$dst/$dir"
rsync -av "$src/$dir"/*/*.jpg "$dst/$dir/"

If YYMMDD represents a series of directories use a loop to iterate across them

for dir in "$src"/??????
do
    mkdir -p "$dst/${dir##*/}"
    rsync -av "$dir"/*/*.jpg "$dst/${dir##*/}/"
done
roaima
  • 107,089
  • 14
  • 139
  • 261