Original path, RootFolderI:

RootFolderI/FolderA/Subfolder1/Subsub1/JPG1.jpg -> want this jpg
RootFolderI/FolderA/Subfolder2/Subsub1/JPG2.jpg -> want this jpg
. . .
RootFolderI/FolderB/Subfolder1/Subsub1/JPG4.jpg -> want this jpg
RootFolderI/FolderB/Subfolder2/Subsub1/JPG5.jpg -> want this jpg

There are many folders FolderA/B/C/etc, but the structure of folders inside is always the same (except the files, they are different)

I want to copy to RootFolderII:

  • only all the FolderA/B/etc
  • all the files in Subsub1

...Resulting is this new path, RootFolderII:

RootFolderII/FolderA/ with JPG1.jpg, JPG2.jpg
RootFolderII/FolderB/ with JPG4.jpg,JPG5.jpg

I've got at the moment the following code (but it's not working):

rsync -r --include='Folder*/Subfolder1/Subsub1/***' --exclude="/*/*/" /RootFolderI/ /RootFolderII/


--exclude="/*/*/" : creates FolderA/B/... -> working

--include='Folder*/Subfolder1/Subsub1/***' : i want to copy all files (not the structure) ... -> it's not working

??? well, I think that even if it was working, it would copy files from FolderB to FolderA, and I don't want that!

  • How do we identify which files you do want, and which you don't? (i.e. why don't you want JPG3.jpg?) – roaima Aug 3 '16 at 12:29
  • 1
    I don't want files from all "Subsub2" folders. Inside FolderA/B/C/etc the structure/naming of folders is the same. Only the files are different – blocnt Aug 3 '16 at 12:34

Give this a shot.

rsync -r --include='*.jpg' --exclude="*" /RootFolderI/Folder*/Subfolder1/Subsub1/. /RootFolderII/.
  • thanks. The problem is that I need to create that middle "Folder*" too (FolderA/B/etc) , and I need those jpg files in the right "Folder*". – blocnt Jun 22 '16 at 14:16
  • I'm not sure how you'd accomplish that. with a single rsync command. I would use a for loop. Something like for DIR in /RootFolderI/Folder*;do rsync -r --include='*.jpg' --exclude="*" ${DIR}/Subfolder1/Subsub1/. $(basename ${DIR})/.;done – Zachary Brady Jun 22 '16 at 15:24

What I did:

  • rsync with includes and excludes

  • Move files up (2level up)

    find /folder/ -type f -mindepth 3 -execdir mv -t ../../ -- {} +
  • Delete empty folders

    find /folder/*/ -type d -empty -delete

I don't think I would use rsync for this task. (There's no delta algorithm, so little to be gained by using it.) Here's a version using cp:

for rfipath in RootFolderI/*
    mkdir -p "RootFolderII/$folder"
    cp -p "$rfipath"/*/Subsub1/* "RootFolderII/$folder"



If you really want to use rsync, replace the cp line with this. It'll gain you the modification times for the directories:

for folder in RootFolderI/*
    rsync -a "$folder"/*/Subsub1/ "RootFolderII/${folder/*\/}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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