I would like to find all directories in a folder starting and ending with two digits.
24xyzc91
02abcd00
using find /path of directory -type d | grep [0-9][0-9]*[0-9][0-9]
I can match the digits (with [0-9][0-9]*[0-9][0-9]) but can’t find anyway to handle the full request yet.
Thanks in advance for your help
1
The find
command takes -regex
and -regextype
parameters, so you don’t have to return a large list just to pipe it into grep.
find ./ -type d -regextype posix-extended -regex '.*/[0-9]{2}[^/]*[0-9]{2}$'
The docs state that the match is on the whole path, so I added .*/
at the start to make sure that it finds the final path, and [^/]
to make sure it doesn’t match deep paths which would otherwise match the regex.
I made a few directories:
$ find ./ -type d
./
./11bbfgf00w
./abc
./00a00
./11b00
./11b00/11nsds44
Results:
$ find ./ -type d -regextype posix-extended -regex '.*/[0-9]{2}[^/]*[0-9]{2}$'
./00a00
./11b00
./11b00/11nsds44
1