linux查找命令能結合正則表達式嗎
find命令要使用正則表達式需要結合-regex參數
另,-type參數可以指定查找類型(f為文件,d為文件夾)
root@localhost:~/regular_expression# ls -alh
總計 8.0K
drwxr-xr-x. 5 root root 66 4月 8日 16:26 .
dr-xr-x---. 19 root root 4.0K 4月 8日 16:25 ..
drwxr-xr-x. 2 root root 6 4月 7日 09:55 1213
drwxr-xr-x. 2 root root 6 4月 8日 16:26 cc
-rw-r--r--. 1 root root 660 4月 7日 15:04 regular_express.txt
drwxr-xr-x. 2 root root 6 4月 7日 09:54 't a'
# .* -->代表零個或多個任意字符的意思
#因為 . 與 * 兩者是 “相乘” 的關系,等于 . 所代表的 一個任意字符 乘以 * 所代表的零個到無窮多個字符。所以,包含從零個開始到任意多個字符。
#查找以 .txt結尾的文件
root@localhost:~/regular_expression# find . -type f -regex ".*\.txt"
./regular_express.txt
root@localhost:~/regular_expression# find . -type f -regex ".*c"#查找包含任意字符的文件
root@localhost:~/regular_expression# find . -type f -regex ".*"
./regular_express.txtroot@localhost:~/regular_expression# find . -regex ".*"
.
./t a
./1213
./regular_express.txt
./ccroot@localhost:~/regular_expression# find . -regex ".*c"
./ccroot@localhost:~/regular_expression# find . -regex ".*c$"
./ccroot@localhost:~/regular_expression# find . -regex "^.*c$"
./cc#指定查找類型為文件夾類型
root@localhost:~/regular_expression# find . -type d -regex "^.*c$"
./cc