每一個程式設計師需要了解的10個Linux命令
作為一個程式設計師,在軟體開發職業生涯中或多或少會用到Linux系統,並且可能會使用Linux命令來檢索需要的資訊。本文將為各位開發者分享10個有用的Linux命令,希望對你會有所幫助。
以下就是今天我們要介紹的Linux命令:
man touch, cat and less sort and grep cut sed tar find diff uniq chmod
接下來讓我們逐一來詳細介紹。
1、man命令
第一個你需要知道的Linux命令就是man命令,該命令可以顯示指定命令的用法和描述。比如你想知道ls命令的用法和選項,可以在終端執行“man ls”:
語法: man <command name> man ls
[email protected]:~# 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 do not ignore entries starting with .
2、touch,cat和less命令
touch命令可以在Linux系統中建立大小為0的任意型別檔案,作為程式開發者,當你需要在Linux伺服器上建立檔案時,可以使用touch命令:
語法: touch <filename> touch demo.txt [email protected]:~# touch demo.txt [email protected]:~# ls demo.txt
cat命令用來檢視檔案的內容,但是使用cat命令並不能編輯檔案的內容,它僅僅是可以瀏覽檔案內容。cat命令不支援鍵盤上下鍵翻頁。
語法: cat <filename> cat demo.txt
同樣的less命令也可以讓你瀏覽檔案,less命令非常快,並且支援上下鍵檢視檔案的開頭和末尾。然而more命令和它類似,只是more命令只能用enter鍵實現檔案的向前翻頁,不支援回退。
語法: less <filename> more <filename>
less demo.txt more demo.txt
3、sort和grep命令
sort命令用來對檔案內容進行排序。建立一個名為test.txt的檔案,並且把以下內容拷貝到該檔案中:
1 mike level intermediate jan 10 lucy level beginer mar 45 Dave level expert dec 4 dennis start beginner jul 7 Megan employee trainee feb 58 Mathew Head CEO nov
上面的例子中,第二列是名稱,所以如果你想對名稱列按字母排序,就可以使用“-k”選項,並標註列號,比如“-k2”:
語法: sort sort -k2 test.txt
排序結果
[email protected]:~# sort -k2 test.txt 45 Dave level expert dec 4 dennis start beginner jul 10 lucy level beginer mar 58 Mathew Head CEO nov 7 Megan employee trainee feb 1 mike level intermediate jan
第一列是數字,如果你想按數字排序,可以使用“-h”選項。如果數字在不同列上,你可以在“-h”選項後使用“-k”選項:
[email protected]:~# sort -h test.txt 1 mike level intermediate jan 4 dennis start beginner jul 7 Megan employee trainee feb 10 lucy level beginer mar 45 Dave level expert dec 58 Mathew Head CEO nov
最後一列是月份,你可以使用“-M”選項來讓檔案內容按月份排序:
[email protected]:~# sort -k5 -M test.txt 1 mike level intermediate jan 7 Megan employee trainee feb 10 lucy level beginer mar 4 dennis start beginner jul 58 Mathew Head CEO nov 45 Dave level expert dec
注:如果你想消除重複的行,可以在sort命令後使用“-u”選項。
使用“-r”選項,是檔案倒序排列:
[email protected]:~# sort -h -r test.txt 58 Mathew Head CEO nov 45 Dave level expert dec 10 lucy level beginer mar 7 Megan employee trainee feb 4 dennis start beginner jul 1 mike level intermediate jan
Grep命令:
Grep命令非常強大,系統管理員經常會用到它。grep命令可以在檔案中搜索指定格式的字串,同時對其進行標準輸出。
語法: grep "<search string>" <filename> grep "Mathew" test.txt
[email protected]:~# grep "dennis" test.txt 4 dennis start beginner jul
上面命令的輸出結果是包含該子字串的,如果你想檢索完整的單詞,你需要新增“-i”選項。同時,也可以用grep命令在多個檔案中搜索字串,命令程式碼如下:
grep "dennis" test1.txt test2.txt test3.txt
當然你也可以用正則表示式來匹配字串。
4、cut命令
cut命令可以讓你用列或者分隔符提取檔案中的指定部分。如果你要列出檔案中某列的全部內容,可以使用“-c”選項。例如,下面將從test.txt檔案中提取第1、2列的全部內容。
cut -c1-2 test.txt
[email protected]:~# cut -c1-2 test.txt 1 10 45 4 7 58
如果你希望從檔案中提取指定的字串,那麼你可以使用分隔符選項“-d”和“-f”選項選中列。例如,我們可以利用cut命令提取names列:
cut -d' ' -f2 test.txt
[email protected]:~# cut -d' ' -f2 test.txt mike lucy Dave dennis Megan Mathew
下面的例子從/etc/passd file中提取users列:
cut -d':' -f1 /etc/passwd
5、sed命令
sed 是一種線上編輯器,它一次處理一行內容。處理時,把當前處理的行儲存在臨時緩衝區中,稱為“模式空間”(pattern space),接著用sed命令處理緩衝區中的內容,處理完成後,把緩衝區的內容送往螢幕。接著處理下一行,這樣不斷重複,直到檔案末尾。檔案內容並沒有 改變,除非你使用重定向儲存輸出。
如果你想通過搜尋替換檔案中的指定內容,你可以使用“s”選項來檢索到它然後將它替換。
語法: sed 's/<old-word>/<new-word>/' test.txt
例如,在test.txt檔案中用“michael”替換“mike”:
sed 's/mike/michael/' test.txt
[email protected]:~# sed 's/mike/michael/' test.txt 1 michael level intermediate jan 10 lucy level beginer mar 45 Dave level expert dec 4 dennis start beginner jul 7 Megan employee trainee feb 58 Mathew Head CEO nov
6、tar命令
tar命令用來壓縮和解壓縮檔案,其中經常會用到“-cf”和“-xf”選項。
語法: tar <options> <archive-name> <file/folder name>
讓我們將test.txt檔案打包:
tar -cf test.tar test.txt
[email protected]:~# tar -cf test.tar test.txt [email protected]:~# ls test.tar test.txt
用“-C”選項將剛才打包好的test.tar檔案解壓縮至“demo”目錄:
tar -xf test.tar -C /root/demo/
[email protected]:~# tar -xf test.tar -C /root/demo/ [email protected]:~# cd demo/ [email protected]:~/demo# ls test.txt
7、find命令
find命令用來檢索檔案,可以用“-name”選項來檢索指定名稱的檔案:
find -name find -name test.txt
[email protected]:/home/ubuntu# cd ~ [email protected]:~# find -name test.txt ./demo/test.txt ./test.txt
你也可以用“/ -name”來檢索指定名稱的資料夾:
find / -name passwd
[email protected]:~# find / -name passwd /etc/cron.daily/passwd /etc/pam.d/passwd /etc/passwd /usr/share/lintian/overrides/passwd
8、diff命令
diff命令用來找出2個檔案的不同點。diff命令通過分析檔案內容,然後將不同的行打印出來,下面的例子可以找出兩個檔案test和test1的不同點:
語法: diff <filename1> <filename2> diff test.txt test1.txt
[email protected]:~# diff test.txt test1.txt 7c7 < 59 sdfsd --- > 59 sdfsd CTO dec
9、Uniq命令
uniq命令用來過濾檔案中的重複行:
語法: uniq uniq test.txt
[email protected]:~# uniq test.txt 1 mike level intermediate jan 10 lucy level beginer mar 45 Dave level expert dec 4 dennis start beginner jul 7 Megan employee trainee feb 58 Mathew Head CEO nov
10、chmod命令
chmod命令用來改變檔案的讀/寫/執行許可權,許可權數值如下所示:
4 - read permission 2 - write permission 1 - execute permission 0 - no permission
下面的命令可以給test.txt檔案賦最高的許可權:
chmod 755 test.txt