深入理解Unix/Linux命令


創(chuàng)新互聯(lián)建站成立10年來,這條路我們正越走越好,積累了技術(shù)與客戶資源,形成了良好的口碑。為客戶提供網(wǎng)站設(shè)計、做網(wǎng)站、網(wǎng)站策劃、網(wǎng)頁設(shè)計、域名注冊、網(wǎng)絡(luò)營銷、VI設(shè)計、網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。網(wǎng)站是否美觀、功能強(qiáng)大、用戶體驗好、性價比高、打開快等等,這些對于網(wǎng)站建設(shè)都非常重要,創(chuàng)新互聯(lián)建站通過對建站技術(shù)性的掌握、對創(chuàng)意設(shè)計的研究為客戶提供一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進(jìn)步。

1. 命令的剖析

Unix 的命令由2部分組成,命令本身和附加的參數(shù)。例如 ls 命令,如果直接執(zhí)行 ls 命令,不帶附加參數(shù),那么默認(rèn)執(zhí)行的目標(biāo)即為當(dāng)前目錄,如下

[root@localhost /]$ls
bin?? dev?? etc?? lib??? media? opt?? root? sbin? sys? usr
boot? docs? home? lib64? mnt??? proc? run?? srv?? tmp? var

我們可以添加命令參數(shù),來使得它更加靈活, -l 參數(shù)可以使得結(jié)果集是以長結(jié)果顯示更多信息,后面的路徑可以指定命令執(zhí)行的目標(biāo)路徑

[root@localhost /]$ls -l /root
total 181328
-rw-------. 1 root root????? 1264 Aug 15 19:36 anaconda-ks.cfg
drwxr-xr-x. 3 root root??????? 18 Sep? 1 22:12 backups
-rw-r--r--. 1 root root????? 2381 Sep? 1 21:35 baidu.html
-rwxr-xr-x. 1 root root??????? 10 Sep? 1 05:57 cat.txt
drwxr-xr-x. 2 root root??????? 25 Sep? 1 21:43 docs
-r--r--r--. 2 root root??????? 17 Aug 18 12:18 file
-r--r--r--. 2 root root??????? 17 Aug 18 12:18 ha_link
-rw-r--r--. 1 root root??????? 49 Aug 18 12:14 hard_link
drwxr-xr-x. 7?? 10? 143?????? 245 Aug 26 11:44 jdk1.8
-rw-r--r--. 1 root root 185646832 Aug 26 11:30 jdk-8u181-linux-x64.tar.gz
-rw-r--r--. 1 root root???????? 0 Sep? 1 22:18 log.file
-rw-r--r--. 1 root root???????? 0 Sep? 1 23:36 log.fileat
-rwxr--r--. 1 root root?????? 114 Sep? 2 06:53 purple.sh
lrwxrwxrwx. 1 root root???????? 4 Aug 18 12:08 soft_link -> file
-rw-r--r--. 1 root root????? 1326 Aug 22 12:01 test.txt

2. 查找命令相關(guān)的信息

如果你從未使用過 ls 命令,該如何學(xué)習(xí)它如何使用呢?除了通過搜索引擎學(xué)習(xí),你還可以使用 man 來學(xué)習(xí)所有你想學(xué)習(xí)的指令。如下展示了 man ls 命令執(zhí)行后的輸出結(jié)果,由于篇幅過長,省略了后面部分,有興趣的朋友自行嘗試

[root@localhost /]$man ls


LS(1)??????????????????????????? User Commands?????????????????????????? LS(1)

NAME
?????? ls - list directory contents

SYNOPSIS
?????? ls [OPTION]... [FILE]...

DESCRIPTION
?????? List? information? about? the FILEs (the current directory by default).
?????? Sort entries alphabetically if none of -cftuvSUX nor --sort? is? speci‐
?????? fied.

?????? Mandatory? arguments? to? long? options are mandatory for short options
?????? too.

?????? -a, --all


GNU coreutils 8.22?????????????? November 2016?????????????????????????? LS(1)
Manual page ls(1) line 219/251 (END) (press h for help or q to quit)

從輸出中我們可以了解到該命令的主要功能和支持的所有參數(shù)。除了使用 man 來學(xué)習(xí)命令,我們還可以使用 info 來學(xué)習(xí)。例如命令行輸入 info ls:

[root@localhost /]$info ls

File: coreutils.info,? Node: ls invocation,? Next: dir invocation,? Up: Directory listin\
g

10.1 'ls': List directory contents
==================================

The 'ls' program lists information about files (of any type, including
directories).? Options and file arguments can be intermixed arbitrarily,
as usual.

?? For non-option command-line arguments that are directories, by
default 'ls' lists the contents of directories, not recursively, and
omitting files with names beginning with '.'.? For other non-option
arguments, by default 'ls' lists just the file name.? If no non-option
argument is specified, 'ls' operates on the current directory, acting as
if it had been invoked with a single argument of '.'.
? ...

該操作同樣會告訴你如何使用 ls 來高效化你的工作。


3. 修改命令

我們可以使用一些工具來增強(qiáng)命令的功能,例如元字符,輸入輸出重定向,管道,命令置換。


元字符

上面延時的 ls 命令輸出了很多文件,但是假如我們只需要列出,文件名末尾是 link 結(jié)尾的文件,可以如何操作呢?我們可以使用參數(shù) + 通配符來完成這個功能。

[root@localhost ~]$ls *link
ha_link? hard_link? soft_link

* 代表匹配文件名中的一個或多個字符。

? 匹配文件名中的任何一個字符。

[] 匹配包含在 [] 符號內(nèi)的某個字符即可。

[root@localhost ~]$ls *[link]
baidu.html? ha_link? hard_link? soft_link

如上匹配除了結(jié)尾最后一個字符是 l,i,n,k 任何一個字符的所有文件。


輸出,輸出重定向

上述我們操作的命令執(zhí)行結(jié)果都是直接輸出到了控制臺,但是假如我們的輸出結(jié)果很長,或者暫時有其他事情要做,結(jié)果需要等稍后去分析該怎么辦呢?這個時候我們可以將結(jié)果輸出重定向到到一個文件中,保存起來,稍后查看。

[root@localhost ~]$ls *[link] > links.txt
[root@localhost ~]$cat links.txt
baidu.html
ha_link
hard_link
soft_link

如上操作,我們便將文件列表的結(jié)果輸出到了 links.txt 文件中,以便稍后調(diào)查問題。


管道

剛剛我們查看的是用戶工作目錄下的文件,輸出很少,可以直接查看。但是當(dāng)我們查詢一個文件夾下面有很多文件的時候,輸出結(jié)果很長,此時一個屏幕都無法展示結(jié)果,查看并不方便。有沒有一種方法,可以讓我們先查看一部分結(jié)果,查看完畢后,按下一個鍵繼續(xù)查看下一頁呢?也許聰明的你會想到 more 或者 less 命令。但是這倆個命令是來查看文件的,此時管道可以幫助你。管道可以使得輸入輸出重定向,將一個命令的輸出作為另外一個命令的輸入。

[root@localhost ~]$ls /etc | more
adjtime
aliases
aliases.db
alternatives
anacrontab
asound.conf
at.deny
audisp
audit
bash_completion.d
bashrc
binfmt.d
centos-release
centos-release-upstream
--more--

如上,ls 的輸出結(jié)果,作為了 more 的輸入,這樣我們就可以優(yōu)哉游哉的慢慢查看 ls 的結(jié)果。當(dāng)然這里只是用 ls 和 more 來舉例,朋友們可以自己去探索其他的命令結(jié)合管道來使用,你會愛上它的。


命令置換

同樣,命令置換也可以達(dá)到像管道一樣的操作。它也將命令的輸出結(jié)果作為另外一個命令輸入。

[root@localhost ~]$ls ${pwd}
anaconda-ks.cfg? cat.txt? ha_link??? jdk-8u181-linux-x64.tar.gz? log.fileat? test.txt
backups????????? docs???? hard_link? links.txt?????????????????? purple.sh
baidu.html?????? file???? jdk1.8???? log.file??????????????????? soft_link

如上,我們將 pwd 命令的輸出(當(dāng)前工作目錄),作為 ls 命令的輸入。當(dāng)然該命令也可以用管道來實(shí)現(xiàn):pwd | ls,達(dá)到同樣的效果:

[root@localhost ~]$pwd | ls
anaconda-ks.cfg? cat.txt? ha_link??? jdk-8u181-linux-x64.tar.gz? log.fileat? test.txt
backups????????? docs???? hard_link? links.txt?????????????????? purple.sh
baidu.html?????? file???? jdk1.8???? log.file??????????????????? soft_link

實(shí)現(xiàn)的同樣的效果,但是實(shí)現(xiàn)原理不同。命令置換是通過在子 shell 中執(zhí)行結(jié)果,然后將結(jié)果返回到主 shell 中。而管道則一直在主 shell 中執(zhí)行。

—————END—————

喜歡本文的朋友們,歡迎長按下圖訂閱,收看更多精彩內(nèi)容

深入理解 Unix / Linux 命令

名稱欄目:深入理解Unix/Linux命令
轉(zhuǎn)載來源:http://muchs.cn/article6/pdjhig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、外貿(mào)網(wǎng)站建設(shè)云服務(wù)器、定制開發(fā)搜索引擎優(yōu)化、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)