第二章 GNU and Unix Commands
一. Shell概述
1. shell是一command interpreter,在核心及使用者間提供一介面,並且可讓使用者與系統做交談(interact)。
以下圖來說,比如使用者在shell下輸入date指令,這時候會透過shell解析給核心這個訊息,因電腦認得的是二進位的程式碼,它並不認識date是什麼東西,所以要經過shell來將date解析成核心認得的二進位程式碼,如此工作才有辦法順利進行。
2. 微軟下,都是配標準的shell,如MS-DOS的command.com,Windows的explorer. exe,而在Linux下則有多種shell可供選擇 (sh、ash、ksh、csh、tcsh、bash)。
3. 第一個Unix系統的shell為Steve Bourne所寫,稱之為sh,在sh出現後,一些人相繼發展出各種不同的shell,而Liunx預設使用的shell為Bourne Again SHell,就是所謂的bash,是依sh發展而來的。
4. $ cat /etc/shells
→ 看看系統上目前可使用的shell有哪些。
$ chsh –s /bin/sh
→ 改變自己的shell為sh ( logout再login才生效 ) 。
$ echo $SHELL
→ 顯示目前所使用的shell。
# chsh –s /bin/csh userol
→ root將userol的shell改為csh。
二. Entering commands at the command prompt
在介紹基本指令之前,我們先大概認識一下提示號,以bash為例,如:
[barry@openlinux barry]$
在@左邊的barry為使用者名稱 ( 帳號 ),openlinux為主機名稱,在@右邊的barry為目前所在的目錄位置,也就是一般使用者登入系統後預設的家目錄位置barry。$ 是一般使用者的提示號,若是超級使用者 root登入,則其提示號為 #,如:[root@openlinux root]#
1. cd ( change directory ):
$ cd
→ 切換至家目錄,等於 $ cd ~
# cd ~user01
→ 切換至使用者user01的家目錄。
$cd -
→ 切換至前一個目錄的位置(非上一層目錄),於此前並顯示完整路徑。
$ cd /etc/mail
→ 切換至mail目錄,此為絕對路徑寫法。
$ cd ..
→ 切換至上一層目錄。
$ cd ../dir
→ 切換至上一層目錄下的dir目錄。此為相對路徑寫法。
另外」.」表示現在這個目錄,例如:
$ cp /etc/passwd .
→ 將passwd複製到現在這個目錄下。
2. ls (list):
-l:Lists in long format.
-a:Lists all files,including hidden files.
-R:Lists all files recursively down the tree.
-r:reverse order while sorting.
-d:Lists only the name of a directory,not its contents,一般搭配 -l。
例:$ ls -laR
$ ls –l /var
→ 列出var ( 不含 ) 以下的檔案目錄名稱,並以長格式顯示。
$ ls –ld /etc
→ 只列出etc目錄的長格式。
$ ls –l /etc/passwd
→ 顯示passwd檔案的長格式。
3. cat:
To send the contents of one or more files and to create new files.
例:$ cat /etc/passwd
$ cat /etc/named.conf>babafile
$ cat>newfile
→ 馬上按ctrl+d就是製造新檔案,亦可輸入內容後,再按ctrl+d。
$ cat>oldfile
→ 馬上按ctrl+d就等於清空原檔案的內容。
$ cat file1 file2 file3
$ cat –n /etc/termcap>newfile
4. more、less:顯示檔案內容
例:$ more /etc/termcap
→ to scroll forward using the spacebar
to scroll backward using the b key.
$ less /etc/termcap
→ 同上,但習慣用pageup、pagedown來翻頁,上式也可寫成
$ cat /etc/termcap | less
若要退出,按q。
尋找大檔案中字串:
例:$ cat –n /etc/termcap>barry
$ more barry → /fix 尋找fix字串 ( 從最前面找起 ) → n 找下一個fix。無法往回找。
$ less barry → /fix→n ( 找下一個 ) → n ( 再下一個 ) → N ( 回上一個 )。
若要進入vi command mode,可按v進入,若要退回原more或less
畫面,使用:q 。
˙Redirector and Pipes and Stream
(1) Stream: stdin ( standard input ),代表數字0。
stdout ( standard output ),代表數字 1。
stderr ( standard error ),代表數字 2。
stdin預設為從keyboard輸入。
stdout、stderr此兩種輸出類型則預設輸出至screen。
(2) Pipe:send the stdout of one command as stdin to another command
例:$ cat /etc/termcap | less
= $ less /etc/termcap
$ rpm –qa | grep bind
(3) Redirector:主要為改變stream的預設值。
>:where to send stdout
例:$ cat /etc/passwd> barryfile ( 會cover原內容 )
$ cat >file
→ 按ctrl+d等於建新的空檔案或清空原檔案內容,亦可先
輸入內容後,再按ctrl+d。
$ ls –l /var> newfile
$ cat /etc/passwd noexistfile 1>file1 2>file2
→ 將此兩個檔案內容,其stdout輸出至file1,stderr輸出
至file2,1為預設值。
$ cat /etc/passwd noexistfile>file1
→ 將stdout輸出至file1,stderr輸出至screen。
$ cat /etc/passwd noexistfile>barryfile 2>&1
= $ cat /etc/passwd noexistfile >& barryfile
= $cat /etc/passwd noexistfile &> barryfile
→ 將stdout及stderr輸出至barryfile。
$ cat /etc/fstab noexistfile > /dev/null 2>&1
$ true > barryfile
→清空檔案內容
= $ cat >barryfile 按ctrl+d
= $ cat /dev/null > barryfile
>>:append file,can』t cover file content.
例:$ cat /etc/fstab >> barryfile
<:redirect stdin ( 由stdin讀取資料 )
例:$ cat < /etc/fstab
→ file was opened by the shell ( note:shell一次只能開一
個檔案 )
$ cat /etc/fstab
→ file was opened by cat.以上兩者結果相同,但意義不同。
$ mail Jessica < messagefile
<<:
例:$ cat <<endoff
→ 表示輸入至endoff字串時結束。
$ cat << end > barryfile
→ 輸入至end後結束,並且將輸入的內容輸出到barryfile。
以下為file filter command
5. head:output the first part of files
例:$ head -5 /etc/passwd
→ 列檔案前5行
= $ head –n 5 /etc/passwd
= $ cat /etc/passwd | head –5
$ head /etc/passwd
→ 預設列前10行
$ head -8 /etc/passwd /etc/termcap
6.tail:output the last part of files
例:$ tail -15 /etc/termcap
→ 列檔案最後面15行的內容
= $ tail –n 15 /etc/termcap
$ tail +20 /etc/passwd
→ 從第20行開始列至最後一行
# tail –f /var/log/messages
→ 按ctrl+c結束
(看最新訊息紀錄。Note:須root身份)
$ cat –n /etc/termcap | head –n 20 | tail –n 6
= $ cat –n /etc/termcap | head –20 | tail +15
→ 列出檔案15-20行的內容
7.cut:縱向截取檔案部份資料。
-c ( character )
-f (field) 搭配-d ( delimit )
例:$ cat /etc/passwd | cut –c 2,5
→ 列出檔案中第2及第5個字元。
$ cut –c 2-5 /etc/passwd
→ 列出第2到第5個字元。
$ cut –d: -f 2 /etc/passwd
→ 列出檔案內容中以 」 : 」 做界限的第2欄位。
$ cut –d: -f 3,5 /etc/passwd
→ 以 」 : 」 做界限來列出第3及第5 欄位。
$ cut –d: -f 3-5 /etc/passwd
→ 以 」 : 」 做界限,列出第3至5欄位。
8.wc: -l(lines) -w(words) -c(characters)
例 $ wc /etc/fstab /etc/passwd
→ 列出順序:行、字、字元。即分別列出fstab、passwd內容的
行數、字數、字元數,兩檔案的合計也會列出來。
$ cat /etc/termcap | wc –c
$ cat /etc/passwd | wc –l
→ 有幾行即表幾個帳號。
9.tr:replace one string with another or delete characters in a file
-s characters 將重覆字元縮成一個。
-d characters 刪除所指定字元。
<note> tr can』t open files
→ tr是所有filters中,唯一不能直接open file的。
例:$ cat /etc/passwd | tr a-z A- Z
→ 將所有小寫英文字母替換成大寫英文字母。
$ cat /etc/passwd | tr bary abcd
→ 將檔案中的b換成a,a換成b,r換成c,y換成d。
$ tr a-z A-Z /etc/passwd
→ 錯誤,因不能open files。
$ cat file | tr –s ad
→ 將a及d 字母有重覆的,縮成一個。
$ cat file1 file2 | tr –d c
→ 刪除檔案內容中的c字元。
10.nl: = cat –n 將檔案內容附上編號列出來。
例: $ nl /etc/passwd
11.sort ( 排序 ) :預設以每行第一字元為依據做排序。
預設順序:空格 → 其它字元(#!…) → 數字 → 英文字母大、小寫。
-n:按數字大小排序,英文字母大、小寫 → 數字。
-r:以預設相反預序作排序 (reverse) 。
-m (merge) :將檔案內容合併(上下),但不做排序。
-o (output) :排序後,將結果輸出至file中。
例: $ cat /etc/passwd | cut –d: -f 1,3 | sort
$ sort –n file1 file2
$ sort –m file1 file2
$ sort –r /etc/passwd -o newfile
= $ sort –r /etc/passwd > newfile
$ cat /etc/fstab /etc/passwd | sort –m -o newfile
12.tac:cat的相反順序
例:$ tac /etc/passwd
13.od:examine binary files and output in octal numbers。
例:$ od /usr/bin/nslookup( $ file /usr/bin/nslookup查屬性)
14.sed:a stream editor
例:$ cat –n /etc/passwd > passwd
$ sed 『3,5d』 passwd
→ 刪除第3~5行。
$ sed 『5,$d』 passwd
→ 刪除第5行~最後一行。
$ sed 『10d』 passwd
→ 刪除第10行。
$ sed 『/root/d』 passwd
→ 刪除root字串所在列數的那一行。
$ sed 『s/user/barry/』 passwd
→ 將每列遇到的第一個user字串改成barry。
<note>:替代字串與原字串字數可不同,但tr指令須需相同。
Ex:$ cat passwd | tr user barry
→ 只替代了barr。
$ sed 『s/user/mary/g』 passwd
→ global,將檔案內容中所有user字串改成mary,不限於只修改每列遇到的第一個字串。
$ sed –n 『34s/user/christal/gp』 passwd
→ 只列出有改變的行數,記得p一定要搭配 –n。
$ sed 』34,$s/user/michael/g』 passwd
$ sed –n 『3,5p』 passwd
$ sed –n 』20,$p』 passwd
$ vi file
→ 編輯內容:35s/user/david/gp
$ sed –nf file passwd
以下二者為重導指令
15.tee:唯一可同時將檔案內容輸出至screen及file中。
例:$ cat /etc/fstab | sed 『s/none/barry/g』 | tee file1
若要避免cover,可用tee –a ( append )
16.xargs:將前面指令執行結果,當成xargs其後所接指令的引數(argument),
此引數可能為檔案名稱或目錄名稱。
例:$ find /home/barry -name passwd | xargs rm –f
$ ls | xargs rm –rf