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 , 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/etc/fstab
output:
[Begin] 2016/1/7 16:30:21[[email protected] ~]# head -n -5 /etc/fstab ## /etc/fstab# created by anaconda on thu dec 31 16:39:19 2015## accessible filesystems, by reference, are maintained under '/dev/disk ' # see man pages fstab (5), findfs (8), mount (8) and/or Blkid (8) for more info#/dev/mapper/vgLegion-root / ext4 defaults,acl 1 1uuid= 4bdd95a5-f81b-4cfd-a446-dc4f5070c2aa /boot ext4 defaults 1 2/dev/mapper/vglegion-data /data ext4 defaults,acl 1 2/dev/mapper/vgLegion-home /home ext4 defaults,acl 1 2/dev/mapper/vgLegion-usr /usr ext4 defaults,acl 1 2[[email protected] ~]# cat -n /etc/fstab 1 2# 3# /etc/fstab 4# created by anaconda on thu dec 31 16:39:19 2015 5# 6# Accessible filesystems, by reference, are maintained under '/dev/disk ' 7# see man pages fstab (5), findfs (8), mount (8) and/or blkid (8) for more info 8# 9/dev/mapper/vgLegion-root / ext4 Defaults,acl 1 1 10uuid= 4bdd95a5-f81b-4cfd-a446-dc4f5070c2aa /boot ext4 defaults 1 2 11/dev/ mapper/vglegion-data /data ext4 defaults,acl 1 2 12/dev/mapper/vglegion-home /home ext4 defaults,acl 1 2 13/dev/mapper/vgLegion-usr /usr ext4 defaults,acl 1 2 14/dev/mapper/vgLegion-swap swap swap defaults,acl 0 0 15tmpfs /dev/shm tmpfs defaults 0 0 16devpts /dev/pts devpts gid=5,mode=620 0 0 17sysfs /sys sysfs defaults 0 0 18proc /proc proc defaults 0 0[end] 2016/1/7 16:30:48
Description: The Fstab file contains a total of 18 lines, while the command head-n-5 displays only the first five elements.
Example 2: Displaying the first n bytes of a file
Command:
head-c 20/etc/fstab
Output:
[BEGIN] 2016/1/7 16:33:43[[email protected] ~]# [[email protected] ~]# head-c 20/etc/fstab # #/etc/fstab# Cr[[email Prot Ected] ~]# [END] 2016/1/7 16:34:13
Example 4: Output file except the last n rows of the entire content
Command:
head-n -3/etc/fstab
Output:
[begin] 2016/1/7 16:40:29[[email protected] ~]# cat -n /etc/fstab 1 2# 3# /etc/ fstab 4# created by anaconda on thu dec 3 08:33:34 2015 5# 6# accessible filesystems, by reference, are maintained under '/dev/disk ' 7# see man pages fstab (5), findfs (8), mount (8) and/or blkid (8) for more info 8# 9uuid =8a9c5d3a-e525-4fa2-bcfe-90ced11a0b08 / ext4 defaults 1 1 10uuid=0167fd91-560f-4baF-bb20-b54d429faf9d /boot ext4 defaults 1 2 11uuid= 7b81c9d0-9139-4fcb-8700-0173c7ba0f60 swap swap defaults 0 0 12tmpfs /dev/shm tmpfs defaults 0 0 13devpts /dev/pts devpts gid=5,mode=620 0 0 14sysfs /sys sysfs defaults 0 0 15proc /proc proc defaults 0 0[[email protected] ~]# head -n -3 /etc/fstab ## /etc/fstab# Created by Anaconda on thu dec 3 08:33:34 2015## accessible filesystems, by reference, are maintained under '/dev/disk ' # see man pages fstab (5), findfs (8), Mount (8) and/or blkid (8) for more info#uuid=8a9c5d3a-e525-4fa2-bcfe-90ced11a0b08 / ext4 defaults 1 1UUID=0167fd91-560f-4baf-bb20-b54d429faf9d /boot ext4 Defaults 1 2uuid=7b81c9d0-9139-4fcb-8700-0173c7ba0f60 swap swap defaults 0 0tmpfs /dev/shm tmpfs defaults 0 0[end] 2016/1/7 16:40:39
Note: Except the last three lines are not displayed, all other display
A Linux head command every day