Linux Learning Notes 13--command head and tail

Source: Internet
Author: User

Head and tail are as simple as their name, which is used to display the beginning or end of a number of chunks of text, the head is used to display the beginning of the file to the standard output, and tail to see the end of the file.

1. Command format:

Head [parameters] ... [File] ...

2. Command function:

The head is used to display the beginning of the file to the standard output, and the default head command prints the first 10 lines of its corresponding file.

3. Command parameters:

-Q Hide file names

-V Show file name

-c< bytes > Display bytes

-n< rows > rows displayed

4. Usage examples:

Example 1: Display the first n rows of a file

Command:

Head-n 5 Log2014.log

Output:

[email protected] test]# cat Log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12

==============================

[Email protected] test]# head-n 5 Log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05[[email protected] test]#

Example 2: Displaying the first n bytes of a file

Command:

Head-c Log2014.log

Output:

[Email protected] test]# head-c Log2014.log

2014-01

2014-02

2014

[Email protected] test]#

Example 3: The file's contents except the last N bytes

Command:

Head-c -32 Log2014.log

Output:

[Email protected] test]# head-c -32 log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12[[email protected] test]#

Example 4: Output file except the last n rows of the entire content

Command:

Head-n-6 Log2014.log

Output:

[Email protected] test]# Head-n-6 Log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07[[email protected] test]#

I used the head command:

HEAD-20 fileName: Displays the first 20 lines of the file.

##########################################################################

##########################################################################

The tail command writes the file to standard output starting at the specified point. Using the-f option of the tail command makes it easy to see the log files that are being changed, TAIL-F filename will display the most up-to-date contents of the filename on the screen, and not only refresh, so you can view the latest file content.

If more than one file is given, a filename header is prepended to each file that is displayed. If no file is specified or the file name is "-", the standard input is read.


1. command format;

tail[necessary parameters [selection parameters] [file]

2. Command function:

Used to display the content at the end of the specified file, and is processed as input when the file is not specified. Common view log files.

3. Command parameters:

-F Loop Read

-Q does not display processing information

-V displays detailed processing information

Number of-c< > bytes displayed

-n< lines > Display rows

--pid=pid is shared with-F, which means that the process ends after the id,pid dies.

-Q,--quiet,--silent never output the header of the file name

-S,--sleep-interval=s is combined with-F, which means sleep s seconds at each repetition interval

4. Usage examples:

Example 1: Displaying the end of a file

Command:

Tail-n 5 Log2014.log

Output:

[Email protected] test]# tail-n 5 Log2014.log

2014-09

2014-10

2014-11

2014-12

==============================[[email protected] test]#

Description

Displays the last 5 lines of the file

Example 2: Looping through the contents of a file

Command:

Tail-f Test.log

Output:

[Email protected] ~]# ping 192.168.120.204 > Test.log &

[1] 11891[[email protected] ~]# tail-f Test.log

PING 192.168.120.204 (192.168.120.204) bytes of data.

Bytes from 192.168.120.204:icmp_seq=1 ttl=64 time=0.038 ms

Bytes from 192.168.120.204:icmp_seq=2 ttl=64 time=0.036 ms

Bytes from 192.168.120.204:icmp_seq=3 ttl=64 time=0.033 ms

Bytes from 192.168.120.204:icmp_seq=4 ttl=64 time=0.027 ms

Bytes from 192.168.120.204:icmp_seq=5 ttl=64 time=0.032 ms

Bytes from 192.168.120.204:icmp_seq=6 ttl=64 time=0.026 ms

Bytes from 192.168.120.204:icmp_seq=7 ttl=64 time=0.030 ms

Bytes from 192.168.120.204:icmp_seq=8 ttl=64 time=0.029 ms

Bytes from 192.168.120.204:icmp_seq=9 ttl=64 time=0.044 ms

Bytes from 192.168.120.204:icmp_seq=10 ttl=64 time=0.033 ms

Bytes from 192.168.120.204:icmp_seq=11 ttl=64 time=0.027 ms

[Email protected] ~]#

Description

Ping 192.168.120.204 > Test.log &//Ping remote host in background. and output files to Test.log; This practice also allows for more than one file to monitor. Use CTRL + C to terminate.

Example 3: Displaying a file starting from line 5th

Command:

Tail-n +5 Log2014.log

Output:

[email protected] test]# cat Log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12

==============================

[Email protected] test]# tail-n +5 log2014.log

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12

==============================

The tail commands I use:

TAIL-200F FileName: Refreshes the contents of Fiename constantly.

Tail file (displays the last 10 lines of the files)

Tail +20 file (displays the contents of the files, from line 20th to the end of the file)

Tail-c file (displays the last 10 characters of the filename)


Linux Learning Notes 13--command head and tail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.