Linux常用命令精講

一、Linux命令基礎

1、Linux命令的通用格式

命令字  [選項]  [參數]
  • 命令字:命令字是唯一的,嚴格區分大小寫;

  • 選項:用於調節命令的具體功能,可以有一個或者多個選項,選項分爲短格式和長格式,“-”表示短格式,例如“ls -a”;“--”表示長格式,例如“ls --help”;

  • 參數:參數是命令處理的對象,例如文件、目錄名等;

2、Linux命令作用

用於實現某一類功能的指令或程序,命令的執行依賴於解釋器程序(例如:/bin/bash)。

3、Linux命令的分類

1)內部命令

集成在bash中,安裝Linux系統自帶,不需要對應文件,執行速度快,屬於shell解釋器的一部分。

2)外部命令

獨立於shell解釋器之外的程序,安裝程序或者服務生成命令,需要對應到系統文件中,執行速度慢。

4、編輯Linux命令行的輔助操作

1)Tab鍵

自動補全命令

[root@centos01 ~]# whi  <!--輸入前三個字符按Tab鍵自動補全-->
which     while     whiptail  

2)反斜槓“\”

強制換行

[root@centos01 ~]# user\
> add bob            <!--強制換行創建bob用戶-->
[root@centos01 ~]# tail -2 /etc/passwd    <!--驗證是否創建成功-->
test:x:1000:1000:test:/home/test:/bin/bash  
bob:x:1001:1001::/home/bob:/bin/bash

3)快捷鍵Ctrl+U

清空至行首

[root@centos01 ~]# useradd  
[root@centos01 ~]# eradd  <!--將光標移至e字符執行Ctrl+u清空至行首-->

4)快捷鍵Ctrl+K

清空至行尾

[root@centos01 ~]# useradd
[root@centos01 ~]# use  <!--將光標移至e字符執行Ctrl+k清空至行尾-->

5)快捷鍵Ctrl+L

清空屏幕內容

6)快捷鍵Ctrl+C

取消本次命令編輯

[root@centos01 ~]# tail -2 /etc/passwd^C
[root@centos01 ~]# 

5、獲取命令幫助

1)內部命令help

查看Bash內部命令的幫助信息

[root@centos01 ~]# help pwd
pwd: pwd [-LP]
    打印當前工作目錄的名字。

    選項:
      -L    打印 $PWD 變量的值,如果它命名了當前的
        工作目錄
      -P    打印當前的物理路徑,不帶有任何的符號鏈接

    默認情況下,`pwd' 的行爲和帶 `-L' 選項一致

    退出狀態:
    除非使用了無效選項或者當前目錄不可讀,否則
    返回狀態爲0

2)命令的“--help”選項

適用於大多數外部命令。

[root@centos01 ~]# vim --help
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Aug  2 2017 00:45:39)

用法: vim [參數] [文件 ..]       編輯指定的文件
  或: vim [參數] -               從標準輸入(stdin)讀取文本
  或: vim [參數] -t tag          編輯 tag 定義處的文件
  或: vim [參數] -q [errorfile]  編輯第一個出錯處的文件

參數:
   --           在這以後只有文件名
   -v           Vi 模式 (同 "vi")
   -e           Ex 模式 (同 "ex")
   -E           Improved Ex mode
   -s           安靜(批處理)模式 (只能與 "ex" 一起使用)
   -d           Diff 模式 (同 "vimdiff")
   -y           容易模式 (同 "evim",無模式)
   -R           只讀模式 (同 "view")
   -Z           限制模式 (同 "rvim")
   -m           不可修改(寫入文件)
   -M           文本不可修改
   -b           二進制模式
   -l           Lisp 模式
   -C           兼容傳統的 Vi: 'compatible'
   -N           不完全兼容傳統的 Vi: 'nocompatible'
   -V[N][fname]     Be verbose [level N] [log messages to fname]
   -D           調試模式
   -n           不使用交換文件,只使用內存
   -r           列出交換文件並退出
   -r (跟文件名)        恢復崩潰的會話
   -L           同 -r
   -A           以 Arabic 模式啓動
   -H           以 Hebrew 模式啓動
   -F           以 Farsi 模式啓動
   -T <terminal>    設定終端類型爲 <terminal>
   -u <vimrc>       使用 <vimrc> 替代任何 .vimrc
   --noplugin       不加載 plugin 腳本
   -P[N]        打開 N 個標籤頁 (默認值: 每個文件一個)
   -o[N]        打開 N 個窗口 (默認值: 每個文件一個)
   -O[N]        同 -o 但垂直分割
   +            啓動後跳到文件末尾
   +<lnum>      啓動後跳到第 <lnum> 行
   --cmd <command>  加載任何 vimrc 文件前執行 <command>
   -c <command>     加載第一個文件後執行 <command>
   -S <session>     加載第一個文件後執行文件 <session>
   -s <scriptin>    從文件 <scriptin> 讀入正常模式的命令
   -w <scriptout>   將所有輸入的命令追加到文件 <scriptout>
   -W <scriptout>   將所有輸入的命令寫入到文件 <scriptout>
   -x           編輯加密的文件
   --startuptime <file> Write startup timing messages to <file>
   -i <viminfo>     使用 <viminfo> 取代 .viminfo
   -h  或  --help    打印幫助(本信息)並退出
   --version        打印版本信息並退出

3)使用man命令閱讀手冊頁

使用“⬆”、“⬇”方向鍵滾動文本,使用page Up和page Down鍵翻頁,按Q或q鍵退出閱讀環境、按“/”鍵查找內容。

[root@centos01 ~]# man pwd
PWD(1)                                                          FSF                                                         PWD(1)

NAME
       pwd - 顯示出當前/活動目錄的名稱

總覽 (SYNOPSIS)
       pwd [OPTION]

描述 (DESCRIPTION)
       顯示出 完整的 當前 活動目錄 名稱.

       --help 顯示 幫助 信息, 然後 退出

       --version
              顯示 版本 信息, 然後 退出

報告 BUGS
       發現的 bug 寄往 <[email protected]>.

另見 (SEE ALSO)
       Pwd 的 完整文檔 以 Texinfo 手冊 的 形式 維護. 如果 正確 安裝了 info 和 pwd 文件, 用 命令

              info pwd

       可以 訪問 完整 的 手冊.

版權 (COPYRIGHT)
       Copyright © 1999 Free Software Foundation, Inc.
       This  is  free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FIT‐
       NESS FOR A PARTICULAR PURPOSE.

[中文版維護人]
       徐明 <[email protected]>

[中文版最新更新]
       2003/05/13

《中國Linux論壇man手冊頁翻譯計劃》
       http://cmpp.linuxforum.net

6、ls(list)命令詳解

ls命令選項

  • -l:以長格式顯示文件和目錄的列表;

  • -a:顯示所有子目錄和文件的信息;

  • -A:與-a選項的作用基本類似,但有兩個特殊隱藏目錄不會顯示,“.”和“..”;

  • -d:顯示目錄本身的屬性,而不是顯示目錄中的內容;

  • -h:以更人性化的方式顯示出目錄或文件的大小,此選項需要結合-l選項一起使用;

  • -R:以遞歸的方式顯示指定目錄及其子目錄中的所有內容;

  • --color:顯示顏色;

應用舉例:

[root@centos01 ~]# ls -l /
總用量 102428
-rw-r--r--    1 root root 104857600 1月   9 21:42 1.iso
drwxr-xr-x    3 root root        18 1月   9 21:22 benet
lrwxrwxrwx.   1 root root         7 10月 23 22:29 bin -> usr/bin
dr-xr-xr-x.   5 root root      4096 10月 23 22:37 boot
drwxr-xr-x   18 root root      3140 1月   9 21:00 dev
[root@centos01 ~]# ls -ld /     <!--長格式查看根的屬性-->
dr-xr-xr-x. 19 root root 269 1月   9 21:31 /
[root@centos01 ~]# ls -ld /test/yun/benet/1.txt    <!--長格式查看文件屬性-->
-rw-r--r-- 1 root root 0 1月   9 21:40 /test/yun/benet/1.txt
[root@centos01 benet]# dd if=/dev/zero of=./1.iso bs=10M count=10      <!--創建測試數據-->
記錄了10+0 的讀入
記錄了10+0 的寫出
104857600字節(105 MB)已複製,1.13446 秒,92.4 MB/秒

[root@centos01 benet]# ls -lh ./1.iso    
        <!--長格式顯示文件屬性;以KB或者M顯示文件大小-->
-rw-r--r-- 1 root root 100M 1月   9 21:43 ./1.iso
[root@centos01 ~]# ls -lhR /test/ <!--遞歸顯示目錄中包含的子文件大小-->
/test/:
總用量 0
drwxr-xr-x 3 root root 19 1月   9 21:31 yun

/test/yun:
總用量 0
drwxr-xr-x 2 root root 84 1月   9 21:43 benet

/test/yun/benet:
總用量 100M
-rw-r--r-- 1 root root 100M 1月   9 21:43 1.iso
-rw-r--r-- 1 root root    0 1月   9 21:40 1.txt
-rw-r--r-- 1 root root    0 1月   9 21:40 2.txt
-rw-r--r-- 1 root root    0 1月   9 21:40 3.txt
-rw-r--r-- 1 root root    0 1月   9 21:40 4.txt
-rw-r--r-- 1 root root    0 1月   9 21:40 5.txt

7、du(disk usage)命令詳解

用於統計指定目錄(或)文件所佔磁盤空間的大小,du命令常用的幾個選項如下:

  • -a:統計磁盤空間佔用時包括所有的文件,而不僅僅只統計目錄;

  • -h:以更人性化的方式(默認以KB計數,但不顯示單位)顯示出統計結果;

  • -s:只統計所佔用空間總的(Summary)大小,而不是統計每個子目錄,文件的大小;

應用舉例:

[root@centos01 ~]# df -Th    <!--統計磁盤大小-->
文件系統       類型      容量  已用  可用 已用% 掛載點
/dev/sda3      xfs        76G  3.8G   72G    5% /
devtmpfs       devtmpfs  474M     0  474M    0% /dev
tmpfs          tmpfs     489M     0  489M    0% /dev/shm
tmpfs          tmpfs     489M  7.0M  482M    2% /run
tmpfs          tmpfs     489M     0  489M    0% /sys/fs/cgroup
/dev/sda1      xfs       197M  136M   61M   70% /boot
tmpfs          tmpfs      98M     0   98M    0% /run/user/0
[root@centos01 ~]# du -sh /var/log/   
              <!--統計所有文件總共佔用的空間大小-->
4.9M    /var/log/
[root@centos01 ~]# du -sh /test/   <!--統計目錄大小-->
100M    /test/
[root@centos01 ~]# du -ah /test/yun/benet/1.iso<!--統計文件大小-->
100M    /test/yun/benet/1.iso

二、目錄和文件的基本操作

1、touch命令

touch命令用於創建空文件,用於測試,當目標文件已存在時,將更新該文件的時間標記。

應用舉例:

[root@centos01 ~]# touch /test/1.iso  <!--創建單個文件-->
[root@centos01 ~]# cd /test/    
[root@centos01 test]# ls         <!--驗證-->
1.iso
[root@centos01 ~]# touch /test/{1..10}.txt   <!--連續創建多個文件-->
[root@centos01 ~]# 
[root@centos01 ~]# cd /test/
[root@centos01 test]# ls      <!--驗證創建的文件-->
10.txt  1.iso  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

2、常用命令——mkdir(make directory)

mkdir命令用於創建新目錄,語法格式如下:

mkdir  [選項]  目錄位置及名稱...
  • -p:-p選項表示一次性創建嵌套的多層目錄;

應用舉例:

[root@centos01 ~]# mkdir www                  <!--創建目錄-->
[root@centos01 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  www
[root@centos01 ~]# mkdir -p /test/yun/benet   <!--循環創建目錄-->
[root@centos01 ~]# cd /test/yun/benet/
[root@centos01 benet]# pwd
/test/yun/benet 

3、常用命令——cp(copy)

複製文件或目錄,需要複製多個文件或目錄時,目標位置必須是目錄,而且目標目錄必須以及存在。語法格式如下:

cp  [選項]...  源文件或目錄...  目標文件或目錄...

cp常用的選項如下:

  • -f:覆蓋目錄同名文件或目錄時不進行提醒,直接強制複製;

  • -i:覆蓋目錄同名文件或目錄時提醒用戶確認;

  • -p:複製時保持源文件的權限,屬主及時間標記等屬性不變;

  • -r:複製目錄時必須使用此選項,表示遞歸複製所有文件及子目錄;

應用舉例:

[root@centos01 ~]# cp -r benet/ www/ <!--複製目錄到benet目錄中-->
[root@centos01 ~]# cp -rpf /test/ ./yun/ 
<!--循環複製/test目錄中的數據到當前目錄yun中,
test目錄中的數據保持權限不變-->

4、常用命令——rm(remove)

rm命令用於刪除指定的文件或目錄,語法格式如下:

rm  [選項]  要刪除的文件或目錄...

rm命令常用的選項如下:

  • -f:不提醒,直接強制刪除;

  • -i:提醒用戶確認;

  • -r:遞歸刪除整個目錄樹;

應用舉例:

[root@centos01 test]# ls
10.txt  1.iso  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt  yun
[root@centos01 test]# rm -rf yun  <!--不提醒遞歸刪除yun目錄-->
[root@centos01 test]# rm -rf ./*.iso 
              <!--不提醒遞歸刪除所有以*.iso結尾的文件-->
[root@centos01 test]# ls
10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt
[root@centos01 test]# rm -ri ./*           <!--提醒用戶確認刪除當前目錄所有文件和目錄-->
rm:是否刪除普通空文件 "./10.txt"?y   <!--輸入y確認即可-->
rm:是否刪除普通空文件 "./1.txt"?y
............              <!--此處省略部分內容-->

rm -rf命令極度危險,慎重操作!!!

5、常用命令——mv(move)

mv命令用於將指定的文件或目錄轉移位置,如果目標位置與源位置相同,則相當於執行重命名操作。mv命令語法格式如下:

mv [選項]...  源文件或目錄...  目標文件或目錄

應用舉例:

[root@centos01 ~]# mv 1.mp3 /benet/bdqn/accp/xsh/
<!--將當前目錄的1.mp3數據剪切到/benet/bdqn/accp/xsh/目錄-->
[root@centos01 xsh]# ls
1.mp3
[root@centos01 xsh]# mv 1.mp3 2.mp3 <!--將1.mp3重命名爲2.mp3-->
[root@centos01 xsh]# ls
2.mp3

6、常用命令——which

which命令用來查找Linux命令程序所在的位置,注意:默認當找到第一個目標後不再繼續查找;若在所有搜素路徑中查找,加“-a”選項;無法查找內部命令所對應的程序文件,例如:which cd。語法格式如下:

which  命令|程序名

應用舉例:

[root@centos01 ~]# which passwd <!--查看passwd命令的位置-->
/usr/bin/passwd
[root@centos01 ~]# which -a groupadd <!--查看groupadd命令的位置-->
/usr/sbin/groupadd

7、常用命令——find

find命令用來精細查找文件或目錄。基本語法格式如下:

find  [查找範圍]  [查找條件表達式]

find常用的查找條件如下:

  • -name:按名稱查找;根據目標文件的名稱進行查找,允許使用“*”及“?”通配符;

  • -size:按文件大小查找;一般使用“+”、“-”號設置超過或小於指定的大小作爲查找條件。常用的容量單位包括kB(注意k是小寫)、MB、GB;

  • -user:按文件屬主查找;

  • -type:按文件類型查找;類型指的是普通文件(f)、目錄(d)、塊設備文件(b)、字符設備文件(c)等。

同時使用多個查找條件時,各表達式之間可以使用邏輯運算符“-a”、“-o”,分別表示而且(and)、或者(or)。

應用舉例:

[root@centos01 ~]# find /test/yun/benet/ -name *.txt 
        <!--查看/test/yun/benet/目錄下所有*.txt的文件-->
/test/yun/benet/1.txt
/test/yun/benet/2.txt
/test/yun/benet/3.txt
/test/yun/benet/4.txt
/test/yun/benet/5.txt
[root@centos01 ~]# find /test/yun/benet/ -type d   
        <!--查看/test/yun/benet/目錄中所有目錄-->
/test/yun/benet/
[root@centos01 ~]# find /test/yun/benet/ -type f   
        <!--查看/test/yun/benet/目錄中所有文件-->
/test/yun/benet/1.txt
/test/yun/benet/2.txt
/test/yun/benet/3.txt
/test/yun/benet/4.txt
/test/yun/benet/5.txt
/test/yun/benet/1.iso
[root@centos01 ~]# find /test/yun/benet/ -size +100M  
     <!--查看/test/yun/benet/目錄中大於100M的文件-->
/test/yun/benet/2.iso
[root@centos01 ~]# find /test/yun/benet/ -size -100M   
      <!--查看/test/yun/benet/目錄中小於100M的文件-->
/test/yun/benet/
/test/yun/benet/1.txt
/test/yun/benet/2.txt
/test/yun/benet/3.txt
/test/yun/benet/4.txt
/test/yun/benet/5.txt
[root@centos01 ~]# find /test/yun/benet/ -size +100M -a -name *.iso   
<!--查看/test/yun/benet/目錄中大於100M和名字爲*.iso的顯示出來-->
/test/yun/benet/2.iso
[root@centos01 ~]# find /test/yun/benet/ -size +100M -o -name *.iso       
  <!--查看/test/yun/benet/目錄中大於100M或者名字爲*.iso的顯示出來-->
/test/yun/benet/1.iso
/test/yun/benet/2.iso

8、日期和日曆的查看方式

顯示日期的命令:

[root@centos01 ~]# date
2020年 01月 10日 星期五 22:05:54 CST
[root@centos01 ~]# date +%F
2020-01-10
[root@centos01 ~]# date +%Y/%m/%d
2020/01/10
[root@centos01 ~]# date +%H:%M
22:07

顯示日曆的命令:

[root@centos01 ~]# cal
      一月 2020     
日 一 二 三 四 五 六
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

[root@centos01 ~]# cal 22 11 1999
     十一月 1999    
日 一 二 三 四 五 六
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

———————— 本文至此結束,感謝閱讀 ————————

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章