I need to check if a directory already exists, in this case the one with a number suffix, and I won't know the exact number. Example directories to search:


[[ -d /usr/names ]] && echo "yes" || echo "no"

  • To get a proper answer, you'll have to explain what type of number you're looking form. – Thomas Dickey Mar 20 '16 at 15:36
  • Similar to the above example, it will be the date of the file's backup, e.g. 20160203232229. The number itself is irrelevant to me, I just need a way to use regex or the equivalent to search for names.^(\d*)$ – Paul Mar 20 '16 at 15:42

If you want to do it all in bash and the file's backup date is always the same number of digits (i.e., yyyymmddhhmmssxx), then a bash file expansion expression like:


(one [0-9] for each digit) would match, but if the number sequence were a different number of digits, you would need an expression for each length.

A good description of file expansion options can be found in the GNU Bash Manual Filename Expansion section.

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.