Linux common commands: head command

  Head and tail are as easy to understand as their names, it is used to display a certain number of text blocks at the beginning or end, head is used to display the beginning of the file to the standard output, and tail is supposed to read the file. end. 

1. Command format:

head [parameter]... [file]...  

2. Command function:

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

3. Command parameters:

-q hide filenames

-v show filename

-c <bytes> display the number of bytes

-n<number of lines> number of lines to display

4. Example of use:

Example 1: Display the first n lines of a file

Order:

head -n 5 log2014.log

output:

[root@localhost 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

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

[root@localhost test]# head -n 5 log2014.log 

2014-01

2014-02

2014-03

2014-04

2014-05[root@localhost test]#

 

Example 2: Display the first n bytes of a file

Order:

head -c 20 log2014.log

output:

[root@localhost test]# head -c 20 log2014.log

2014-01

2014-02

2014

[root@localhost test]#

 

Example 3: Contents of file except last n bytes 

Order:

head -c -32 log2014.log

output:

[root@localhost 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[root@localhost test]#

 

Example 4: Output the entire contents of the file except the last n lines

Order:

head -n -6 log2014.log

output:

[root@localhost test]# head -n -6 log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07[root@localhost test]#

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325080557&siteId=291194637