Linux系統(tǒng)基礎(chǔ)(二)

什么是shell

shell 是一個(gè)命令語言解釋器(command-language interpreter)。 擁有自己內(nèi)建的 shell 命令集。此外,shell也能被系統(tǒng)中其他有效的Linux 實(shí)用程序和應(yīng)用程序(utilities and application programs)所調(diào)用。

不論何時(shí)你鍵入一個(gè)命令,它都被Linux shell所解釋。一些命令,比如打印當(dāng)前工作目錄命令(pwd), 是包含在Linux bash內(nèi)部的(就象DOS的內(nèi)部命令)。其他命令,比如拷貝命令(cp)和移動(dòng)命令(rm), 是存在于文件系統(tǒng)中某個(gè)目錄下的單獨(dú)的程序。而對(duì)用戶來說,你不知道(或者可能不關(guān)心) 一個(gè)命令是建立在shell內(nèi)部還是一個(gè)單獨(dú)的程序。

內(nèi)部命令與外部命令

文件管理類命令

1,ls ? ? ? 查看文件和目錄

語法: ls [-aAdfFhilRS] [文件或目錄]

常用選項(xiàng):

-a:顯示所有文件,包括隱藏文件(以?.?開頭的文件)

-l:詳細(xì)列出文件的屬性等信息

-d:僅列出目錄本身,而不是列出目錄內(nèi)的文件數(shù)據(jù)

范例:

[root@foundation0 www]# cd /tmp/

[root@foundation0 tmp]# ls ? ? ? ? ? ? ? ? ? ?#查看目錄/tmp中的文件

test1? test2

[root@foundation0 tmp]# ls -a ? ? ? ? ? ? ? ?#查看目錄/tmp中的所有文件,包括隱藏文件

.? ..? .esd-1000? .font-unix? .ICE-unix? test1? test2? .Test-unix? .X0-lock? .X11-unix? .XIM-unix

[root@foundation0 tmp]# ls -l ? ? ? ? ? ? ? #查看目錄中的文件的詳細(xì)信息

total 0

drwxr-xr-x. 2 root root? 6 Sep 24 16:33 test1

drwxr-xr-x. 3 root root 18 Sep 24 16:33 test2

[root@foundation0 tmp]# ls -l -d ? ? ? ? ? ? ? #查看本目錄/tmp的詳細(xì)信息 ,可以簡寫成ls -ld

drwxrwxrwt. 12 root root 4096 Sep 24 16:56 .

2,cd ? ? ?切換目錄

常用的一些特殊目錄:

.? ? ? ? 代表當(dāng)前目錄

..?????? 代表上一層目錄

- ? ? ? ?代表前一個(gè)工作目錄

~ ? ? ? 代表【目前用戶身份】所在的自家目錄

~account??代表?account?這個(gè)用戶的自家家目錄

范例:

[root@foundation0 ~]# cd /home/ ? ?# 進(jìn)入/home目錄

[root@foundation0 home]# cd . ? ? ? # .表示當(dāng)前目錄,所以還是在home目錄

[root@foundation0 home]# cd ..? ? ? # ..表示上一層目錄,所以此時(shí)就進(jìn)入到了/目錄下

[root@foundation0 /]# cd ~ ? ? ? ? ?# ~表示當(dāng)前用戶的家目錄,當(dāng)前用戶是root,所以進(jìn)入目錄/root/

[root@foundation0 ~]# cd ~xcp ? ? ?# ~account代表account這個(gè)用戶的自家家目錄,所以進(jìn)入用戶xcp的家目錄/home/xcp/

[root@foundation0 xcp]# pwd ? ? ? #當(dāng)前確實(shí)位于目錄/home/xcp/

/home/xcp

[root@foundation0 xcp]# cd - ? ? ?# -表示前一個(gè)工作目錄,我們之前位于目錄/root/下,所以就進(jìn)入了目錄/root/

[root@foundation0 ~]# cd ../var/ ? ?#切換目錄時(shí)可以寫絕對(duì)路徑也可以寫相對(duì)路徑

[root@foundation0 home]# pwd

/var

3,pwd ? ? ? 顯示當(dāng)前所在目錄

選項(xiàng):-P: 顯示出真實(shí)路徑,而非使用鏈接(link)路徑。(大寫的參數(shù)P)

范例:

[root@foundation0 home]# pwd ? ? ? ? ? ?#顯示當(dāng)前目錄

/home

[root@foundation0 home]# cd /var/mail/

[root@foundation0 mail]# pwd ? ? ? ? ? ?#顯示當(dāng)前所在目錄是/var/mail

/var/mail

[root@foundation0 mail]# pwd -P ? ? ? ?#加了選項(xiàng)-P結(jié)果卻不一樣了

/var/spool/mail

[root@foundation0 mail]# ls -l /var/mail ? ? ? ? ? ? ? ?#其實(shí)是因?yàn)?var/mail是一個(gè)鏈接文件,鏈接到/varspool/mail,所以加上-P就會(huì)顯示真實(shí)目錄

lrwxrwxrwx. 1 root root 10 Sep5 03:22 /var/mail -> spool/mail

4,touch ? ? ?創(chuàng)建空文件

范例:

[root@foundation0 tmp]# touch 1.txt 2.txt 3.txt

[root@foundation0 tmp]# ls

1.txt? 2.txt? 3.txt

[root@foundation0 tmp]# ls -l

total 0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #這一行表示列出的文件大小之和,0表示全都是空文件。

-rw-r--r--. 1 root root 0 Sep 24 17:06 1.txt

-rw-r--r--. 1 root root 0 Sep 24 17:06 2.txt

-rw-r--r--. 1 root root 0 Sep 24 17:06 3.txt

[root@foundation0 tmp]#

5,mkdir ? ? ? ? 創(chuàng)建新目錄

選項(xiàng): -p:如果不存在父級(jí)目錄,則按照需要遞歸創(chuàng)建目錄,如果父級(jí)目錄存在則也不報(bào)錯(cuò)。

范例:

[root@foundation0 ~]# cd /tmp/

[root@foundation0 tmp]# rm -rf *

[root@foundation0 tmp]# mkdir test1 ? ? ? ? ?#創(chuàng)建一個(gè)新目錄

[root@foundation0 tmp]# mkdir test2/test3 ? ? ? #遞歸創(chuàng)建兩個(gè)目錄,結(jié)果系統(tǒng)提示沒有目錄test2

mkdir: cannot create directory ‘test2/test3’: No such file or directory

[root@foundation0 tmp]# mkdir -p test2/test3 ? ? ? #加了選項(xiàng)-p成功創(chuàng)建這兩個(gè)目錄

[root@foundation0 tmp]# ls

test1? test2

[root@foundation0 tmp]# ls test2

test3

6,cp ? ? 復(fù)制文件或目錄

語法:cp [-adfilprsu] ?source(源文件或目錄) destination(目的文件或目錄)

or:cp [options] source1 source2 source3 ..... directory(目的目錄) ??

常用選項(xiàng):

-a? :相當(dāng)于 -pdr,歸檔備份(保存文檔的所有原屬性)

-f? :為強(qiáng)制 (force) 的意思,若有重復(fù)或其他疑問時(shí),不會(huì)詢問使用者,而強(qiáng)制復(fù)制

-i? :若目的文件(destination)已經(jīng)存在時(shí),在覆蓋時(shí)會(huì)先詢問是否真的覆蓋

-p? :連同文件的屬性一起復(fù)制過去,而非使用默認(rèn)屬性

-r? :遞歸持續(xù)復(fù)制,用于目錄的復(fù)制行為

范例:

[root@foundation0 tmp]# cp /var/log/wtmp wtmp

[root@foundation0 tmp]# ls -l /var/log/wtmp wtmp

-rw-rw-r--. 1 root utmp 23424 Sep 24 15:41 /var/log/wtmp

-rw-r--r--. 1 root root 23424 Sep 24 17:30 wtmp

[root@foundation0 tmp]#

#在不加任何參數(shù)的情況下,文件的所屬者會(huì)改變,權(quán)限也跟著改變了,連文件建立的時(shí)間也不一樣了! 如果想要將文件的所有特性都一起復(fù)制過來,加上 選項(xiàng)-a 即可。

[root@foundation0 tmp]# cp /root/.bashrc bashrc

[root@foundation0 tmp]# cp -i /root/.bashrc bashrc

cp: overwrite ‘bashrc’? y

# 重復(fù)做兩次拷貝,由于 /tmp 底下已經(jīng)存在 bashrc 了,加上 選項(xiàng)-i ,則在覆蓋前會(huì)詢問使用者是否確定!可以按下 n 或者 y 。反過來說,如果不想要詢問時(shí),則加上 -f 來強(qiáng)制直接覆蓋。

[root@foundation0 tmp]# cp /root/ /tmp/

cp: omitting directory ‘/root/’?

[root@foundation0 tmp]# cp -r /root/ /tmp/

[root@foundation0 tmp]# ls

1.txt? 2.txt? 3.txt? bashrc? root? wtmp

#如果我們復(fù)制的是目錄,此時(shí)必須要加上 -r 這個(gè)選項(xiàng)才行

7,mv ? 移動(dòng)文件與目錄、文件重命名

范例:

[root@foundation0 ~]# cp .bashrc /tmp/bashrc

[root@foundation0 ~]# cd /tmp/

[root@foundation0 tmp]# mv bashrc test1/bashrc ? ?#將文件bashrc移動(dòng)到目錄/tmp/test1/中去

[root@foundation0 tmp]# cd test1

[root@foundation0 test1]# mv bashrc bashrc.bak ? ?#將文件bashrc更名為bashrc.bak

[root@foundation0 test1]# ls

bashrc.bak

[root@foundation0 test1]#

#其實(shí)在 Linux 底下還有個(gè)命令 rename ,該命令就是專門用來對(duì)文件或者目錄進(jìn)行重命名的??梢詤㈤?man rename 了解其更多的用法。

8,rm ? ? ? ?刪除文件或目錄

常用選項(xiàng):

-f? :就是 force 的意思,強(qiáng)制移除

-i? :互動(dòng)模式,在刪除前會(huì)詢問使用者是否動(dòng)作

-r? :遞歸刪除,常用在目錄的刪除

范例:

[root@foundation0 tmp]# rm ?test2 ? ? ? #刪除目錄必須要加選項(xiàng)-r

rm: cannot remove ‘test2’: Is a directory

[root@foundation0 tmp]# rm -ir test2? ? ? #刪除時(shí)會(huì)詢問是否確認(rèn)刪除

rm: remove directory ‘test2’? y

[root@foundation0 tmp]# rm -rf test1/ ? #加了選項(xiàng)-f直接強(qiáng)制刪除目錄以及里面所有文件

#其實(shí)rm命令系統(tǒng)內(nèi)置了別名相當(dāng)于‘rm -i’,所以就算不加選項(xiàng)-i系統(tǒng)也會(huì)提示再次確認(rèn),關(guān)于別名的用法在后面會(huì)進(jìn)行說明。此外在實(shí)際環(huán)境中要習(xí)慣不用命令‘rm -rf’來刪除文件,以防錯(cuò)寫目錄或者文件路徑?jīng)]有寫全導(dǎo)致系統(tǒng)文件被誤刪。


Bash的工作特性

Bash,全稱為Bourne-Again Shell。它是一個(gè)命令處理器,屬于shell的一種,通常運(yùn)行于文本窗口中,并能執(zhí)行用戶直接輸入的命令。

關(guān)于Bash的工作特性,主要有以下幾點(diǎn):

1,命令執(zhí)行狀態(tài)返回值

在我們每次輸入命令之后,系統(tǒng)都會(huì)把命令執(zhí)行的狀態(tài)結(jié)果保存在一個(gè)變量中,我們稱這個(gè)變量是特殊變量,用$?來表示。

范例:

[root@foundation0 tmp]# mkdir test1

[root@foundation0 tmp]# rm test1/

rm: cannot remove ‘test1/’: Is a directory

[root@foundation0 tmp]# echo $? ? ? ?

1 ? ? ? ? ? ? ? ? ? ? ? ? ? # 非0表示執(zhí)行過程中出現(xiàn)異?;蚍钦M顺?,范圍是1-255

[root@foundation0 tmp]# touch 1.txt

[root@foundation0 tmp]# echo $?

0 ? ? ? ? ? ? ? ? ? ? ? ? ? # 0表示執(zhí)行成功

[root@foundation0 tmp]#

2,命令行展開

~ 和 {}

~ ?:展開為對(duì)應(yīng)用戶的家目錄。其實(shí)前面我們已經(jīng)使用過,管理員root通過cd ~xcp進(jìn)入到用戶xcp的家目錄

{}? :在非引號(hào)內(nèi)的內(nèi)容中,如果用花括號(hào)包括,而且里面用逗號(hào)分隔(至少包含一個(gè)逗號(hào),可以是空內(nèi)容),這樣花括號(hào)里的內(nèi)容會(huì)被展開成用空格分開的一個(gè)列表,花括號(hào)前后可以緊隨前綴和后綴(前后綴都是可選的)。

范例:

(1)在/tmp目錄下創(chuàng)建文件a_c , a_d,b_c,b_d:

[root@foundation0 tmp]# touch {a,b}_{c,d}

[root@foundation0 tmp]# ls

a_c? a_d? b_c? b_d

[root@foundation0 tmp]#

(2)在/tmp/mylinux目錄下創(chuàng)建目錄樹:

目錄樹結(jié)構(gòu)圖

通過命令行展開一步創(chuàng)建:

通過命令行展開創(chuàng)建

3,別名機(jī)制

(1)alias:命令alias可以直接查看當(dāng)前系統(tǒng)定義的所有別名條目。

范例:

[root@foundation0 ~]# alias

alias cp='cp -i'

alias l.='ls -d .* --color=auto'

alias ll='ls -l --color=auto'

alias mv='mv -i'

alias rm='rm -i'

[root@foundation0 ~]#

(2)alias 別名="原始命令 [選項(xiàng)]"? :定義一項(xiàng)別名,選項(xiàng)可加可不加。

如需要使其永久生效可寫入局部配置文件中:~/.bashrc(每個(gè)用戶可自定義)

范例:

別名ipconfig

(3)unalias 別名 ? :撤銷自定義的別名

范例:

[root@foundation0 ~]# alias ipconfig

alias ipconfig='ifconfig eno16777736'

[root@foundation0 ~]# unalias ipconfig

[root@foundation0 ~]# alias ipconfig

-bash: alias: ipconfig: not found

[root@foundation0 ~]#

4,shell引用

'? ':強(qiáng)引用,變量替換不會(huì)進(jìn)行

" ":弱引用,能夠執(zhí)行變量替換

` `:命令替換,引用命令的執(zhí)行結(jié)果

$(命令):同樣可以實(shí)現(xiàn)命令替換

范例:

[root@foundation0 ~]# echo '$PATH' ? ? ? ? ? ? ?# 強(qiáng)引用,變量沒有被替換

$PATH

[root@foundation0 ~]# echo "$PATH" ? ? ? ? ? ?# 弱引用,執(zhí)行變量替換

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

[root@foundation0 ~]# echo `which ls` ? ? ? ? ?# 命令echo引用了``里的命令結(jié)果

alias ls='ls --color=auto' /usr/bin/ls

[root@foundation0 ~]# echo $(which touch) ? ?#命令echo引用了$()里的命令結(jié)果

/usr/bin/touch

[root@foundation0 ~]#

5,Bash補(bǔ)全

Bash補(bǔ)全的按鍵是<Tab>,可以實(shí)現(xiàn)命令與路徑的補(bǔ)全。

注:參數(shù)的補(bǔ)全并非Bash自帶功能,而是由/etc/bash_completion.d/下對(duì)應(yīng)的腳本實(shí)現(xiàn)的。

命令補(bǔ)全

shell程序在接收到用戶執(zhí)行命令的請(qǐng)求且分析完成之后,最左側(cè)字符串將被當(dāng)作命令去查找;

查找機(jī)制:

(1) 查找內(nèi)部命令;

(2) 查找外部命令:

<1> ?去$PATH變量所指定的各路徑下,自左而右逐個(gè)搜索各目錄下的文件名;

<2> ?給定的打頭的字符串如果能唯一標(biāo)識(shí)某命令程序文件的文件名,則直接補(bǔ)全;

<3> ?不能惟一標(biāo)識(shí),再擊tab可給列表;

<4> ?錯(cuò)誤:沒有任何命令可被此打頭字符串標(biāo)識(shí)。

范例:

[root@foundation0 ~]# firewall-<Tab><Tab>? ? #不能惟一標(biāo)識(shí),再擊tab可給列表

firewall-cmd? ? ? ? ? firewall-config? ? ? firewall-offline-cmd

[root@foundation0 ~]# firewall-

路徑補(bǔ)全

在起始路徑的上級(jí)目錄下,以對(duì)應(yīng)路徑下的打頭字符串來逐一匹配上級(jí)目錄下的每個(gè)文件:

惟一標(biāo)識(shí):tab補(bǔ)全;

不能惟一標(biāo)識(shí):tab, tab給出列表;

錯(cuò)誤路徑:沒有響應(yīng)。

范例:

[root@foundation0 ~]#ls /t<Tab> ? ? ? ? ? # 唯一補(bǔ)全結(jié)果會(huì)立即補(bǔ)全

[root@foundation0 ~]#ls /tmp/

[root@foundation0 ~]# vim /etc/sys<Tab><Tab> ? # 不能唯一標(biāo)識(shí)結(jié)果需要按兩下<Tab>給出列表

sysconfig/? ? ? ? ? sysctl.d/? ? ? ? ? system-release

sysctl.conf? ? ? ? systemd/? ? ? ? ? ? system-release-cpe

[root@foundation0 ~]# vim /etc/sys

Bash除了以上5條特性之外還有很多特性,例如命令歷史,Globbing,文件名通配,命令行快捷鍵,提供編程環(huán)境,Bash變量,輸入輸出重定向等等。


文件的元數(shù)據(jù)信息

linux系統(tǒng)中文件是由3個(gè)部分組成:

1,文件名——directory entry(也叫目錄項(xiàng))

2,數(shù)據(jù)——data(指的是文件中編輯保存的內(nèi)容)

3,元數(shù)據(jù)——matedata(當(dāng)你用ls -l命令列出的信息就是元數(shù)據(jù),在linux、unix系統(tǒng)中,所有與文件相關(guān)的元數(shù)據(jù)都保存在一個(gè)叫做inode的結(jié)構(gòu)中)

通過ls -l(或者ll)命令列出的信息有文件的類型、所有者的身份與對(duì)應(yīng)的權(quán)限、鏈接數(shù)量、文件最后的ctime等等。

范例:

[root@foundation0 tmp]# ll 1.txt

-rw-r--r--. 1 root root 115 Sep 24 19:46 1.txt

對(duì)于文件1.txt:

第一列是-rw-r--r--, 其中第一個(gè)字符是"-",在縮寫表中對(duì)應(yīng)常規(guī)文件。后面的九個(gè)字符“rw-r--r--”表 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?示權(quán)限;

第二列是1 ? ? ? ? ? ? ?表示鏈接數(shù),如果有硬鏈接到這個(gè)文件,這里的數(shù)值會(huì)+1,刪除硬連接這里 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?會(huì)-1;

第三列是root ? ? ? ? 表示文件所有者是root;

第四列是root ? ? ? ? 表示文件所有組是root;

第五列是115 ? ? ? ? ? 表示文件占用115字節(jié)

第六列是Sep 24 19:46 ?默認(rèn)表示文件最后的ctime, change-time

第七列是1.txt ? 表示文件名

此外,我們還可以使用stat命令來查看文件更多的元數(shù)據(jù)信息:

[root@foundation0 tmp]# stat 1.txt

File: ‘1.txt’

Size: 115 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Blocks: 8 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?IO Block: 4096? regular file

Device: 801h/2049d ? ? ? ? ? ? ?Inode: 1093845547 ? ? ? ? ? ? Links: 1

Access: (0644/-rw-r--r--) ? ? ? ? ? ?Uid: (? ? 0/? ? root) ? ? ? ? ? ? ? ?Gid: (? ? 0/? ? root)

Context: unconfined_u:object_r:user_tmp_t:s0

Access: 2016-09-24 19:46:21.368240354 +0800

Modify: 2016-09-24 19:46:19.162240439 +0800

Change: 2016-09-24 19:46:19.162240439 +0800

Birth: -

[root@foundation0 tmp]#

對(duì)各項(xiàng)元數(shù)據(jù)的解釋:

Size? 表示文件的理論長度,單位是字節(jié)

Block 與 IO Block 的乘積是文件所占的實(shí)際大小,在linux下文件所占的空間分配,最小的單位是塊(Bolck),而塊的大小與塊的數(shù)量,決定了文件實(shí)際占用的磁盤空間.

Device: 表示內(nèi)核對(duì)該設(shè)備的編號(hào)

Inode: ?是內(nèi)核為每一個(gè)文件分配的標(biāo)志

Links: ? 表示文件名指向的inode節(jié)點(diǎn)的數(shù)量

Access:(0755/-rwxr-xr-x) 表示了訪問權(quán)限,以及文件類型

uid: ? ? ?表示了文件所有者,包括了系統(tǒng)為所有者分配的數(shù)值id

gid: ? ? ?表示了文件組,包括了系統(tǒng)為組分配的數(shù)值id

Context:環(huán)境,也叫上下文關(guān)系,在某些目錄下至關(guān)重要,決定了該文件是否能被系統(tǒng)識(shí)別使用

最后就是三個(gè)時(shí)間戳了:

Access time: 訪問時(shí)間,簡寫為atime,文件數(shù)據(jù)每次被閱讀后所記錄的時(shí)間

Modify time: 修改時(shí)間,mtime,文件內(nèi)容數(shù)據(jù)被修改后記錄的時(shí)間

Change time: 改動(dòng)時(shí)間,ctime,文件的inode節(jié)點(diǎn)信息被改變后記錄的時(shí)間

如何修改時(shí)間戳呢,可以用touch命令來實(shí)現(xiàn):

語法:touch [options] file

選項(xiàng):

-a:僅修改access time;

-m:僅修改modify?time;

-t?STAMP:指定一個(gè)任意時(shí)間

范例:

[root@foundation0 tmp]# touch 1.txt ? ? ? ? ? ? ? ? ? #不加任何參數(shù)表示把三個(gè)時(shí)間戳都改成當(dāng)前時(shí)間

[root@foundation0 tmp]# stat 1.txt ? ? ? ? ? ? ? ? ? ? #三個(gè)時(shí)間都變成了當(dāng)前時(shí)間

File: ‘1.txt’

Size: 115? ? ? Blocks: 8? ? ? ? ? IO Block: 4096? regular file

Device: 801h/2049d Inode: 1093845547? Links: 1

Access: (0644/-rw-r--r--)? Uid: (? ? 0/? ? root)? Gid: (? ? 0/? ? root)

Context: unconfined_u:object_r:user_tmp_t:s0

Access: 2016-09-24 23:31:27.303718903 +0800

Modify: 2016-09-24 23:31:27.303718903 +0800

Change: 2016-09-24 23:31:27.303718903 +0800

Birth: -

[root@foundation0 tmp]# touch -a 1.txt? ? ? ? ? ? #使用-a參數(shù),修改訪問時(shí)間

[root@foundation0 tmp]# touch -m -t 201608011001.20 1.txt? ? #使用-m -t把時(shí)間改為指定的時(shí)間

[root@foundation0 tmp]# stat 1.txt ? ? ? ? ? ? ? ? ? #mtime已更改為上面指定的時(shí)間

File: ‘1.txt’

Size: 115? ? ? Blocks: 8? ? ? ? ? IO Block: 4096? regular file

Device: 801h/2049d Inode: 1093845547? Links: 1

Access: (0644/-rw-r--r--)? Uid: (? ? 0/? ? root)? Gid: (? ? 0/? ? root)

Context: unconfined_u:object_r:user_tmp_t:s0

Access: 2016-09-24 23:32:21.721716802 +0800

Modify: 2016-08-01 10:01:20.000000000 +0800

Change: 2016-09-24 23:33:53.905713243 +0800

Birth: -

[root@foundation0 tmp]#


命令練習(xí):

1,顯示/var目錄下所有以l開頭,以一個(gè)小寫字母結(jié)尾,且中間至少出現(xiàn)一位數(shù)字(可以有其它字符)的文件或目錄:

[root@foundation0 tmp]# touch /var/like123321like

[root@foundation0 tmp]# ls -d /var/l*[[:digit:]]*[[:lower:]]

/var/like123321like

或者

[root@foundation0 tmp]# ls -d /var/l*[0-9]*[a-z]

/var/like123321like

2,顯示/etc目錄下 以任意一個(gè)數(shù)字開頭 且以非數(shù)字結(jié)尾的文件或目錄:

[root@foundation0 tmp]# touch /etc/{1..3}.txt

[root@foundation0 tmp]# mkdir /etc/{4..5}note

[root@foundation0 tmp]# ls -d /etc/[0-9]*[^0-9]

/etc/1.txt? /etc/2.txt? /etc/3.txt? /etc/4note? /etc/5note

3,顯示/etc目錄下,以非字母開頭 后面跟了一個(gè)字母以及其他任意長度任意字符的文件或目錄:

[root@foundation0 tmp]# ls -ld /etc/[^[:alpha:]][[:alpha:]]*

drwxr-xr-x. 2 root root 6 Sep 25 00:19 /etc/4note

drwxr-xr-x. 2 root root 6 Sep 25 00:19 /etc/5note

或者

[root@foundation0 tmp]# ls -dl /etc/[^a-z][a-z]*?

drwxr-xr-x. 2 root root 6 Sep 25 00:19 /etc/4note

drwxr-xr-x. 2 root root 6 Sep 25 00:19 /etc/5note

[root@foundation0 tmp]# mkdir /etc/4NOTE ? ? ? ? ?#新創(chuàng)建一個(gè)第二個(gè)字符是大寫字母的目錄

[root@foundation0 tmp]# ls -dl /etc/[^a-z][a-z]* ? ? ?#不管是[a-z]還是[A-Z],結(jié)果并不區(qū)分大小寫

drwxr-xr-x. 2 root root 6 Sep 25 00:19 /etc/4note ??

drwxr-xr-x. 2 root root 6 Sep 25 00:37 /etc/4NOTE

drwxr-xr-x. 2 root root 6 Sep 25 00:19 /etc/5note

4,在/tmp 目錄下創(chuàng)建以tfile 開頭,后跟當(dāng)前日期和時(shí)間的文件,文件名形如:tfile-2016-05-27-09-32-22:

[root@foundation0 tmp]# touch tfile-`date +%Y-%m-%d-%H-%M-%S`

[root@foundation0 tmp]# ls tfile*

tfile-2016-09-25-00-53-04

5,復(fù)制/etc 目錄下所有以p 開頭 以非數(shù)字結(jié)尾的文件或目錄到/tmp/mytest1目錄中:

[root@foundation0 tmp]# mkdir mytest1 ? #復(fù)制多個(gè)文件到目錄時(shí),該目錄必須已經(jīng)存在。

[root@foundation0 tmp]# cp -r /etc/p*[^0-9] mytest1/

[root@foundation0 tmp]# ls !$

ls mytest1/

pam.d? passwd-? pki? ? ? pm? ? ? postfix? prelink.conf.d? profile? ? protocols? purple

passwd? pinforc? plymouth? popt.d? ppp? ? ? printcap? ? ? ? profile.d? pulse? ? ? python

6,復(fù)制/etc目錄下所有以.d結(jié)尾的文件或目錄至/tmp/mytest2目錄中:

[root@foundation0 tmp]# mkdir mytest2

[root@foundation0 tmp]# cp -r /etc/*.d mytest2

7,復(fù)制/etc目錄下所有以.d結(jié)尾的文件或目錄至/tmp/mytest2目錄中

[root@foundation0 tmp]# mkdir mytest3

[root@foundation0 tmp]# cp -r /etc/[l,m,n]*.conf /tmp/mytest3


附注:(常用通配符)

* :匹配任意長度的任意字符

?: ?匹配任意單個(gè)字符

[ ]:匹配指定字符范圍內(nèi)的任意單個(gè)字符

[a-z] ?不區(qū)分大小寫 ?<--->[[:lower:]] 所有小寫字母

[A-Z] 不區(qū)分大小寫 ?<--->[[:upper:]] 所有大寫字母

[0-9] ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[[:digit:]] 所有數(shù)字

[a-z0-9A-Z] ? ? ? ? ? ? ? ? ? ? ? ? ?[[:alnum:]] 所有字母和數(shù)字

[[:alpha:]] 所有大小寫字符

[[:space:]] 空白字符

[[:punct:]] 特殊符號(hào)、標(biāo)點(diǎn)符號(hào)

[^]:匹配指定字符范圍外的任意單個(gè)字符;

如:非數(shù)字:[^0-9]


參考文檔

Bash——維基百科

Linux基礎(chǔ)概要——GitBook


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容