Let's say I have this file structure:


Is there any way for me to rsync the directories and its contents while excluding the files outside the directories(1.jpg, 2.jpg and 3.jpg)? I have some sort of planned backup thing going on and wanna add a script, make it executable and automate it via crontab, thanks!

  • if that files are all .jpg files, just use > rsync -avz --exclude '*.jpg' source/ dest/ – Kresimir Pendic Jan 20 at 7:44
  • Yes, but the .jpg files inside the directories are also excluded, it only copies the directories, not the contents within the directories, that was what I tried earlier. – emmanuel Jan 20 at 7:46
  • did you tried rsync -avz --exclude '/*.jpg' source/ dest/ – Kresimir Pendic Jan 20 at 7:48
  • that worked! my parameters were "-avrP" and i saw somewhere that using the z flag takes up unnecessary CPU resources, thank you! – emmanuel Jan 22 at 7:06
  • at first i tried: rsync -avrP --exclude './*.jpg' ./* /destination/ then: rsync -avrP --exclude '.jpg' ./ /destination/ was the issue the recursion? – emmanuel Jan 22 at 7:15

To exclude only files in top root directory, you have to add starting slash /, like:

rsync -avz --exclude '/*.jpg' source/ dest/

that will exclude all image files only in root and not in any other child directories

enter image description here

  • 1
    thanks for the visual aid, your suggestion worked, i had the wrong parameters, so rsync treats the current directory you are in as the root directory in its parameters? – emmanuel Jan 22 at 7:07

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.