How to exclude numeric directories with rsync?

24,034

Solution 1

rsync's exclude option doesn't really support regex, it's more of a shell globbing pattern matching.

If those directories are fairly static, you should just list them in a file and use --exclude-from=/full/path/to/file/exclude_directories.txt.

Updated to provide example

First, you just put the directories into a file:

find . -type d -regex '.*/[0-9]*$' -print > /tmp/rsync-dir-exlcusions.txt

or

( cat <<EOT
123414
42344523
345343
2323
EOT ) > /tmp/rsync-directory-exclusions.txt

then you can do your rsync work:

rsync -avHp --exclude-from=/tmp/rsync-directory-exclusions.txt /path/to/source/ /path/to/dest/

You just need an extra step to set up the text file that contains the directories to exclude, 1 per line.

Keep in mind that the path of the directories in the job, is their relative path to how rsync sees the directory.

Solution 2

You can't say “a name that contains only digits” in rsync's pattern syntax. So include all names that contain a non-digit and exclude the rest.

rsync --include='*[!0-9]*' --exclude='*/' …

See also my rsync pattern guide.

Solution 3

You can do it in one line:

rsync -avHp --exclude-from=<(cd /path/to/source; find . -type d -regex './[0-9]*' | sed -e 's|./||') /path/to/source/ /path/to/dest/

Solution 4

Use find to make a list of the directories to be excluded, then use rsync's --exclude-from option as Tim Kennedy described it.

Share:
24,034

Related videos on Youtube

cwd
Author by

cwd

Updated on September 18, 2022

Comments

  • cwd
    cwd over 1 year

    I know rsync has an --exclude option which I use quite frequently. But how can I specify that it should exclude all "numeric" directories?

    In the directory listing below I would like to only have it copy css,html,and include

    .
    ..
    123414
    42344523
    345343
    2323
    css
    html
    include
    

    Normally my syntax is something like

    rsync -avz /local/path/ user@server:/remote/path/ --exclude="cache"

    I think it should look something like --exclude="[0-9]*" but I don't think that will work.

  • Rory Alsop
    Rory Alsop over 12 years
    You could use a script which uses a regex to create this file before running the rsync
  • cwd
    cwd over 12 years
    Could you give an example as an answer?
  • Tim Kennedy
    Tim Kennedy over 12 years
    just updated to include an example, including what @RoryAlsop suggested combined with what thiton suggested below.
  • Ludovic Kuty
    Ludovic Kuty about 8 years
    Haa the power of the shell :)
  • Tal Ben Shalom
    Tal Ben Shalom almost 6 years
    @TimKennedy In the file containing excludes, does rsync understand line-changes or space is enough?
  • Tim Kennedy
    Tim Kennedy almost 6 years
    @Abhinav rsync understands one entry per line. spaces will not work.
  • Anthony Geoghegan
    Anthony Geoghegan almost 6 years
    That excludes InnoDB files -- not numeric directories.