1. 程式人生 > 其它 >【02-01】檔名萬用字元在過濾檔案中的使用

【02-01】檔名萬用字元在過濾檔案中的使用

正則表示式元字元的運用

字元匹配

* 作用:匹配任意長度的任意字元

?作用:匹配任意單個字元

[]:作用:匹配指定範圍內的任意單個字元

特殊格式

[a-z]、[A-Z]、[0-9]、[a-z0-9]

[[:upper:]]:匹配所有大寫字母

[[:lower:]]:匹配所有小寫字母

[[:alpha:]]:匹配所有字母

[[:digit:]]:匹配所有數字

[[:alnum:]]:匹配所有的字母和數字

[[:space:]]:匹配所有的空白字元

[[:punct:]]:匹配所有的標點符號

[^]作用:不匹配指定範圍內(不匹配方括號內的)的字元

[^[:upper:]]

[^0-9]

案例:顯示/etc目錄下,以非字母開頭,後面跟了一個字母以及其它任意長度任意字元的檔案或目錄

1.建立檔案及目錄

touch abc a2c 123 1b3 
mkdir def d5f 456 4e6

2.進行查詢

[root@host etc]# ls -ld /etc/[[:digit:]][[:alpha:]]*
-rw-r--r--. 1 root root 0 Oct 29 00:30 /etc/1b3
drwxr-xr-x. 2 root root 4096 Oct 29 00:30 /etc/4e6
或
[root@host etc]# ls -ld /etc/[^[:alpha:]][^[:digit:]]*
-rw-r--r--. 1 root root    0 Oct 29 00:24 /etc/1b3
drwxr
-xr-x. 2 root root 4096 Oct 29 00:24 /etc/4e6