Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Linux 查看檔案內容的常用指令

看檔案內容的指令琳瑯滿目,各有用途:

  • cat:由第一行開始顯示全部內容。
  • tac:從最後一行開始顯示。
  • nl:顯示時連行號一起輸出。
  • more:一頁一頁顯示。
  • less:與 more 類似,但可往前翻頁。
  • head:只看頭幾行。
  • tail:只看結尾幾行。
  • od / hexdump:以二進位方式讀取內容。

常用用法

cat -n            # 連行號輸出,類似 nl
more test.txt     # 一頁頁顯示
ls -al | more     # 把 ls 輸出分頁顯示
less test.txt     # 可 pageup/pagedown 翻看
less 內按 /word    # 搜尋 word 字串
less 內按 :q       # 退出 less
head -n 20 test.txt   # 顯示前 20 行
tail -n 20 test.txt   # 顯示後 20 行
head -n 20 test.txt | tail -n 10   # 取第 11~20 行

其中 less 比 more 更有彈性,支援搜尋與雙向翻頁;head 搭配 tail 則能精準截取檔案中特定區段的行。

Copyright © Linux教程網 All Rights Reserved