Shell - How to deal with find -regex?

I need to look in the directory for subdirectories that begin with a "course", but they have the next version. for example

course1.1.0.0
course1.2.0.0
course1.3.0.0

So, how do I change my command so that it gives me the correct directory listing?

find test -regex "[course*]" -type d
+4
source share
3 answers

You can do:

find test -type d -regex '.*/course[0-9.]*'

it will correspond to files whose name course, as well as the number of numbers and points.

For example:

$ ls course*
course1.23.0  course1.33.534.1  course1.a  course1.a.2
$ find test -type d -regex '.*course[0-9.]*'
test/course1.33.534.1
test/course1.23.0
+5
source

You need to remove the brackets and use the appropriate wildcard syntax for regular expressions ( .*):

find test -regex "course.*" -type d

, -name -regex:

find test -name 'course*' -type d
+3

I suggest using regex to match subdirectories of version number exactly:

find . -type d -iregex '^\./course\([0-9]\.\)*[0-9]$'

TESTS:

ls -d course*
course1.1.0.0   course1.1.0.5   course1.2.0.0   course1.txt

find . -type d -iregex '^\./course\([0-9]\.\)*[0-9]$'
./course1.1.0.0
./course1.1.0.5
./course1.2.0.0

UPDATE: To match [0-9].exactly 3 times, use this find command:

find test -type d -regex '.*/course[0-9]\.[0-9]\.[0-9]\.[0-9]$'
+1
source

All Articles