The head command displays the starting row (default is 10) of one or more documents. Also it can display the data that transmits on the output of another utility.
Today we will tell you how to use this command for Linux and show you how to use in practice the most popular options head.
Head command in Linux
The syntax of head command is the following:
$ head file
- Options is the option that allows you to configure the command so that the result matched to the specific needs of the user.
- File is the name of the document (or document names, if any). If this value is not set, or is it the sign “-“, the command will take the data from standard output.
Most of the team head apply the following options:
- -c (–bytes) — allows you to specify the number of the text in lines and in bytes. When recording in the form of –bytes=[-]NUM displays all the contents of the file, except the NUM of bytes located at the end of the document.
- -n (–lines) — shows the specified number of rows instead of the 10 that are displayed by default. If you write this option in the form –lines=[-]NUM, you will see the whole text except the last NUM lines.
- -q (–quiet –silent) — displays only the text, not adding to it the file name.
- -v (–verbose) — before the text displays the file name.
- -z (–zero-terminated) characters of new-line characters replace line endings.
The variable NUM is mentioned above is any number from 0 to infinity, defined by the user. It can be normal or contain a multiplier.
Examples of the use of head
The easiest way to use the head command with the file name, but without the options. In this case, it will be displayed the first 10 rows.
If you need a lump sum to output multiple files, this will not have any problems. It is sufficient to list the names, separated by a space:
head file-name1.txt file-name2.txt
Of course, files can be three, four, and more. To avoid any confusion, the contents are automatically separated by a blank line, and before the text is displayed in the title of the document.
That the file name is output even in the case when the team asked only one document, use the-v option:
If ten lines is the default output from the command will be too few or too many, nothing prevents to change the number manually. For this purpose, the-n option:
Let’s not forget about another interesting aspect of this option. It allows you to display the number of rows that will remain after “clipping” the extra text. For this you need to use a non-shortened (single-letter), and full entry options:
While working in the terminal brackets are not used, the minus sign comes immediately after the equal sign. Is NUM, specify number. Here is the command and its result on the screenshot.
It should be noted that line “cut off” from the last.
Up to this point we (and the team head along with us) counted the number of text line by line. But it is not required — with the same success the unit of measure can be bytes. It is enough to tell the team about the new rule with the option -s, at the same time and putting the number of bytes that you want to display (NUM):
head-c NUM file-name.txt
As in the case with the option –lines, you can “trim” the unnecessary amount of text using the full form option with –bytes. The entry team is on the same principle and also extra bytes are counted starting from the end of the document:
When writing bytes, you can use alphabetic suffixes:
- b — multiplies the number by 512.
- kB — 1000.
- k — on 1024.
- MB — 1 000 000.
- M — 1 048 576.
By the way, the command head can be used not only independently but also in combination with other commands. For example, such a record hashes randomly selected environment variable, displays the first 32 bytes from the specified file and displays a random string of 24 characters:
echo $RANDOM | sha512sum | head -c 24 file-name.txt; echo
Our examples head linux has come to an end.
The linux head command, which displays the initial lines of the file, is a kind of antagonist commands tail (it prints in the terminal the last line). Because its syntax is quite simple, users usually do not have problems using this command. If you have questions or comments on the part of the head command and application in Linux, be sure to leave a comment below.