掌握了這些Linux命令,你的工作效率事半功倍

2020年12月11日13:57:20 1 4,694 ℃

寫代碼是一個(gè)既簡(jiǎn)單又復(fù)雜的工作,有編程大師說(shuō),寫代碼就像寫詩(shī),關(guān)鍵在于如何能夠用最簡(jiǎn)潔的語(yǔ)言和邏輯寫出最精彩的代碼。今天阿湯博客就與大家分享一些Linux簡(jiǎn)單又實(shí)用的Linux命令,掌握了這些命令,可以幫助你提升工作效率,節(jié)約時(shí)間,達(dá)到事半功倍的效果。

1、tab

如果一定要從最基本、最好用的命令開始算起,不得不提到這個(gè)“tab”鍵,它能夠幫助你補(bǔ)全可能的命令。

當(dāng)你敲打某個(gè)命令時(shí),敲擊tab鍵,它會(huì)建議你幾種可能的選項(xiàng),然后你只要選擇你想要的就好。

前提:已經(jīng)安裝bash-completion。

Centos:

$ yum -y install bash-completion

Ubuntu:

$ apt-get install bash-completion

例:輸入命令yum,然后敲擊tab鍵,會(huì)顯示yum支持的參數(shù)。

[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 -

如果你現(xiàn)在在一個(gè)很長(zhǎng)的目錄路徑中,想要移動(dòng)到另一個(gè)完全不同的路徑上的另一個(gè)目錄, 然后你發(fā)現(xiàn)你不得不回到你之前的目錄。此時(shí),最好的辦法就是鍵入這個(gè)命令,然后切換會(huì)上一個(gè)命令。這樣您將回到上一個(gè)工作目錄,而不需要鍵入長(zhǎng)目錄路徑或復(fù)制粘貼它。

例:

[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 ~這個(gè)命令可以幫助你從Linux命令行中的任何位置回歸到你最開始的的主目錄,當(dāng)然你也可以直接食用cd命令回到主目錄。

例:

[root@m.maowutv.com network-scripts]# cd ~
[root@m.maowutv.com ~]# 
[root@m.maowutv.com network-scripts]# cd 
[root@m.maowutv.com ~]#

4、分號(hào)";"

假如你要在一個(gè)命令后馬上運(yùn)行另一個(gè)命令,其實(shí)可以利用分號(hào)來(lái)實(shí)現(xiàn)在同一行中執(zhí)行多個(gè)不同的命令,而不需要等到前面的命令執(zhí)行完畢。用法:“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、&&

在第四條中,我們看到了如何同時(shí)執(zhí)行多個(gè)不同命令,那么如果你想要讓一個(gè)命令一定在另一個(gè)命令執(zhí)行完畢后才開始執(zhí)行該怎么操作呢?答案是,你可以用&&符號(hào)將前面一定要執(zhí)行完成才開始執(zhí)行下一個(gè)命令之間進(jìn)行分離。用法:“command_1 &&  command_2”。

例1:

[root@m.maowutv.com ~]# ping www.baidu2.com
ping: www.baidu2.com: 未知的名稱或服務(wù)
[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:結(jié)合";" 合并命令。

[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

有些時(shí)候我們用了一個(gè)命令后,幾個(gè)小時(shí)后會(huì)再用一次,可能這個(gè)命令很長(zhǎng),如果有一種命令能夠快速找到自己之前的碼字歷史,是不是很好用呢?

你只要輸入ctrl+r,再加上你要找的命令的一部分,Linux系統(tǒng)就會(huì)為你展示可能匹配的之前的命令。

例:輸入ctrl+r以后,再輸入baidu.com,就出現(xiàn)了之前使用過(guò)的命令。

(reverse-i-search)`baidu.com': ping -c 1 www.baidu.com &> /dev/null;[ $? -eq 0 ] && echo "可以ping通"

7、ctrl+s 與 ctrl+q

你可能早已習(xí)慣于使用Ctrl + S進(jìn)行保存。 但是如果您在Linux終端上使用該命令,它卻會(huì)凍結(jié)你的設(shè)備。當(dāng)然,這種手誤不是什么大問(wèn)題,無(wú)需關(guān)機(jī)重啟,你只要使用ctrl+Q在解鎖終端設(shè)備就好了。

8、ctrl+a 與 ctrl+e

假設(shè)你輸入了一個(gè)長(zhǎng)命令,中途你意識(shí)到你必須在開始時(shí)改變一些東西。 你可能會(huì)使用幾個(gè)左箭頭不斷點(diǎn)擊,然后移動(dòng)到這一行的開頭。但有一種更快的方式,就是可以使用Ctrl + a轉(zhuǎn)到行的開頭,Ctrl + e可以轉(zhuǎn)到最后。

9、z命令

一般而言,服務(wù)器日志都需要被壓縮,然后節(jié)省磁盤空間的。而有時(shí)候你想查看壓縮文件中的日志,只能將壓縮文件傳到本地,然后解壓縮才能查看。但是,z命令提供了一種無(wú)需解壓縮,而直接查看命令的方法。如 zless, zcat, zgrep等命令直接查看相應(yīng)的日志文件。

例:

[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、嘆號(hào)"!"

!!:可以調(diào)用上一條使用過(guò)的命令。

例:

[root@m.maowutv.com ~]# which bash
/usr/bin/bash
[root@m.maowutv.com ~]# !!
which bash
/usr/bin/bash

!$:調(diào)用上一條命令最后一個(gè)參數(shù)。

例:

[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

!^:調(diào)用上一條命令第一個(gè)參數(shù),適用于參數(shù)比較長(zhǎng)的場(chǎng)景。

例:

[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

!*:調(diào)用上一條命令的所有參數(shù)。

例:

[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

![命令名]:[參數(shù)號(hào)];調(diào)用上一條命令的指定參數(shù),適用于參數(shù)比較長(zhǎng)的場(chǎng)景。

例:

[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:調(diào)用history歷史命令,n表示歷史命令序號(hào)。-n表示倒數(shù)第幾條。

例:

[root@m.maowutv.com ~]]# !1259
which bash
/usr/bin/bash
[root@m.maowutv.com ~]]# !-1
which bash
/usr/bin/bash

![關(guān)鍵字]:調(diào)用上一條以關(guān)鍵字開頭的命令,

例:

[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,把一些經(jīng)常使用,比較復(fù)雜的命令設(shè)置為自定指令的別名,方便我們自己使用。若僅輸入alias,則可列出目前所有的別名設(shè)置。alias的生效僅該次登錄的操作。若要每次登錄是即自動(dòng)設(shè)好別名,可在.profile或.cshrc中設(shè)定指令的別名。

例:

[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

這個(gè)看起來(lái)太像復(fù)制命令,但實(shí)際上,他是一個(gè)快速結(jié)束某個(gè)命令或進(jìn)程的方法。雖然這聽起來(lái)有些怪,但是如果你在前臺(tái)運(yùn)行一個(gè)命令,但是你想退出,你可以使用Ctrl+C來(lái)快速結(jié)束這個(gè)命令。

13、xargs

xargs命令能夠捕獲一個(gè)命令的輸出,然后傳遞給另外一個(gè)命令。之所以能用到這個(gè)命令,關(guān)鍵是由于很多命令不支持|管道來(lái)傳遞參數(shù),而日常工作中有這個(gè)必要,所以就有了 xargs 命令。

xargs命令格式:

somecommand |xargs -item  command

xargs命令參數(shù):

-a file 從文件中讀入作為sdtin

-e flag ,注意有的時(shí)候可能會(huì)是-E,flag必須是一個(gè)以空格分隔的標(biāo)志,當(dāng)xargs分析到含有flag這個(gè)標(biāo)志的時(shí)候就停止。

-p 當(dāng)每次執(zhí)行一個(gè)argument的時(shí)候詢問(wèn)一次用戶。

-n num 后面加次數(shù),表示命令在執(zhí)行的時(shí)候一次用的argument的個(gè)數(shù),默認(rèn)是用所有的。

-t 表示先打印命令,然后再執(zhí)行。

-i 或者是-I,這得看linux支持了,將xargs的每項(xiàng)名稱,一般是一行一行賦值給 {},可以用 {} 代替。

-r no-run-if-empty 當(dāng)xargs的輸入為空的時(shí)候則停止xargs,不用再去執(zhí)行了。

-s num 命令行的最大字符數(shù),指的是 xargs 后面那個(gè)命令的最大命令行字符數(shù)。

-L num 從標(biāo)準(zhǔn)輸入一次讀取 num 行送給 command 命令。

-l 同 -L。

-d delim 分隔符,默認(rèn)的xargs分隔符是回車,argument的分隔符是空格,這里修改的是xargs的分隔符。

-x exit的意思,主要是配合-s使用。。

-P 修改最大的進(jìn)程數(shù),默認(rèn)是1,為0時(shí)候?yàn)閍s many as it can 。

例:我們需要查找某個(gè)文件夾下面所有的文件,找到所有包含數(shù)據(jù)庫(kù)信息的文件,并更新數(shù)據(jù)庫(kù)root的密碼。

[root@m.maowutv.com ~]# grep -ilr 3306 /app/wwwroot/ | xargs -n 1 -- sed -i 's@root:123456@root:abc123@g'

14、man

有些時(shí)候,我們可能忘記了某個(gè)命令的使用方法,或者某個(gè)參數(shù)的使用規(guī)則,可以使用 man 命令查看此命令的詳細(xì)使用手冊(cè)。

使用方法:man 命令

例:

[root@m.maowutv.com ~]# man grep

掌握了這些Linux命令,你的工作效率事半功倍

15、help

應(yīng)該說(shuō)這是個(gè)萬(wàn)能的命令,你幾乎可以在任何時(shí)候輸入這個(gè)命令尋求幫助,畢竟幾乎所有的命令行工具都帶有幫助頁(yè)面,顯示如何使用命令。有些命令獲取幫助是:command --help。help輸出內(nèi)容為man 命令的精簡(jiǎn)內(nèi)容。

例:

[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: --: 無(wú)效選項(xiàng)
cd: 用法:cd [-L|[-P [-e]]] [dir]
【騰訊云】云服務(wù)器、云數(shù)據(jù)庫(kù)、COS、CDN、短信等云產(chǎn)品特惠熱賣中

發(fā)表評(píng)論取消回復(fù)

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前評(píng)論:1   其中:訪客  1   博主  0

    • avatar pornpics 0

      Wish You All The Best In 2021!