A Linux command a day (14): head command

post thumb
Devops
by Admin/ on 26 Sep 2021

A Linux command a day (14): head command


head and tail are as easy to understand as its name, it is used to display the beginning or end of a certain amount of text blocks, head is used to display the beginning of the file to the standard output, and tail is of course to see the end of the file.

1. Command format.

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

2. Command function.

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

3. Command parameters.

-q hide the file name

-v show file name

-c<bytes> shows the number of bytes

-n<line> Number of lines to display

4. Examples of use.

Example 1: Displaying the first n lines of a file

Command:

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: Displaying the first n bytes of a file

Command:

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 a file other than the last n bytes

Command:

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

Command:

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]#  

Reference:

www.cnblogs.com/peida/archive/2012/11/06/2756278.html

comments powered by Disqus