深入理解 Unix / Linux 命令
1. 命令的剖析
Unix 的命令由2部分組成,命令本身和附加的參數。例如 ls 命令,如果直接執行 ls 命令,不帶附加參數,那么默認執行的目標即為當前目錄,如下
[root@localhost /]$lsbin?? dev?? etc?? lib??? media? opt?? root? sbin? sys? usr
boot? docs? home? lib64? mnt??? proc? run?? srv?? tmp? var
我們可以添加命令參數,來使得它更加靈活, -l 參數可以使得結果集是以長結果顯示更多信息,后面的路徑可以指定命令執行的目標路徑
[root@localhost /]$ls -l /roottotal 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. 查找命令相關的信息
如果你從未使用過 ls 命令,該如何學習它如何使用呢?除了通過搜索引擎學習,你還可以使用 man 來學習所有你想學習的指令。如下展示了 man ls 命令執行后的輸出結果,由于篇幅過長,省略了后面部分,有興趣的朋友自行嘗試
[root@localhost /]$man lsLS(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)
從輸出中我們可以了解到該命令的主要功能和支持的所有參數。除了使用 man 來學習命令,我們還可以使用 info 來學習。例如命令行輸入 info ls:
[root@localhost /]$info lsFile: 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. 修改命令
我們可以使用一些工具來增強命令的功能,例如元字符,輸入輸出重定向,管道,命令置換。
元字符
上面延時的 ls 命令輸出了很多文件,但是假如我們只需要列出,文件名末尾是 link 結尾的文件,可以如何操作呢?我們可以使用參數 + 通配符來完成這個功能。
[root@localhost ~]$ls *linkha_link? hard_link? soft_link
* 代表匹配文件名中的一個或多個字符。
? 匹配文件名中的任何一個字符。
[] 匹配包含在 [] 符號內的某個字符即可。
[root@localhost ~]$ls *[link]baidu.html? ha_link? hard_link? soft_link
如上匹配除了結尾最后一個字符是 l,i,n,k 任何一個字符的所有文件。
輸出,輸出重定向
上述我們操作的命令執行結果都是直接輸出到了控制臺,但是假如我們的輸出結果很長,或者暫時有其他事情要做,結果需要等稍后去分析該怎么辦呢?這個時候我們可以將結果輸出重定向到到一個文件中,保存起來,稍后查看。
[root@localhost ~]$ls *[link] > links.txt[root@localhost ~]$cat links.txt
baidu.html
ha_link
hard_link
soft_link
如上操作,我們便將文件列表的結果輸出到了 links.txt 文件中,以便稍后調查問題。
管道
剛剛我們查看的是用戶工作目錄下的文件,輸出很少,可以直接查看。但是當我們查詢一個文件夾下面有很多文件的時候,輸出結果很長,此時一個屏幕都無法展示結果,查看并不方便。有沒有一種方法,可以讓我們先查看一部分結果,查看完畢后,按下一個鍵繼續查看下一頁呢?也許聰明的你會想到 more 或者 less 命令。但是這倆個命令是來查看文件的,此時管道可以幫助你。管道可以使得輸入輸出重定向,將一個命令的輸出作為另外一個命令的輸入。
[root@localhost ~]$ls /etc | moreadjtime
aliases
aliases.db
alternatives
anacrontab
asound.conf
at.deny
audisp
audit
bash_completion.d
bashrc
binfmt.d
centos-release
centos-release-upstream
--more--
如上,ls 的輸出結果,作為了 more 的輸入,這樣我們就可以優哉游哉的慢慢查看 ls 的結果。當然這里只是用 ls 和 more 來舉例,朋友們可以自己去探索其他的命令結合管道來使用,你會愛上它的。
命令置換
同樣,命令置換也可以達到像管道一樣的操作。它也將命令的輸出結果作為另外一個命令輸入。
[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 命令的輸出(當前工作目錄),作為 ls 命令的輸入。當然該命令也可以用管道來實現:pwd | ls,達到同樣的效果:
[root@localhost ~]$pwd | lsanaconda-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
實現的同樣的效果,但是實現原理不同。命令置換是通過在子 shell 中執行結果,然后將結果返回到主 shell 中。而管道則一直在主 shell 中執行。
—————END—————
喜歡本文的朋友們,歡迎長按下圖訂閱,收看更多精彩內容
轉載于:https://blog.51cto.com/7779961/2169233
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的深入理解 Unix / Linux 命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (第五篇)Linux操作系统基本结构介绍
- 下一篇: linux 其他常用命令