
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to display the first part of the file in the Linux system?
To display the first part of the file, we use the head command in the Linux system.
The head command is used to display the beginning of a text file or piped data. By default, it displays the first ten lines of the specified files. The tail command is also used to display the ending part of the file.
Syntax
The general syntax of the head command is as follow −
head [OPTION]... [FILE]...
Brief description of options available in the head command.
Sr.No. | Option & Description |
---|---|
1 | -c, --byte = [-]NUM Display the first NUM bytes of each file. With the leading ‘-‘, print all but the last NUM bytes of each file. |
2 | -n, --lines [-]NUM Display the first NUM lines instead of the first ten with the leading ‘- ‘, display all but the last NUM lines of each file. |
3 | -q, --quiet, --silent Never prompt headers giving file names. |
4 | -v, --verbose Always display headers giving file names. |
5 | -z, --zero-terminated Line delimiter is NULL, not newline. |
6 | --help Displays a help message and then exits. |
7 | --version It gives info about the version and then exits. |
By default, the head command prints the first ten lines without any option as shown in this example.
First, we will create a file containing more than ten lines using the cat command in the Linux system as shown below.
$ cat >text.txt First line... Second line... Third line... Fourth line... Fifth line... Sixth line... Seventh line... Eighth line...Ninth line... Tenth line... Eleventh line...
Then, we will use the head command in the Linux system to display the first ten lines.
$ head text.txt First line... Second line... Third line... Fourth line... Fifth line... Sixth line... Seventh line... Eighth line... Ninth line... Tenth line...
To print the first n lines, we use the -n or --lines option with the head command as shown below.
Suppose we want to display four lines of the text.txt file then we have to execute the command as shown below.
$ head -n 4 text.txt
To print lines between m and n, we use the head and tail command in the Linux system as shown below.
Suppose we want to display lines between 7 to 9 of the text.txt file then we have to execute the command as shown below.
$ head -n 7 text.txt | tail -9
Note – The tail command is used to prints lines from last in the Linux system.
To check more information about the head command, we use the --help option with the head command in the Linux operating system as shown below.
$ head --help
To check version information of the head command, we use the --version option with the head command in the Linux operating system as shown below. $ head --version
- Related Questions & Answers
- How to display the last part of the file in the Linux system?\n
- How to flushes file system buffers in the Linux operating system?
- How to display the current working directory in the Linux system?
- How to format contents of a text file in the Linux system?
- How to Create a File in the Linux/Unix system using terminal?
- How to Determine the File System Type in Linux (Ext2, Ext3 or Ext4)?
- How to merge lines of files in the Linux system?
- How to move jobs to the background in the Linux system?
- Getting the first part from the Postcode in MySQL
- How to converts tabs to spaces in the Linux system?
- How to create key binds in the Linux system using the terminal?
- How to Create a New Ext4 File System in Linux?
- How to extract a part of the file path (a directory) in Python?
- Explain the concept of the traditional file system in DBMS?
- C++ Program to Split the array and add the first part to the end?