1. 程式人生 > 實用技巧 >linux系統中常用的萬用字元*、?、[ ]、[^xxx]

linux系統中常用的萬用字元*、?、[ ]、[^xxx]

1、*比較常用的萬用字元,表示匹配任意長度的任意字元

建立測試資料:

[root@linuxprobe test]# touch {1..3}.txt {1..3}.csv {1..3}.xxx  ## 利用touch常見空檔案
[root@linuxprobe test]# ls
1.csv  1.txt  1.xxx  2.csv  2.txt  2.xxx  3.csv  3.txt  3.xxx
[root@linuxprobe test]# ll -h *.txt   ## 利用*匹配所有的字尾為txt的檔案
-rw-r--r--. 1 root root 0 Oct  5 10:38
1.txt -rw-r--r--. 1 root root 0 Oct 5 10:38 2.txt -rw-r--r--. 1 root root 0 Oct 5 10:38 3.txt [root@linuxprobe test]# ll -h *.txt *.xxx ## 利用*匹配所有的字尾為txt和xxx的檔案 -rw-r--r--. 1 root root 0 Oct 5 10:38 1.txt -rw-r--r--. 1 root root 0 Oct 5 10:38 1.xxx -rw-r--r--. 1 root root 0 Oct 5 10:38 2.txt -rw-r--r--. 1
root root 0 Oct 5 10:38 2.xxx -rw-r--r--. 1 root root 0 Oct 5 10:38 3.txt -rw-r--r--. 1 root root 0 Oct 5 10:38 3.xxx

2、?用於匹配任意單個字元

[root@linuxprobe test]# rm *  ## 清空當前目錄
[root@linuxprobe test]# touch 1.txt 12.txt 123.txt ## 建立測試資料
[root@linuxprobe test]# find ?.txt  ## ? 匹配任意單個字元
1.txt
[root@linuxprobe test]# find 
??.txt ##同上 12.txt [root@linuxprobe test]# find 1?.txt ## 同上 12.txt [root@linuxprobe test]# find ???.txt ## 同上 123.txt [root@linuxprobe test]# find 1??.txt ## 同上 123.txt [root@linuxprobe test]# find ?2?.txt ## 同上 123.txt

3、[] 匹配中括號內的任意一個字元

[root@linuxprobe test]# rm * ## 清空當前目錄
[root@linuxprobe test]# touch 1.txt 3.txt 12.txt 123.txt a.txt x.txt ab.txt ##建立測試資料
[root@linuxprobe test]# ls
123.txt  12.txt  1.txt  3.txt  ab.txt  a.txt  x.txt
[root@linuxprobe test]# find [0-9].txt  ##用於匹配0到9的任意單個數字
1.txt
3.txt
[root@linuxprobe test]# find [a-z].txt  ##用於匹配a到z的任意單個字母
a.txt
x.txt
[root@linuxprobe test]# find [178].txt  ##匹配1、7、8中的任意一個數字
1.txt
[root@linuxprobe test]# find [abcde].txt ##匹配a、b、c、d、e中任意一個字母
a.txt

4、[root@linuxprobe test]# ls

123.txt  12.txt  1.txt  3.txt  ab.txt  a.txt  x.txt
[root@linuxprobe test]# find [1].txt ## 尋找匹配1的txt檔案
1.txt
[root@linuxprobe test]# find [^1].txt  ## 提取沒有匹配1的單字元名稱的txt檔案
3.txt
a.txt
x.txt
[root@linuxprobe test]# find [a].txt ##同上
a.txt
[root@linuxprobe test]# find [^a].txt ## 尋找沒有匹配a的單字元名稱的txt檔案
1.txt
3.txt
x.txt

[
^xxx] : 限定為任意一個字元