寫代碼是一個既簡單又復雜的工作,有編程大師說,寫代碼就像寫詩,關鍵在于如何能夠用最簡潔的語言和邏輯寫出最精彩的代碼。今天阿湯博客就與大家分享一些Linux簡單又實用的Linux命令,掌握了這些命令,可以幫助你提升工作效率,節約時間,達到事半功倍的效果。
1、tab
如果一定要從最基本、最好用的命令開始算起,不得不提到這個“tab”鍵,它能夠幫助你補全可能的命令。
當你敲打某個命令時,敲擊tab鍵,它會建議你幾種可能的選項,然后你只要選擇你想要的就好。
前提:已經安裝bash-completion。
Centos:
$ yum -y install bash-completion
Ubuntu:
$ apt-get install bash-completion
例:輸入命令yum,然后敲擊tab鍵,會顯示yum支持的參數。
[root@m.maowutv.com ~]# yum check groups load-transaction search check-update help makecache shell/ clean history provides update deplist info reinstall upgrade distro-sync install remove version downgrade list repolist
2、cd -
如果你現在在一個很長的目錄路徑中,想要移動到另一個完全不同的路徑上的另一個目錄, 然后你發現你不得不回到你之前的目錄。此時,最好的辦法就是鍵入這個命令,然后切換會上一個命令。這樣您將回到上一個工作目錄,而不需要鍵入長目錄路徑或復制粘貼它。
例:
[root@m.maowutv.com network-scripts]# cd /usr/local/share/info/ [root@m.maowutv.com info]# cd - /etc/sysconfig/network-scripts [root@m.maowutv.com network-scripts]#
3、cd ~ 或 cd
cd ~這個命令可以幫助你從Linux命令行中的任何位置回歸到你最開始的的主目錄,當然你也可以直接食用cd命令回到主目錄。
例:
[root@m.maowutv.com network-scripts]# cd ~ [root@m.maowutv.com ~]# [root@m.maowutv.com network-scripts]# cd [root@m.maowutv.com ~]#
4、分號";"
假如你要在一個命令后馬上運行另一個命令,其實可以利用分號來實現在同一行中執行多個不同的命令,而不需要等到前面的命令執行完畢。用法:“command_1; command_2; command_3”。
例1:
[root@m.maowutv.com ~]# echo -n "阿湯博客";echo -n "地址是:";echo " 阿湯博客地址是:http://m.maowutv.com
例2:
[root@m.maowutv.com ~]# for i in `seq 1 9`;do echo ${i};done
1
2
3
4
5
6
7
8
9
5、&&
在第四條中,我們看到了如何同時執行多個不同命令,那么如果你想要讓一個命令一定在另一個命令執行完畢后才開始執行該怎么操作呢?答案是,你可以用&&符號將前面一定要執行完成才開始執行下一個命令之間進行分離。用法:“command_1 && command_2”。
例1:
[root@m.maowutv.com ~]# ping www.baidu2.com ping: www.baidu2.com: 未知的名稱或服務 [root@m.maowutv.com ~]# [ $? -eq 0 ] && echo "可以ping通" [root@m.maowutv.com ~]# ping -c 1 www.baidu.com PING www.baidu.com (14.215.177.39) 56(84) bytes of data. 64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=54 time=34.9 ms --- www.baidu.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 34.981/34.981/34.981/0.000 ms [root@m.maowutv.com ~]# [ $? -eq 0 ] && echo "可以ping通" 可以ping通
例2:結合";" 合并命令。
[root@m.maowutv.com ~]# ping -c 1 www.baidu.com &> /dev/null;[ $? -eq 0 ] && echo "可以ping通" 可以ping通 [root@m.maowutv.com ~]# ping -c 1 www.baidu2.com &> /dev/null;[ $? -eq 0 ] && echo "可以ping通" [root@m.maowutv.com ~]#
6、ctrl+r
有些時候我們用了一個命令后,幾個小時后會再用一次,可能這個命令很長,如果有一種命令能夠快速找到自己之前的碼字歷史,是不是很好用呢?
你只要輸入ctrl+r,再加上你要找的命令的一部分,Linux系統就會為你展示可能匹配的之前的命令。
例:輸入ctrl+r以后,再輸入baidu.com,就出現了之前使用過的命令。
(reverse-i-search)`baidu.com': ping -c 1 www.baidu.com &> /dev/null;[ $? -eq 0 ] && echo "可以ping通"
7、ctrl+s 與 ctrl+q
你可能早已習慣于使用Ctrl + S進行保存。 但是如果您在Linux終端上使用該命令,它卻會凍結你的設備。當然,這種手誤不是什么大問題,無需關機重啟,你只要使用ctrl+Q在解鎖終端設備就好了。
8、ctrl+a 與 ctrl+e
假設你輸入了一個長命令,中途你意識到你必須在開始時改變一些東西。 你可能會使用幾個左箭頭不斷點擊,然后移動到這一行的開頭。但有一種更快的方式,就是可以使用Ctrl + a轉到行的開頭,Ctrl + e可以轉到最后。
9、z命令
一般而言,服務器日志都需要被壓縮,然后節省磁盤空間的。而有時候你想查看壓縮文件中的日志,只能將壓縮文件傳到本地,然后解壓縮才能查看。但是,z命令提供了一種無需解壓縮,而直接查看命令的方法。如 zless, zcat, zgrep等命令直接查看相應的日志文件。
例:
[root@m.maowutv.com test]# zcat m.maowutv.com.gz 1、阿湯博客地址:http://m.maowutv.com。 2、zless, zcat, zgrep命令非常方便。 [root@m.maowutv.com test]# zgrep '阿湯博客' m.maowutv.com.gz 1、阿湯博客地址:http://m.maowutv.com。 [root@m.maowutv.com test]# zless m.maowutv.com.gz 1、阿湯博客地址:http://m.maowutv.com。 2、zless, zcat, zgrep命令非常方便。 m.maowutv.com.gz (END)
10、嘆號"!"
!!:可以調用上一條使用過的命令。
例:
[root@m.maowutv.com ~]# which bash /usr/bin/bash [root@m.maowutv.com ~]# !! which bash /usr/bin/bash
!$:調用上一條命令最后一個參數。
例:
[root@m.maowutv.com ~]# ls /usr/bin/bash /usr/bin/bash [root@m.maowutv.com ~]# ls -la !$ ls -la /usr/bin/bash -rwxr-xr-x. 1 root root 964608 10月 31 2018 /usr/bin/bash
!^:調用上一條命令第一個參數,適用于參數比較長的場景。
例:
[root@m.maowutv.com ~]]# ls /usr/bin/sh /usr/bin/bash /usr/bin/bash /usr/bin/sh [root@m.maowutv.com ~]]# ls -ls !^ ls -ls /usr/bin/sh 0 lrwxrwxrwx. 1 root root 4 5月 9 2019 /usr/bin/sh -> bash
!*:調用上一條命令的所有參數。
例:
[root@m.maowutv.com ~]]# fin ./ -name 'm.maowutv.com.gz' -bash: fin: 未找到命令 [root@m.maowutv.com ~]]# find !* find ./ -name 'm.maowutv.com.gz' ./m.maowutv.com.gz
![命令名]:[參數號];調用上一條命令的指定參數,適用于參數比較長的場景。
例:
[root@m.maowutv.com ~]]# ls /usr/bin/bash /usr/bin/sh /usr/bin/wc /usr/bin/bash /usr/bin/sh /usr/bin/wc [root@m.maowutv.com ~]]# ls -la !ls:2 ls -la /usr/bin/sh lrwxrwxrwx. 1 root root 4 5月 9 2019 /usr/bin/sh -> bash
!n:調用history歷史命令,n表示歷史命令序號。-n表示倒數第幾條。
例:
[root@m.maowutv.com ~]]# !1259 which bash /usr/bin/bash [root@m.maowutv.com ~]]# !-1 which bash /usr/bin/bash
![關鍵字]:調用上一條以關鍵字開頭的命令,
例:
[root@m.maowutv.com ~]# ls /usr/bin/bash /usr/bin/sh /usr/bin/wc /usr/bin/bash /usr/bin/sh /usr/bin/wc [root@m.maowutv.com ~]# !ls ls /usr/bin/bash /usr/bin/sh /usr/bin/wc /usr/bin/bash /usr/bin/sh /usr/bin/wc
11、alias
我們可利用alias,把一些經常使用,比較復雜的命令設置為自定指令的別名,方便我們自己使用。若僅輸入alias,則可列出目前所有的別名設置。alias的生效僅該次登錄的操作。若要每次登錄是即自動設好別名,可在.profile或.cshrc中設定指令的別名。
例:
[root@m.maowutv.com ~]# alias blog="echo -n "阿湯博客";echo -n "地址是:";echo "http://m.maowutv.com"" [root@m.maowutv.com ~]# blog 阿湯博客地址是:http://m.maowutv.com
12、Ctrl+c
這個看起來太像復制命令,但實際上,他是一個快速結束某個命令或進程的方法。雖然這聽起來有些怪,但是如果你在前臺運行一個命令,但是你想退出,你可以使用Ctrl+C來快速結束這個命令。
13、xargs
xargs命令能夠捕獲一個命令的輸出,然后傳遞給另外一個命令。之所以能用到這個命令,關鍵是由于很多命令不支持|管道來傳遞參數,而日常工作中有這個必要,所以就有了 xargs 命令。
xargs命令格式:
somecommand |xargs -item command
xargs命令參數:
-a file 從文件中讀入作為sdtin
-e flag ,注意有的時候可能會是-E,flag必須是一個以空格分隔的標志,當xargs分析到含有flag這個標志的時候就停止。
-p 當每次執行一個argument的時候詢問一次用戶。
-n num 后面加次數,表示命令在執行的時候一次用的argument的個數,默認是用所有的。
-t 表示先打印命令,然后再執行。
-i 或者是-I,這得看linux支持了,將xargs的每項名稱,一般是一行一行賦值給 {},可以用 {} 代替。
-r no-run-if-empty 當xargs的輸入為空的時候則停止xargs,不用再去執行了。
-s num 命令行的最大字符數,指的是 xargs 后面那個命令的最大命令行字符數。
-L num 從標準輸入一次讀取 num 行送給 command 命令。
-l 同 -L。
-d delim 分隔符,默認的xargs分隔符是回車,argument的分隔符是空格,這里修改的是xargs的分隔符。
-x exit的意思,主要是配合-s使用。。
-P 修改最大的進程數,默認是1,為0時候為as many as it can 。
例:我們需要查找某個文件夾下面所有的文件,找到所有包含數據庫信息的文件,并更新數據庫root的密碼。
[root@m.maowutv.com ~]# grep -ilr 3306 /app/wwwroot/ | xargs -n 1 -- sed -i 's@root:123456@root:abc123@g'
14、man
有些時候,我們可能忘記了某個命令的使用方法,或者某個參數的使用規則,可以使用 man 命令查看此命令的詳細使用手冊。
使用方法:man 命令
例:
[root@m.maowutv.com ~]# man grep

15、help
應該說這是個萬能的命令,你幾乎可以在任何時候輸入這個命令尋求幫助,畢竟幾乎所有的命令行工具都帶有幫助頁面,顯示如何使用命令。有些命令獲取幫助是:command --help。help輸出內容為man 命令的精簡內容。
例:
[root@m.maowutv.com ~]# help cd cd: cd [-L|[-P [-e]]] [dir] Change the shell working directory. Change the current directory to DIR. The default DIR is the value of the HOME shell variable. The variable CDPATH defines the search path for the directory containing DIR. Alternative directory names in CDPATH are separated by a colon (:). A null directory name is the same as the current directory. If DIR begins with a slash (/), then CDPATH is not used. If the directory is not found, and the shell option `cdable_vars' is set, the word is assumed to be a variable name. If that variable has a value, its value is used for DIR. Options: -Lforce symbolic links to be followed -Puse the physical directory structure without following symbolic links -eif the -P option is supplied, and the current working directory cannot be determined successfully, exit with a non-zero status The default is to follow symbolic links, as if `-L' were specified. Exit Status: Returns 0 if the directory is changed, and if $PWD is set successfully when -P is used; non-zero otherwise. [root@m.maowutv.com ~]# cd --help -bash: cd: --: 無效選項 cd: 用法:cd [-L|[-P [-e]]] [dir]



2020年12月13日 上午8:02 沙發
Wish You All The Best In 2021!