Linux文本查看命令(cat,tac,rev,head,tail,more,less) 詳解

來源:互聯網
上載者:User

Linux文本查看命令(cat,tac,rev,head,tail,more,less) 詳解

Linux中我們總會有查看文本某些內容的時候,如果我們僅因為查看文本就使用vi進去檔案又增加了不少額外的操作不方便。下面介紹一些文本查看命令,大家看後應該會有所協助

--------------------------------------------------------------------------------
cat

cat - concatenate files and print on the standard output
表達格式:cat [OPTION]... [FILE]...
常用選項:
-A:顯示所有控制符
-n:顯示行數
-E:顯示行結束符

下面以幾個例子來具體說明cat及其選項的使用
實驗目錄/test  文本/test/cat1  /test/cat2
1234567891011121314 [root@localhost test]# ll /test/
總用量 8
-rw-r--r--. 1 root root 43 8月  7 16:46 cat1
-rw-r--r--. 1 root root 19 8月  7 16:46 cat2
[root@localhost test]# cat cat1   
hi,everyone
now I will show how to use cat
[root@localhost test]# cat -An cat1
    1  hi,everyone$
    2  now I will show how to use cat$
[root@localhost test]# cat -nE cat1 cat2
    1  hi,everyone$
    2  now I will show how to use cat$
    3  This is an example$

cat後面可以接多個檔案,常用選項可以組合使用

--------------------------------------------------------------------------------
tac  cat的反向顯示,按文本反向

tac - concatenate and print files in reverse 
tac [OPTION]... [FILE]...
[root@localhost test]# tac cat1
now I will show how to use cat
hi,everyone

tac選項都不常用,因此不予介紹

--------------------------------------------------------------------------------
rev  檔案內容按行反向顯示

rev - reverse lines of a file or files
rev [options] [file ...]
[root@localhost test]# rev cat1 
enoyreve,ih
tac esu ot woh wohs lliw I won

rev無有價值可介紹選項

--------------------------------------------------------------------------------
head

head - output the first part of files
head [OPTION]... [FILE]...
常用選項:
-c #:顯示前多少個位元組
-n #:顯示前多少行,n可以省去
head file 預設顯示檔案前十行內容
[root@localhost test]# head -3 head 
12345
67890
12
[root@localhost test]# head -c 3 head 
[root@localhost test]#

--------------------------------------------------------------------------------
tail

tail - output the last part of files
tail [OPTION]... [FILE]...
常用選項:
-c #:顯示後多少個位元組
-n #:顯示後多少行,n可以省去
-f :跟蹤顯示檔案新追加的內容,常用日誌監控
123456789101112 [root@localhost test]# tail -3 head 
345
123
6
[root@localhost test]# tail -c 5 head 
23
6
[root@localhost test]# tail -f /var/log/messages
Aug  7 17:15:04 localhost dhclient[9668]: DHCPDISCOVER on eno16777728 to 255.255.255.255 port 67 interval 11 (xid=0x7773726a)
Aug  7 17:15:05 localhost NetworkManager[916]: <warn>  (eno16777728): DHCPv4 request timed out.
Aug  7 17:15:05 localhost NetworkManager[916]: <info>  (eno16777728): DHCPv4 state changed unknown -> timeout
...

--------------------------------------------------------------------------------
more    特點:翻屏至檔案尾部後自動結束

more - file perusal filter for crt viewing
表達格式:more [options] file [...]
[root@localhost ~]# man /etc/init.d/functions
...
                ;;          stop)                  s=$"Stopping
$prog (via systemctl): "                  ;;          reload|try‐
reload)                  s=$"Reloading  $prog  configuration (via
systemctl): "                ;;        restart|try‐restart|con‐
drestart)                s=$"Restarting $prog (via systemctl): "
                ;;        esac
 Manual page functions line 1 (press h for help or q to quit)

--------------------------------------------------------------------------------
less

less - opposite of more
在less中可以使用之前在man裡面的搜尋功能,可以簡單的認為less是more的加強版。

less翻屏至檔案尾部後不自動結束。

本文永久更新連結地址:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.