head & tail command

1.tail

tail command is to use the last part of the specified file to the output device in accordance with the requirements of the standard,

Usually terminal, popular terms, is to display the last few lines of a file to a file on the terminal,

If the file is updated, tail automatically refresh, make sure you see the latest contents of the file.

tail command syntax

tail   [ -f ]   [ -c Number | -n Number | -m Number| -b Number | -k Number ]    [ File ]

Parameter Description:

-f This parameter is used to monitor File file growth.

-C Number Number reads the specified file from byte position

-n Number Reads the specified file from the Number line position.

-m Number Number read from the multi-byte character position specified file, such as your file if included in the text, if you specify -c parameter may result in truncated, but using -m will avoid this problem.

-B Number 512-byte block location indicated by the Number Reads the specified file.

Number -k reads the specified file from 1KB block location indicated Number.

File destination filename specified action

In the above command, it is related to Number, if not specified, the default display 10 lines . Number may be used in front of the sign, which indicates the offset from the top is counted from the end.

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; or use -c +K to output
                             bytes starting with the Kth of each file
  -f, --follow[={name|descriptor}]
                           output appended data as the file grows;
                             an absent option argument means 'descriptor'
  -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 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 if it is inaccessible
  -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 command usage examples

1、tail -f filename

Description: Monitor filename end of the file contents ( default 10 lines, the equivalent of adding parameter -n 10), refresh the display on the screen. Exit, press CTRL + C.

2、tail -n 20 filename

Description: Displays the last 20 lines of filename.

3、tail -n +10 filename

Description: filename displayed after the first 10 rows. (Show incomplete)

 

2.head

head command displays a block of text files

1, format

  head Parameters [file]

2, parameter

  -q hidden file name (default)

  -v displays the file name

  -c <byte> shows the number of bytes

  -n <line number> number of lines displayed (default)

 

cat test.sh==》
No Name Mark Percent
tom 69 91
jack 71 87
alex 68 98

 

[centos@s203 ~]$ head -n 10 test.sh
No Name Mark Percent
tom 69 91
jack 71 87
alex 68 98
[centos@s203 ~]$ head -n 2 test.sh
No Name Mark Percent
tom 69 91
[centos@s203 ~]$ head -n 2 -v test.sh
==> test.sh <==
No Name Mark Percent
tom 69 91
[centos@s203 ~]$ head -n 2 -v -q test.sh
No Name Mark Percent
tom 69 91
[centos@s203 ~]$ head -n 2 -v -q -c 3 test.sh
No [centos@s203 ~]$
[centos@s203 ~]$
[centos@s203 ~]$
[centos@s203 ~]$ head  -v -q -c 3 test.sh
No [centos@s203 ~]$ head -n 2 -v -q -c 100 test.sh
No Name Mark Percent
tom 69 91
jack 71 87
alex 68 98
[centos@s203 ~]$ head -n 2 -v -q -c 10 test.sh
No Name Ma[centos@s203 ~]$

 

Guess you like

Origin www.cnblogs.com/wqbin/p/10932587.html