2

I got a few hundred folders, all with the same name structure, ##* , I need to backup with rsync all the folders within 00 to 15_ range to a drive and 16_ to 99_ to another drive. How can I filter the each list to pass this to rsync?.

drwxrwxrwx+  10 user  user    4096 Feb 16  2017 07_MrSmith
drwxrwxrwx+   3 user  user    4096 Mar 24 12:55 10_Other_File
drwxrwxrwx+   4 user  user    4096 Aug 23 14:33 15_New_Interesting_folder
drwxrwxrwx+   9 user  user    4096 Aug 31 17:23 16_SimpleFolderNaming
drwxrwxrwx+   3 user  user    4096 May  8 12:53 17_VeryCurious
drwxrwxrwx+   6 user  user    4096 Mar 28 12:38 17_MantinanVinil
drwxrwxrwx+   3 user  user    4096 Mar 28 11:47 17_AnotherFolderWith_Subfolders

I tried looking for parameters of the 'find', 'xargs' or 'rsync' command that allows to compare subsets with numbers, but I can't find a solution to this.

When I do:

find -maxdepth 1 -regextype posix-extended -regex '..[0-1][0-5]_*'

to filter the folders within 00-15 range doesn't returns anything.

2
  • _* is 0 or more underscores. ITYM, _.* (underscore followed by 0 or more characters). [0-1][0-5] doesn't match on 06 Sep 4, 2017 at 10:14
  • Thank you!, I don't know how I missed that one. Now I'll look into passing this list to rsync.
    – Fabman
    Sep 4, 2017 at 10:20

1 Answer 1

3

With zsh:

rsync ... <0-15>_*(/) somewhere
rsync ... <16->_*(/) somewhere-else

With ksh or bash -O extglob or zsh -o kshglob:

rsync ... @(0[0-9]|1[0-5])_* somewhere
rsync ... @(1[6-9]|[2-9][0-9])_* somewhere-else

Or you could do it in separate globs (though you'll see error messages from rsync if either of the glob doesn't match any file and you could end-up rsyncing files that are called literally 0[0-9]_*, 1[0-5]_*...):

rsync ... 0[0-9]_* 1[0-5]_* somewhere
rsync ... 1[6-9]_* [2-9][0-9]_* somewhere-else

(note that bash and ksh don't support the (/) glob qualifier to select files of type directory only).

POSIXly:

find . ! -name . -prune \( -name '0[0-9]_*' -o -name '1[0-5]_*' \) \
  -type d -exec sh -c 'exec rsync ... "$@" somewhere' sh {} +
find . ! -name . -prune \( -name '1[6-9]_*' -o -name '[2-9][0-9]_*' \) \
  -type d -exec sh -c 'exec rsync ... "$@" somewhere-else' sh {} +
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .