1. 程式人生 > >Linux head/tail命令詳解

Linux head/tail命令詳解

link display mes script following -- ati fir between

head命令用於顯示文件的開頭的內容。在默認情況下,head命令顯示文件的頭10行內容。

tail命令用於顯示文件的結尾的內容。在默認情況下,taild命令顯示文件的後10行內容。

head常見命令參數

  -c, --bytes=[-]K         print the first K bytes of each file;
                             with the leading `-‘, print all but the last
                             K bytes of each file
  -n, --lines=[-]K         print the first K lines instead of the first 10;
                             with the leading `-‘, print all but the last
                             K lines of each file
  -q, --quiet, --silent    never print headers giving file names
  -v, --verbose            always print headers giving file names
      --help     display this help and exit
      --version  output version information and exit

tail場景命令

 -c, --bytes=K            output the last K bytes; alternatively, use -c +K
                           to output bytes starting with the Kth of each file
  -f, --follow[={name|descriptor}]
                           output appended data as the file grows;
                           -f, --follow, and --follow=descriptor are
                           equivalent
  -F                       same as --follow=name --retry
  -n, --lines=K            output the last K lines, instead of the last 10;
                           or use -n +K to output lines starting with the Kth
      --max-unchanged-stats=N
                           with --follow=name, reopen a FILE which has not
                           changed size after N (default 5) iterations
                           to see if it has been unlinked or renamed
                           (this is the usual case of rotated log files).
                           With inotify, this option is rarely useful.
      --pid=PID            with -f, terminate after process ID, PID dies
  -q, --quiet, --silent    never output headers giving file names
      --retry              keep trying to open a file even when it is or
                             becomes inaccessible; useful when following by
                             name, i.e., with --follow=name
  -s, --sleep-interval=N   with -f, sleep for approximately N seconds
                             (default 1.0) between iterations.
                           With inotify and --pid=P, check process P at
                           least once every N seconds.
  -v, --verbose            always output headers giving file names
      --help     display this help and exit
      --version  output version information and exit

常用的命令展示

head :顯示文件的前幾行,默認10行  
head -n 2 /home/omc/h.txt
	==>head -n 3 == head -3    可以直接跟行數
cat h.txt | grep -v "hello"     過濾掉特定字符串,效率低,因為有管道
	==>grep -v "hello" h.txt  可以直接跟文件名,效率快
tail: 顯示文件最後幾行,默認10行
tail -10 /home/omc/h.txt    顯示最後10行
tail -f /home/omc/h.txt     實時跟蹤文件, 如果文件不存在,則終止 
tail -F /home/omc/h.txt     如果文件不存在,會繼續嘗試
head -30 /home/omc/h.txt | tail -11 /home/omc/h.txt   輸出一個文件的20-30行顯示文件的前n行
head -n 5 log2014.log輸出文件除了最後n行的全部內容
head -n -6 log2014.log從第5行開始顯示文件
tail -n +5 log2014.log    【顯示文件的全部內容】
tail -n 5  log2014.log    【只顯示最後的5行】  根more/less差不多

Linux head/tail命令詳解