How can I find all folders starting with a value in the number range 500 to 899? I just need to list them in a file.

Additional informations:

  • Maxdepth 2
  • Examples of folder names: 593091_azerty_qwerty or 849934_blablablabla_bla_blabla
  • What's the format of the number? Can there be 0502foo, 5000bar, 0x200123, 899.99xxx, 5e2-qwe... directories? – Stéphane Chazelas Mar 11 '16 at 9:06
  • Are you looking at the first three characters of the file name? With your file names, I would have thought that 593091_azerty_qwerty doesn't match because 593091 is not in the desired range. Should 0555_foo match? – Gilles Mar 11 '16 at 20:11

try this (if your find supports the -maxdepth option):

find parent_dir -maxdepth 2 -type d \(
  -name '[5-8][0-9][0-9][!0-9]*' -o -name '[5-8][0-9][0-9]' \) > my_file1.txt

With zsh, assuming the numbers are always decimal integers (in the range supported natively by your processor):

setopt extended_glob
dirs=(<500-899>([^0-9]*|)(/N) */<500-899>([^0-9]*|)(/DN))

However note that for the second pattern, if there are symlinks to directories in the current directory, zsh will look for <500-899>... directory in their target.

Shortened with brace expansion factorisation:


If the numbers are always followed by _, you can simplify it to:


Note with versions of zsh prior to 5.2, because of the way integers are represented by the processor, that will match numbers that are between 500 and 899 modulo 264, so for instance would match on 590295810358705652224_something as that's 269 + 512.

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.