我需要在一个目录中查找以"course"开头且带有版本号的子目录。例如:
course1.1.0.0
course1.2.0.0
course1.3.0.0
那么我应该如何修改命令才能让它给我正确的目录列表?
find test -regex "[course*]" -type d
你可以做:
find test -type d -regex '.*/course[0-9.]*'
它将匹配文件名为course
加上一定数量的数字和点号。
例如:
$ 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
.*
):find test -regex "course.*" -type d
-name
选项代替 -regex
:find test -name 'course*' -type d
我建议使用正则表达式来精确匹配版本号子目录:
find . -type d -iregex '^\./course\([0-9]\.\)*[0-9]$'
测试:
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
更新:要匹配[0-9]
恰好3次,请使用此查找命令:
find test -type d -regex '.*/course[0-9]\.[0-9]\.[0-9]\.[0-9]$'
find test -type d -iregex '^\./course\([0-9]\.\)*[0-9]$'
- anubhavafind test -type d -regex '.*/course\([0-9]\.\)*[0-9]$'
- Farahfind
命令也会给出路径:test/course5.4.0.0
,但是你使用了“^”符号来强制匹配开头的某个模式。这是正确的方式:find test -type d -iregex '^test/course\([0-9]\.\)*[0-9]$'
。 - Farahsvn ls
。 - anubhava