Convert rsync exclude patterns to explicit list of files

rsyncwildcards

I am trying to find out if there is ways to convert a long list of rsync exclude patterns, that I am maintaining for the purpose of making sensible backups, to an explicit list of file and directory names.

The application is that I am trying to use the same list (whenever I update it by adding or removing a pattern) for a different application (mksquashfs). Both tools allow explicit file lists with one entry per line to be specified as exlude list, but the syntax for the patterns differs in details.

You may assume that the list to be converted consists of all kinds patterns, possibly including wildcards, allowed by rsync.

Two approaches are conceivable:

  1. Find out what rsync uses under the hook (it seems to be neither simple shell globbing nor find)
  2. Use rsync to produce the list by doing a --dry-run dummy transfer with verbose output (here I am not sure how to have it print the names of the excluded files)

Best Answer

Something like this: where "${other_opts[@]}" indocates your normal rsync arguments.

rsync --dry-run --debug=FILTER  "${other_opts[@]}" /proc/$$/no-such-dir/

Should print out all the excluded files.

[sender] hiding file .bash_history-27611.tmp because of pattern *.tmp
[sender] hiding file .bash_history-21217.tmp because of pattern *.tmp
[sender] hiding file .bash_history-29735.tmp because of pattern *.tmp
[sender] hiding file .bash_history-20437.tmp because of pattern *.tmp

you can chop that up using sed.

sed '/^\[sender\] hiding file .* because of / {
  s/^\[sender\] hiding file \(.*\) because of .*/\1/
  p
  }
d '

but this might break if you encounter filenames that contain control characters.

Related Question