linux下的通配符與特殊符號

在bash的操作環境中海油一個非常有用的功能,那就是通配符(wildcard)。我們呢利用bash處理出具就更加方便了。下面我們列出一些常用的通配符,

符號 意義
* 代表0個到無窮多個任意字符
代表一定有一個任意字符
[] 同樣代表一定有一個在中括號內的字符(非任意字符)。例如[abcd]代表有一個字符,可能是a,b,c,d這四個的任何一個
[-] 若有減號在括號內時,代表在編碼順序內的所有字符,例如[0-9]代表0到9之間的所有數字,因爲數字的語系編碼是連續的
[^] 若中括號內的第一個字符爲指數符號(^),那表示原向選擇,例如[^abc]代表一個有一個字符,只要是非a,b,c的其他字符就接收的意思
接下來看一個例子:

root@tian-desktop:~# ll -d /etc/cron*
drwxr-xr-x 2 root root 4096 Apr 29  2010 /etc/cron.d/
drwxr-xr-x 2 root root 4096 Apr 29  2010 /etc/cron.daily/
drwxr-xr-x 2 root root 4096 Apr 29  2010 /etc/cron.hourly/
drwxr-xr-x 2 root root 4096 Apr 29  2010 /etc/cron.monthly/
drwxr-xr-x 2 root root 4096 Jun  2  2014 /etc/cron.weekly/
-rw-r--r-- 1 root root  724 Apr 15  2010 /etc/crontab

root@tian-desktop:~# ll -d /etc/?????
drwxr-xr-x 3 root root 4096 Apr 29  2010 /etc/avahi/
drwxr-xr-x 2 root root 4096 Apr 29  2010 /etc/byobu/
drwxr-xr-x 4 root root 4096 Apr 29  2010 /etc/dhcp3/
drwxr-xr-x 3 root root 4096 Jun  2  2014 /etc/emacs/
drwxr-xr-x 4 root root 4096 Apr 29  2010 /etc/fonts/
-rw-r--r-- 1 root root  756 Jun  2  2014 /etc/fstab
drwxr-xr-x 2 root root 4096 Apr 29  2010 /etc/gamin/
drwxr-xr-x 6 root root 4096 Apr 29  2010 /etc/gconf/
drwxr-xr-x 3 root root 4096 Apr 29  2010 /etc/gnome/
drwxr-xr-x 2 root root 4096 Apr 29  2010 /etc/gre.d/
drwxr-xr-x 2 root root 4096 Apr 29  2010 /etc/groff/
-rw-r--r-- 1 root root  868 May 18  2014 /etc/group
-rw-r--r-- 1 root root  258 May 12  2014 /etc/hosts
-rw-r--r-- 1 root root   24 Apr 23  2010 /etc/issue
-rw-r--r-- 1 root root  267 Apr 23  2010 /etc/legal
-rw-r--r-- 1 root root  111 Apr  1  2010 /etc/magic
drwxr-xr-x 3 root root 4096 Oct 27 14:14 /etc/mysql/
drwxr-xr-x 2 root root 4096 Jun  2  2014 /etc/pam.d/
drwxr-xr-x 2 root root 4096 Apr 29  2010 /etc/pango/
drwxr-xr-x 2 root root 4096 Jun  2  2014 /etc/pulse/
drwxr-xr-x 2 root root 4096 Jun  2  2014 /etc/rc0.d/
drwxr-xr-x 2 root root 4096 Jun  2  2014 /etc/rc1.d/
drwxr-xr-x 2 root root 4096 Jun  2  2014 /etc/rc2.d/
drwxr-xr-x 2 root root 4096 Jun  2  2014 /etc/rc3.d/
drwxr-xr-x 2 root root 4096 Jun  2  2014 /etc/rc4.d/
drwxr-xr-x 2 root root 4096 Jun  2  2014 /etc/rc5.d/
drwxr-xr-x 2 root root 4096 Jun  2  2014 /etc/rc6.d/
drwxr-xr-x 2 root root 4096 Jun  2  2014 /etc/rcS.d/
drwxr-xr-x 2 root root 4096 Apr 29  2010 /etc/samba/
drwxr-xr-x 3 root root 4096 Apr 29  2010 /etc/sound/

root@tian-desktop:~# ll -d /etc/*[0-9]*
drwxr-xr-x 10 root root 4096 Apr 29  2010 /etc/X11/
drwxr-xr-x  5 root root 4096 Apr 29  2010 /etc/dbus-1/
drwxr-xr-x  4 root root 4096 Apr 29  2010 /etc/dhcp3/
drwxr-xr-x  3 root root 4096 Apr 29  2010 /etc/firefox-3.0/
drwxr-xr-x  3 root root 4096 Apr 29  2010 /etc/gnome-vfs-2.0/
drwxr-xr-x  2 root root 4096 Jun  2  2014 /etc/gtk-2.0/
drwxr-xr-x  2 root root 4096 Apr 29  2010 /etc/iproute2/
-rw-r--r--  1 root root  801 Apr 19  2010 /etc/mke2fs.conf
-rw-r--r--  1 root root 7649 Apr 29  2010 /etc/pnm2ppa.conf
drwxr-xr-x  5 root root 4096 Apr 29  2010 /etc/polkit-1/
drwxr-xr-x  2 root root 4096 Apr 29  2010 /etc/python2.6/
drwxr-xr-x  2 root root 4096 Jun  2  2014 /etc/rc0.d/
drwxr-xr-x  2 root root 4096 Jun  2  2014 /etc/rc1.d/
drwxr-xr-x  2 root root 4096 Jun  2  2014 /etc/rc2.d/
drwxr-xr-x  2 root root 4096 Jun  2  2014 /etc/rc3.d/
drwxr-xr-x  2 root root 4096 Jun  2  2014 /etc/rc4.d/
drwxr-xr-x  2 root root 4096 Jun  2  2014 /etc/rc5.d/
drwxr-xr-x  2 root root 4096 Jun  2  2014 /etc/rc6.d/
-rw-r--r--  1 root root 8596 Feb 16  2010 /etc/sensors3.conf
drwxr-xr-x  2 root root 4096 Apr 29  2010 /etc/w3m/
drwxr-xr-x  2 root root 4096 Apr 29  2010 /etc/xulrunner-1.9.2/

root@tian-desktop:~# ll -d /etc/[^a-z]*
drwxr-xr-x  5 root root 4096 Apr 29  2010 /etc/ConsoleKit/
drwxr-xr-x  5 root root 4096 Apr 29  2010 /etc/NetworkManager/
drwxr-xr-x 10 root root 4096 Apr 29  2010 /etc/X11/
除了通配符之外,bash環境中的特殊字符有還有:

# 批註符號,這個最常被用在script當中,視爲說明,其後的數據均不執行
\ 轉義符號,將“特殊字符或通配符”還原成一般字符
| 管道(pipe),分隔兩個管道命令的界定
; 連續命令執行分隔符,連續性命令的界定
~ 用戶的主文件夾
$ 使用變量前導符,即是變量之前需要加的變量替換
! 邏輯運算意義上的“非”的意思
& 作業控制,將命令變成背景下作業
/ 目錄符號,路徑分隔的符號
> 數據流重定向,輸出導向,“替換”
< 數據流重定向,輸入導向
' ' 單引號,不具有變量置換的功能
"" 雙引號,具有變量置換的功能
`` 兩個"`"中間爲可以先執行的命令,也可使用$()代替
() 在中間爲子shell的起始於結束
{} 在中間爲命令塊的組合
   


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章