head和tail命令詳解

   

基礎命令學習目錄首頁html

 

原文連接:https://www.cnblogs.com/amosli/p/3496027.htmllinux

當要查看上千行的大文件時,咱們可不會用cat命令把整個文件內容給打印出來,相反,咱們可能只須要看文件的一小部分地內容(例如文件的前十行和後十行),咱們也有可能須要打印出來前n行或後n行,也有可能打印除了前n行或後n行以外的全部行,也有可能須要實時監控log日誌的更新,那麼怎麼實現呢?下面一塊兒來看一下linux下使用率極高的head ,tail兩個命令。vim

1、head命令詳解

首先,輸入head --help查看幫助信息:app

複製代碼
amosli@amosli-pc:~/learn/fd$ head --help
Usage: head [OPTION]... [FILE]...
Print the first 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -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

K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
複製代碼

head命令的語法格式爲:

head [OPTION]... [FILE]...

接下來將在實例中講解各參數含義及用法:post

實例:

1.使用head命令查看文件內容前十行

新建test.txt,共14行.學習

複製代碼
amosli@amosli-pc:~/learn/fd$ cat -n test.txt 
     1    a
     2    b
     3    c
     4    d
     5    e
     6    f
     7    g
     8    h
     9    i
    10    j
    11    k
    12    l
    13    m
    14    n
複製代碼

使用head命令查看前十行,head命令默認顯示文件前十行ui

複製代碼
amosli@amosli-pc:~/learn/fd$ head test.txt 
a
b
c
d
e
f
g
h
i
j
複製代碼

2.-n參數,顯示test.txt文件的前3行

amosli@amosli-pc:~/learn/fd$ head -n 3 test.txt 
a
b
c

英文提示信息:this

  -n, --lines=[-]K         print the first K lines instead of the first 10;                   

 

3.-n參數顯示除了文件最後3行外的全部內容

複製代碼
amosli@amosli-pc:~/learn/fd$ head -n -3 test.txt 
a
b
c
d
e
f
g
h
i
j
k
複製代碼

英文提示信息:url

  -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

加上'-',打印全部內容除了最後的K行。spa

4.-c參數,按文件內容大小來打印,打印前2個字節的內容

amosli@amosli-pc:~/learn/fd$ head -c 2 test.txt 
a

2個字節就是一個「a」字母。

英文提示信息:

  -c, --bytes=[-]K         print the first K bytes of each file;

5.-c參數,打印除了最後2個字節的文件內容

複製代碼
amosli@amosli-pc:~/learn/fd$ head -c -2 test.txt 
a
b
c
d
e
f
g
h
i
j
k
l
m
複製代碼

英文提示信息:

  -c, --bytes=[-]K         print the first K bytes of each file;
                             with the leading `-', print all but the last
                             K bytes of each file

6.-q參數,打印時不顯示文件名稱

複製代碼
amosli@amosli-pc:~/learn/fd$ head -q test.txt 
a
b
c
d
e
f
g
h
i
j
複製代碼

英文提示信息:

  -q, --quiet, --silent    never print headers giving file names

其實後面跟上--quiet,--silent都是同樣的,都不會顯示文件名稱,和默認打印是同樣的效果。

7.-v參數,打印是顯示文件名稱

複製代碼
amosli@amosli-pc:~/learn/fd$ head -v test.txt 
==> test.txt <==
a
b
c
d
e
f
g
h
i
j
複製代碼

英文提示信息:

  -v, --verbose            always print headers giving file names

其中,用--verbose和-v顯示的是同樣的效果

複製代碼
amosli@amosli-pc:~/learn/fd$ head --verbose test.txt 
==> test.txt <==
a
b
c
d
e
f
g
h
i
j
複製代碼

8.打印多個文件的內容

複製代碼
amosli@amosli-pc:~/learn/fd$ head -n 3 test.txt test2.txt 
==> test.txt <==
a
b
c

==> test2.txt <==
c
d
e
複製代碼

 

2、tail命令詳解

tail命令和head 命令很是類似,只不過它是打印文件的尾部內容的,固然也有一些特點之處,下面一塊兒來看看吧。

首先,輸入tail --help看一下提示信息

複製代碼
amosli@amosli-pc:~/learn/fd$ tail --help 
Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -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

If the first character of K (the number of bytes or lines) is a `+',
print beginning with the Kth item from the start of each file, otherwise,
print the last K items in the file.  K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

With --follow (-f), tail defaults to following the file descriptor, which
means that even if a tail'ed file is renamed, tail will continue to track
its end.  This default behavior is not desirable when you really want to
track the actual name of the file, not the file descriptor (e.g., log
rotation).  Use --follow=name in that case.  That causes tail to track the
named file in a way that accommodates renaming, removal and creation.
複製代碼

語法格式:

tail [OPTION]... [FILE]...

這裏因爲head和tail實在比較像,這裏爲了節省篇幅,對類似之處將盡可能簡潔

實例:

test2.txt,共有12行內容爲從c-n

複製代碼
amosli@amosli-pc:~/learn/fd$ cat -n test2.txt 
     1    c
     2    d
     3    e
     4    f
     5    g
     6    h
     7    i
     8    j
     9    k
    10    l
    11    m
    12    n
複製代碼

 

1.-c 參數,根據文件字節進行輸出打印

  -c, --bytes=K            output the last K bytes; alternatively, use -c +K
                           to output bytes starting with the Kth of each file

打印test2.txt中的最後4 bytes,以下:

amosli@amosli-pc:~/learn/fd$ tail -c 4 test2.txt 
m
n

tail -c +4 test2.txt 加上一個‘+’會是什麼效果呢?

複製代碼
amosli@amosli-pc:~/learn/fd$ tail -c +4 test2.txt 

e
f
g
h
i
j
k
l
m
n
複製代碼

少打印了c d 兩個字母,那麼 -c +K的意思也就很明瞭了,即打印文件的全部內容除了前面的K個字節

二、-n參數,根據文件行數進行打印

看一下提示信息:

複製代碼
 -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
複製代碼

打印test2.txt最後的3行內容:

amosli@amosli-pc:~/learn/fd$ tail -n 3 test2.txt 
l
m
n

從第3行開始輸出test2.txt的全部內容:

複製代碼
amosli@amosli-pc:~/learn/fd$ tail -n +3 test2.txt 
e
f
g
h
i
j
k
l
m
n
複製代碼

3.-q參數,-v參數

 -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

不打印文件名稱信息:

複製代碼
amosli@amosli-pc:~/learn/fd$ tail -q  test2.txt 
e
f
g
h
i
j
k
l
m
n
複製代碼

打印文件名稱信息:

複製代碼
amosli@amosli-pc:~/learn/fd$ tail -v test2.txt 
==> test2.txt <==
e
f
g
h
i
j
k
l
m
n
複製代碼

4、-f參數

tail 命令的一個很重要的用法是從一個內容不斷增長的文件中讀取數據。新增長的內容部民被添加到文件的尾部,所以當新內容被寫入文件的時候,能夠用tail將其顯示出來。只是簡單的使用tail的話,它會讀取文件的最後10行,而後退出,這樣就不能作到實時監控,加入-f參數就能夠作到實時監控文件的更新內容了。

複製代碼
amosli@amosli-pc:~/learn/fd$ tail -f test2.txt 
g
h
i
j
k
l
m
n
o
p
複製代碼

ctrl+alt+t新開一個終端,而後執行下面的命令:

amosli@amosli-pc:~/learn/fd$ echo  'xyz' >> test2.txt

這個時候你就能夠看到前一個終端在裏出現了‘xyz’

複製代碼
amosli@amosli-pc:~/learn/fd$ tail -f test2.txt 
g
h
i
j
k
l
m
n
o
p
xyz
複製代碼

這樣就能實時監控項目裏的log日誌了。

若是想設定一個間隔時間去查看文件的更新應該怎麼作呢?請看-s參數

五、-s參數

英文提示信息:

-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.

如每隔5秒查看一次test2.txt的內容是否更新

複製代碼
amosli@amosli-pc:~/learn/fd$ tail -f -s 5 test2.txt
j
k
l
m
n
o
p
xyz
複製代碼

 默認是1秒更新一次。

6.--pid參數

 tail 具備一個很意思的特性,當某個給定進程結束以後,tail也會隨之終結.

假如咱們正在讀取一個不斷增加的文件,進程Foo一直在向該文件追加數據,那麼tail就會一直執行,直到進程Foo的結束.

$PID=$(pidof Foo)
$tail -f file --pid $PID
#當進程Foo結束以後,tail也會跟着結束

例如用gedit打開test2.txt,不斷加入數據,而後在終端裏使用tail進行監聽,具體爲:

複製代碼
amosli@amosli-pc:~/learn/fd$ PID=$(pidof gedit)
amosli@amosli-pc:~/learn/fd$ tail -f -s 2 test2.txt --pid $PID
h
i
j
k
l
m
n
o
p
xyz
複製代碼

而後不斷在gedit中追加入‘yyy’後保存,按理說終端裏應該會更新,但個人終端不知爲什麼沒有更新數據,這裏就不帖出來了。

關閉gedit後,tail監控也關閉掉了。

 

head和tail取文件第m行到第n行

[root@xiaoma /root/mcw] test!
#vim test.txt
[root@xiaoma /root/mcw] test!
#cat test.txt
1 ma
2 chang
3 wei
4 mo
5 jiang
[root@xiaoma /root/mcw] test!
#cat test.txt |head -4|tail -2 >test1.txt
[root@xiaoma /root/mcw] test!
#cat test1.txt 3 wei 4 mo
相關文章
相關標籤/搜索