Head from to linux

head Command in Linux with Examples

The head command is a command-line utility in most Linux distributions that is used to print data from the beginning of one or more files. It is also used to output piped data to CLI. We can use different options to print any number of lines or bytes of data to the standard output but by default the head command prints the first 10 lines of the file.

In this comprehensive guide, we will learn to use the head command in Linux. Moreover, we will also get familiar with the options for the head command that is available on Linux. The head command is available in all major Linux distributions; but for demonstration purposes, in this guide, we will only use Ubuntu 20.04 LTS.

Let’s begin with understanding the syntax of the head command first:

Syntax:

The syntax of the head command is like any other command that is used to work with files. It takes two different parameters; OPTION and FILE_NAME.

The options are used to manipulate the output of the head command. They can be used to specify the amount of data that needs to be printed to the standard output or to print data with or without headers. Here’s a list of options available for the head command in Linux:

Option Meaning
-c, —byte It is mandatory to follow this option by a number which specifies the bytes that are to be printed to the standard output.
-n, —lines It is mandatory to follow this option by a number as well. The -n option specifies the amount of lines that are to be printed to the standard output.
:-q, —quiet, —silent If this option is used then the head command will not print headers specifying the file names.
-v, —verbose This option will always output the header/filename.
-z, —zero-terminated Changes the line delimiter from newline to NULL.
—help To output the help menu.
—version To output the version information about the head command.
Читайте также:  Как посмотреть символьные ссылки linux

In this how-to guide, we will discuss the head command’s options in great detail; but first, we will learn to use the head command without any options.

How to show first 10 lines from a file

If we execute the head command without providing any option, it will show the first ten lines only of a text file. Let’s say we have a text file named cars.txt which contains the names of some cars and we need to print the names of the first ten cars which are written on the first ten lines. Then we will use the head command in the following way:

How to print a specific number of bytes from the beginning of a file

The options -c or —byte can be used to specify the number of bytes that are to be printed to the standard output. Now we will use the cars.txt file again and print the first fifteen bytes of the file:

How to print a specific number of lines from the beginning of a file

The -n , —lines options can be used to print a specific number of lines from the beginning of a file:

How to display the header/file name

The -v or —verbose option can be used to print the name of the file to the standard output:

How to display data from multiple files

The head command can take multiple file names as parameters. It will print out the first ten lines of both files (proceeded by the file name) by default:

We can also use options along with multiple file names:

head -n 2 cars.txt names.txt 

How to display data from multiple files without headers

The -q , —quiet and the —silent option can be used to display data from multiple files without headers:

head -n 2 -q cars.txt names.txt 

How to combine head command with other commands

The head command can be combined with other commands to filter the data that is being printed to the standard output:

Using [-]K with -c and -n options

If the -c and -n options are followed by [-]K, then the head command prints all the bytes/lines of the file except the last K bytes/lines:

The names.txt file contains twenty-six names in total. If we use the below-given command then the head command will print the first fifteen names (lines).

head -n 15 names.txt command 

But if we add a — before the number then the head command will print all the lines except the last fifteen:

Читайте также:  Kyocera fs 1125 mfp драйвер linux

Conclusion

The head is a command present in all major Linux distributions which are used to print out data from the start of a file. It is the opposite of the tail command which is used to output data from the end of a file.

The head command can be used with different options to specify the number of lines or bytes that should be printed to the standard output. Options are also available to allow or restrict the head command to not show the header or name of the file.

In this how-to guide, we learned to use the header command along with its options.

Источник

How to Use ‘head’ Command in Linux [8 Useful Examples]

In Linux, there are various commands available to display the contents of the text file. Some of the popular and most frequently used commands are cat, less, more, view, etc. However, all of these commands are more relevant when we want to display a large part of the file.

Sometimes, we just want to display the first few lines of the file. In such, cases we can use the head command, which comes in handy when we want to display the first part of the file.

In this guide, we will learn about the head command using some practical examples. After following this guide, Linux users will be able to work with text files efficiently from the command line interface.

head Command Syntax

The syntax of the head command is very simple and it is identical to other Linux commands:

It is important to note that, in the above syntax, both OPTIONS and FILE parameters are optional. So, if the input file is not provided or the file argument is a hyphen (-) then, it reads the input from the stdin stream.

To start, first, let’s create a simple text file with the following contents:

Create Sample Text File

Now, the input file is ready. So let’s use it to demonstrate the usage for the head common.

1. Show First 10 Lines Of File in Linux

By default, the head command displays the first ten lines of the input file as shown.

Print First 10 Lines of File

Here, we can see that the command shows only the first ten lines of the file-1.txt file.

2. Show First N Lines of File in Linux

In the previous example, we saw that the head command displays the first ten lines of the file by default. However, we can overwrite this default behavior using the -n option, which allows us to limit the number of lines to be displayed.

To understand this, let’s use the below command to display the first five lines of the file-1.txt file:

Читайте также:  Установить две операционные системы windows linux

Print First N Lines of File

3. Remove Last N Lines of a File in Linux

In a similar way, we can use the negative number with the -n option to skip the last N lines from the file. For example, let’s use the -10 value to skip the last 10 lines of the file:

Remove Last N Lines of File

In the above output, we can see that now the head command shows only the first two lines.

4. Show First N Characters of the File

We can also instruct the head command to display the first N bytes of the file using the -c option:

In the below output, we can see that the head command shows the first eight characters from the file.

Print First N Characters of File

In this case, the file contains ASCII characters that take 1 byte per character. Hence the command shows the first eight characters including the newline (\n) character.

5. Remove Last N Characters of File

Similarly, we can use the negative number with the -c option to remove the last N bytes. So let’s skip the last line of the file-1.txt file using the below command:

In the below output, we can see that the head command shows all the characters except the last nine characters.

Remove Last N Characters of File

6. Show File Name in Header of File

The head command allows us to display the current file name as a display header using the -v option:

In the below output, ==> file-1.txt

Print File Name in Header of File

This option comes in handy while working with multiple files. Hence the head command enables this option by default when we use multiple files with it.

7. Show File Name in Header in Multiple Files

We can use multiple files with the head command. In such cases, the display header is used to separate the file contents. Let’s understand this with a simple example.

First, let’s create a copy of the file-1.txt using the cp command:

Now, let’s display the first three lines from each file:

$ head -n 3 file-1.txt file-2.txt

Show File Name in Header of Files

8. How to Disable the Display Header

In the previous example, we saw that by default, the head command enables the display header if we use multiple files with it. However, we can use the -q option to override this default behavior.

Let’s use the below command to display the first three lines from both files:

$ head -n 3 -q file-1.txt file-2.txt

Here, we can see that now the head command displays the file contents one after another without any display header.

Disable Header in Files

In this article, we learned about the head command using practical examples. Linux newbies can refer to this guide while working with Linux systems.

Do you know of any other best example of the head command in Linux? Let us know your views in the comments below.

Источник

Оцените статью
Adblock
detector