Linux查詢檔案命令大全

來源:互聯網
上載者:User

1.cat  2.tac  3.nl  4.more  5.less  6.head  7.tail  8.od 

一:直接查看檔案內容命令:
1、cat(concatenate)
參數:
-A :相當於-vET的整合參數,可列出一些特殊字元,而不是空白;
-b :列出行號,僅針對非空白行做行號顯示,空白行不標行號;
-E :將結尾的斷行字元 $ 顯示出來;
-n :列印出行號,連同空白行也會有行號,與 -b 的參數不同;
-T :將[tab]按鍵以^T顯示出來;
-v :列出一些看不出來的特殊字元;

例一:查看/etc/issue這個檔案的內容
[root@localhost ~]# cat /etc/issue
Red Hat Enterprise Linux Server release 5.4 (Tikanga)
Kernel \r on an \m
(注意:這還有個空行)

例二:如果要加行號用-n
[root@localhost ~]# cat -n /etc/issue
    1  Red Hat Enterprise Linux Server release 5.4 (Tikanga)
    2  Kernel \r on an \m
    3
這樣對於大檔案要找某個特定的行時比較有用
如果不想要編排空白的行號,可以使用“cat -b /etc/issue”
[root@localhost ~]# cat -b /etc/issue
    1  Red Hat Enterprise Linux Server release 5.4 (Tikanga)
    2  Kernel \r on an \m

2、tac(反向列示)
參數(沒有)
執行個體:
[root@localhost ~]# tac /etc/issue
(注意:這有個空行)
Kernel \r on an \m
Red Hat Enterprise Linux Server release 5.4 (Tikanga)

3、nl(添加行號列印)
參數:
-b :指定行號指定的方式,主要有兩種:
-b  a :表示不論是否為空白行,也同樣列出行號(類似於cat -n)
-b  t :如果有空行,空的那一行不要列出行號(預設值)
-n :列出行號表示的方式,主要有三種:
-n  ln :行號在螢幕的最左方顯示;
-n  rn :行號在自己欄位的最右方顯示,且不加0;
-n  rz :行號在自己欄位的最右方顯示,且加0;
-w :行號欄位佔用的位元

例一:
[root@localhost ~]# nl /etc/issue
    1  Red Hat Enterprise Linux Server release 5.4 (Tikanga)
    2  Kernel \r on an \m
      (這有個空行,nl預設空行不編排)
注意:這個檔案其實有三行,第三行為空白(沒有任何字元),因為它是空白行,所    以nl不會加上行號,如果要加上行號,可以做如下...
[root@localhost ~]# nl -b a /etc/issue
    1  Red Hat Enterprise Linux Server release 5.4 (Tikanga)
    2  Kernel \r on an \m
    3
如果想讓行號前面自動補上0,可做如下...
[root@localhost ~]# nl -b a  -n rz /etc/issue
000001  Red Hat Enterprise Linux Server release 5.4 (Tikanga)
000002  Kernel \r on an \m
000003\
自動在自己欄位的地方補上0了,預設欄位是6位元,如果想要改成其他位,如下...
[root@localhost ~]# nl -b a  -n rz -w 3  /etc/issue
001    Red Hat Enterprise Linux Server release 5.4 (Tikanga)
002    Kernel \r on an \m
003

二:可翻頁查看:
4、more(一頁一頁翻動)
參數:
空格鍵: 代表向下翻一頁;
Enter: 代表向下滾動一行;
/字串: 代表在這個現實的內容當中向下查詢‘字串’這個關鍵字;
: f : 立刻顯示出檔案名稱以及目前顯示的行數;
q : 代表立刻離開more,不在顯示該檔案內容;
B或[ctrl]-b:代表往回翻頁,不過這操作只對檔案有用,對管道無用。

5、less(一頁一頁翻動)(l與more的區別在於less可以往前翻頁)
參數:
空格鍵: 向下翻動一頁;
[PageDown] 向下翻動一頁;
[PageUp] 向上翻動一頁;
/字串 向下查詢“字串”的功能;
?字串 向上查詢“字串”的功能;
n 重複前一個查詢(與/或?有關);
N  反向重複前一個查詢(與/或?有關);
q  離開less這個程式;

PS:less使用的介面和環境與man page非常的類似,因為man這個命令就是調用less 來顯示說明檔案的內容

三:資料選取:
6、head(取出前面幾行)
參數:
-n :後面接數字,代表顯示幾行的意思

預設顯示前十行,若要更改,,如下...


   
如果後面135行資料都不列印,則可以這樣...

7、tail(取出後面幾行)
參數:
-n :後面接數字,代表顯示幾行的意思
-f :表示持續檢測後面所接的檔案名稱,要等到按下[ctrl]-c才會結束tail的檢測
例一:
[root@localhost ~]# tail /etc/man.config
    預設情況中,顯示最後的10行,若要顯示最後的20行,如下..
[root@localhost ~]# tail-n 20 /etc/man.config 
如果不知道/etc/man.config有幾行,卻只想列出100行以後的資料時可以這樣做..
[root@localhost ~]# tail -n +20 /etc/man.config 
-f範例(由於/etc/man.config隨時會有資料寫入,你想要讓該檔案有資料寫入時就立刻
顯示到螢幕上,就利用-f這個參數)
[root@localhost ~]# tail -f /etc/man.config 
要等到輸入[crtl]-c之後才會離開tail這個命令的檢測
tail,less範例:如果想要顯示/etc/man.config的第11到20行這可做如下...
[root@localhost ~]# head -n 20 /etc/man.config | tail -n 10

四:非純文字檔案
8、od(非純文字檔案)
參數:
-t :後面可以接各種“類型(TYPE)”的輸出,例如:
    a        利用預設的字元來輸出
    c        使用ASCII字元來輸出
    d(size)  利用十進位來輸出資料,每個整數佔用size bytes
    f(size)  利用浮點來輸出資料,每個數佔用size bytes
    o(size)  利用八進位來輸出資料,每個認證佔用size bytes
    x(size)  利用十六進位來輸出資料,每個整數佔用size bytes

範例一:將/usr/bin/passwd的內容使用ASCII方式來輸出
[root@localhost ~]# od -t c /etc/issue
0000000  R  e  d      H  a  t      E  n  t  e  r  p  r  i
0000020  s  e      L  i  n  u  x      S  e  r  v  e  r   
0000040  r  e  l  e  a  s  e      5  .  4      (  T  i  k
0000060  a  n  g  a  )  \n  K  e  r  n  e  l      \  r   
0000100  o  n      a  n      \  m  \n  \n

0000112範例二:將/etc/issue這個檔案的內容以八進位列出儲存值與ASCII的對照表
[root@localhost ~]# od -t oCc /etc/issue
0000000 122 145 144 040 110 141 164 040 105 156 164 145 162 160 162 151
          R  e  d      H  a  t      E  n  t  e  r  p  r  i
0000020 163 145 040 114 151 156 165 170 040 123 145 162 166 145 162 040
          s  e      L  i  n  u  x      S  e  r  v  e  r   
0000040 162 145 154 145 141 163 145 040 065 056 064 040 050 124 151 153
          r  e  l  e  a  s  e      5  .  4      (  T  i  k
0000060 141 156 147 141 051 012 113 145 162 156 145 154 040 134 162 040
          a  n  g  a  )  \n  K  e  r  n  e  l      \  r   
0000100 157 156 040 141 156 040 134 155 012 012
          o  n      a  n      \  m  \n  \n
0000112

小結:其中nl和cat和tac都是一次性將所有的資料顯示到螢幕上面;
若要一頁一頁的翻動可使用命令more和less;還可以互動使用,比如     
[root@localhost ~]# od -t oCc /usr/bin/passwd | more,但這樣最後一行顯示的是“more”,若用more直接輸出的話,最後一行顯示的是百分比

Linux入門基礎命令詳解

Linux命令之cd

Linux命令之cat

Linux命令之alias/unalias

Linux命令解析:su root和su -root

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.