I have a setup where I use rsync
to mirror some source directory to a (remote) destination except for some files and locations defined witch rsync exclude patterns:
# Useless files
- thumbs.db
- *.~
- *.tmp
- /*/.cache
- /*/.local/share/trash
# Already rsynced somewhere
- .dropbox
# Medias files
- *.avi
- *.mkv
- *.wav
- *.mp3
- *.bmp
- *.jpg
# System files
- /hiberfil.sys
- /pagefile.sys
This script runs on Windows and Linux stations and uses the --delete
parameter to delete extraneous files from dest dirs.
The thing is, when I "update" the exclude patterns (eg. add an exclusion pattern for Ogg files: *.ogg
) I have to re-run the rsync to remove any existing Ogg file from the destination.
I was wondering if I could easily apply the exclude rules (some can be complex because it uses wildcards) on a given directory so that I don't have to re-run the rsync from the source to clean my destination directory.
So far I've come to the following which deletes files and directories using basename matching:
dirToClean="/var/somedir"
excludeFile="file_to_exclude"
# Delete files and dir of $dirToClean whose basename matches an exclude pattern from $excludeFile
for fileToDelete in $(grep --extended-regexp "^- .*" $excludeFile | sed 's/^- \(.*\)$/\1/'); do
find "$dirToClean" -iname "$fileToDelete" -exec rm {} \;
done
Maybe I could run rsync using the same directory as source and destination?