每一個程式設計師需要了解的10個Linux命令

作為一個程序員,在軟件開發職業生涯中或多或少會用到Linux系統,並且可能會使用Linux命令來檢索需要的信息。本文將為各位開發者分享10個有用的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 man lsroot@devopscube:~# 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 do not ignore entries starting with .

2、touch,cat和less命令

touch命令可以在Linux系統中創建大小為0的任意類型文件,作為程序開發者,當你需要在Linux服務器上創建文件時,可以使用touch命令:

語法: touch touch demo.txt

root@devopscube:~# touch demo.txtroot@devopscube:~# lsdemo.txt

cat命令用來查看文件的內容,但是使用cat命令並不能編輯文件的內容,它僅僅是可以瀏覽文件內容。cat命令不支持鍵盤上下鍵翻頁。

語法: cat cat demo.txt

同樣的less命令也可以讓你瀏覽文件,less命令非常快,並且支持上下鍵查看文件的開頭和末尾。然而more命令和它類似,只是more命令只能用enter鍵實現文件的向前翻頁,不支持回退。

語法: less

more

less demo.txt

more demo.txt

3、sort和grep命令

sort命令用來對文件內容進行排序。創建一個名為test.txt的文件,並且把以下內容拷貝到該文件中:

1 mike level intermediate jan10 lucy level beginer mar45 Dave level expert dec4 dennis start beginner jul7 Megan employee trainee feb58 Mathew Head CEO nov

上面的例子中,第二列是名稱,所以如果你想對名稱列按字母排序,就可以使用“-k”選項,並標註列號,比如“-k2”:

語法: sort

sort -k2 test.txt

排序結果

root@devopscube:~# sort -k2 test.txt45 Dave level expert dec4 dennis start beginner jul10 lucy level beginer mar58 Mathew Head CEO nov7 Megan employee trainee feb1 mike level intermediate jan

第一列是數字,如果你想按數字排序,可以使用“-h”選項。如果數字在不同列上,你可以在“-h”選項後使用“-k”選項:

root@devopscube:~# sort -h test.txt 1 mike level intermediate jan4 dennis start beginner jul7 Megan employee trainee feb10 lucy level beginer mar45 Dave level expert dec58 Mathew Head CEO nov

最後一列是月份,你可以使用“-M”選項來讓文件內容按月份排序:

root@devopscube:~# sort -k5 -M test.txt1 mike level intermediate jan7 Megan employee trainee feb10 lucy level beginer mar4 dennis start beginner jul58 Mathew Head CEO nov45 Dave level expert dec

注:如果你想消除重複的行,可以在sort命令後使用“-u”選項。

使用“-r”選項,是文件倒序排列:

root@devopscube:~# sort -h -r test.txt58 Mathew Head CEO nov45 Dave level expert dec10 lucy level beginer mar7 Megan employee trainee feb4 dennis start beginner jul1 mike level intermediate jan

Grep命令:

Grep命令非常強大,系統管理員經常會用到它。grep命令可以在文件中搜索指定格式的字符串,同時對其進行標準輸出。

語法: grep ""

grep "Mathew" test.txt

root@devopscube:~# grep "dennis" test.txt4 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.txtroot@devopscube:~# cut -c1-2 test.txt110454758

如果你希望從文件中提取指定的字符串,那麼你可以使用分隔符選項“-d”和“-f”選項選中列。例如,我們可以利用cut命令提取names列:

root@devopscube:~# cut -d' ' -f2 test.txtmike

lucyDavedennisMeganMathew

下面的例子從/etc/passd file中提取users列:

cut -d':' -f1 /etc/passwd

5、sed命令

sed 是一種在線編輯器,它一次處理一行內容。處理時,把當前處理的行存儲在臨時緩衝區中,稱為“模式空間”(pattern space),接著用sed命令處理緩衝區中的內容,處理完成後,把緩衝區的內容送往屏幕。接著處理下一行,這樣不斷重複,直到文件末尾。文件內容並沒有 改變,除非你使用重定向存儲輸出。

如果你想通過搜索替換文件中的指定內容,你可以使用“s”選項來檢索到它然後將它替換。

語法: sed 's///' test.txt

例如,在test.txt文件中用“michael”替換“mike”:

sed 's/mike/michael/' test.txtroot@devopscube:~# sed 's/mike/michael/' test.txt1 michael level intermediate jan10 lucy level beginer mar45 Dave level expert dec4 dennis start beginner jul7 Megan employee trainee feb58 Mathew Head CEO nov

6、tar命令

tar命令用來壓縮和解壓縮文件,其中經常會用到“-cf”和“-xf”選項。

語法: tar

讓我們將test.txt文件打包:

tar -cf test.tar test.txtroot@devopscube:~# tar -cf test.tar test.txtroot@devopscube:~# lstest.tar test.txt

用“-C”選項將剛才打包好的test.tar文件解壓縮至“demo”目錄:

tar -xf test.tar -C /root/demo/root@devopscube:~# tar -xf test.tar -C /root/demo/root@devopscube:~# cd demo/root@devopscube:~/demo# lstest.txt

7、find命令

find命令用來檢索文件,可以用“-name”選項來檢索指定名稱的文件:

find -name find -name test.txtroot@devopscube:/home/ubuntu# cd ~root@devopscube:~# find -name test.txt./demo/test.txt./test.txt

你也可以用“/ -name”來檢索指定名稱的文件夾:

find / -name passwdroot@devopscube:~# 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

diff test.txt test1.txt

root@devopscube:~# diff test.txt test1.txt7c7< 59 sdfsd---> 59 sdfsd CTO dec

9、Uniq命令

uniq命令用來過濾文件中的重複行:

語法: uniq

uniq test.txt

root@devopscube:~# uniq test.txt1 mike level intermediate jan10 lucy level beginer mar45 Dave level expert dec4 dennis start beginner jul7 Megan employee trainee feb58 Mathew Head CEO nov

10、chmod命令

chmod命令用來改變文件的讀/寫/執行權限,權限數值如下所示:

4 - read permission2 - write permission1 - execute permission0 - no permission

下面的命令可以給test.txt文件賦最高的權限:

chmod 755 test.txt


分享到:


相關文章: