05-C.6.1: Working with Files and Directories - head/tail Commands
- Page ID
- 32146
The head Command
The head command, as the name implies, prints lines at the top of the file; by default, it prints the first 10 lines of the specified files. There is an option that allows you to print a number of lines other than 10. If more than one file name is provided then data from each file is preceded by its file name.
Syntax:
head [OPTION]... [FILE]...
Command Options
Options | Option Meaning |
---|---|
-c, --bytes=,[-]NUM/ | Print the first NUM bytes of each file; if the leading '-' is specified then print all but the last NUM bytes of each file. |
-n, --lines=,[-]NUM/ | Print the first NUM lines instead of the first 10; if the leading '-' is specified then print all but the last NUM lines of each file. |
-q, --quiet, --silent | Never print headers giving file names. |
-v, --verbose | Always print headers giving file names. |
-z, --zero-terminated | Line delimiter is NUL, not newline. |
There are a variety of options...but head is very useful for any of the options. The example show the top 10 lines since there is NO option specified. The second command specifies only the first five lines of the file are to be output.
pbmac@ubuntu $ head temp Not like the brazen giant of Greek fame, With conquering limbs astride from land to land; Here at our sea-washed, sunset gates shall stand A mighty woman with a torch, whose flame Is the imprisoned lightning, and her name Mother of Exiles. From her beacon-hand Glows world-wide welcome; her mild eyes command The air-bridged harbor that twin cities frame. “Keep, ancient lands, your storied pomp!” cries she With silent lips. “Give me your tired, your poor, pbmac@ubuntu $ pbmac@ubuntu $ pbmac@ubuntu $ head -n 5 temp Not like the brazen giant of Greek fame, With conquering limbs astride from land to land; Here at our sea-washed, sunset gates shall stand A mighty woman with a torch, whose flame Is the imprisoned lightning, and her name pbmac@ubuntu $
The tail Command
The tail command is the complementary of head command.The tail command, as the name implies, prints the last N number of data of the given input. By default it prints the last 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name.
Syntax:
tail [OPTION]... [FILE]...
Command Options
Options | Meaning |
---|---|
-c, --bytes=,[-]NUM/ | Output the last NUM bytes; or use -c +NUM to output starting with byte NUM of each fileprint the first NUM bytes of each file; if the leading '-' is specified then print all but the last NUM bytes of each file. |
-n, --lines=,[-]NUM/ | Output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with line NUM. |
|
Output appended data as the file grows. |
-q, --quiet, --silent | Never print headers giving file names. |
-v, --verbose | Always print headers giving file names. |
-z, --zero-terminated | Line delimiter is NUL, not newline. |
The tail command operates just like the head command, except it operates from the bottom of the file, not the top.
Adapted from:
"Head command in Linux with examples" by Akash Gupta, Geeks for Geeks is licensed under CC BY-SA 4.0
"Tail command in Linux with examples" by Akash Gupta, Geeks for Geeks is licensed under CC BY-SA 4.0