Find command to list the directory names which consists only of "numbers" (0-9)

7,551

Solution 1

You'd want to use double-negation here:

LC_ALL=C find . ! -name '*[!0-9]*' -type d

That is list the files of type directory whose name does not contain a non-digit.

Without LC_ALL=C, some find implementations, including GNU find could also list files whose name contains sequences of bytes that don't form valid characters in the current locale (like a répertoire encoded in iso8859-1 (mkdir $'r\xe9pertoire') in a locale that uses UTF-8 as charset).

With zsh, you can also do:

print -rC1 -- **/<->(ND/)

Solution 2

Two solutions:

With GNU find:

find /particular/path -type d -regextype egrep -regex '.*/[0-9]{8}'

With standard find:

find /particular/path -type d -name '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'

These will find any directory in or under /particular/path that has a name that consists of eight digits and display their pathnames.

If you by "list" mean that you want to see the contents of those directories, then you may modify the commands above by adding -exec ls {} ';'.

Solution 3

If you have GNU find:

find . -regex '^./[0-9]*$'

Adjust the beginning of the regular expression (^./) to match the starting path (.) if you change it.

Share:
7,551

Related videos on Youtube

Jeff Schaller
Author by

Jeff Schaller

Unix Systems administrator http://www.catb.org/esr/faqs/smart-questions.html http://unix.stackexchange.com/help/how-to-ask http://sscce.org/ http://stackoverflow.com/help/mcve

Updated on September 18, 2022

Comments

  • Jeff Schaller
    Jeff Schaller almost 2 years

    In a particular path, I have a few directories (date as filename), for example:

    • if the directory name is like 20180423 it should be listed
    • if the directory name is like 20180423-backup or 20180423backup it should not be listed.
    • Kusalananda
      Kusalananda about 6 years
      What do you mean by "be listed" in this case? Should ls be run on the directory to list its content, or should the directory name/path be listed?