做爲一個程序員,在軟件開發職業生涯中或多或少會用到Linux系統,而且可能會使用Linux命令來檢索須要的信息。本文將爲各位開發者分享10個有用的Linux命令,但願對你會有所幫助。程序員
每個程序員須要瞭解的10個Linux命令正則表達式
如下就是今天咱們要介紹的Linux命令:ubuntu
man服務器
touch, cat and lessless
sort and grep編輯器
cutide
sedspa
tarorm
find排序
diff
uniq
chmod
接下來讓咱們逐一來詳細介紹。
一、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 .
二、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
三、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
固然你也能夠用正則表達式來匹配字符串。
四、cut命令
cut命令可讓你用列或者分隔符提取文件中的指定部分。若是你要列出文件中某列的所有內容,可使用「-c」選項。例如,下面將從test.txt文件中提取第一、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
五、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
六、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
七、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
八、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
九、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
十、chmod命令
chmod命令用來改變文件的讀/寫/執行權限,權限數值以下所示:
4 - read permission2 - write permission1 - execute permission0 - no permission
下面的命令能夠給test.txt文件賦最高的權限:
chmod 755 test.txt