Search code examples
linuxbashshellfind

Search 32 character long directories only with find


Let say I have a directory with a lot of subdirectories:

04762b39018e3cf4b1a2c6a304919b75
06e0caf156de30dd962cf6b9300aba66
1f1d0cb1b810336299cda5426d0f12f5
2fe7a428eb303da6846800fa20ab7ed4
41e0136413703b0685d6799f8a22a8e6
asdf
1234
Databases

Some of this directories are exact 32 characters long, (md5hash actually) and I would like to find them. Recursive search is not required in this case. I tried with this command:

find /root/my/subdir -maxdepth 1 -type d -regex '^.*[a-fA-F0-9]{32}$'

and I would get result:

04762b39018e3cf4b1a2c6a304919b75
06e0caf156de30dd962cf6b9300aba66
1f1d0cb1b810336299cda5426d0f12f5
2fe7a428eb303da6846800fa20ab7ed4
41e0136413703b0685d6799f8a22a8e6

but I didn't get anything. In my case, I would prefer find for this, because then it is easy to pass bash commands later, but anything bash compatible one-liner will be good.


Solution

  • Depending on your version of find you might only need to turn on egrep:

    $ find /root/my/subdir -maxdepth 1 -type d -regextype egrep -regex '^.*[a-fA-F0-9]{32}$'