Linux Tutorial
Linux File/Directory Management
Linux Packaging And Compression
Vim Text Editor
Linux Text Processing
Linux Software Installation
Linux User/User Group Management
Linux Permission Management
Linux Filesystem Management
Linux Advanced Filesystem Management
Linux System Management
Linux Backup and Recovery
Linux System Service Management
Linux System Log Management
Linux Boot Management
LAMP/LNMP Environment
SELinux Management
The head
command in Linux is used to display the first few lines of a text file or input stream. By default, it shows the first 10 lines, but you can specify a custom number of lines as needed. This command is helpful for getting a quick overview of the contents of a file without opening it entirely. In this tutorial, we'll cover basic usage examples for the head
command.
To display the first 10 lines of a file, simply provide the file as an argument:
head file.txt
This command will display the first 10 lines of file.txt
.
If you want to display a specific number of lines from the beginning of a file, use the -n
option followed by the desired number:
head -n 5 file.txt
This command will display the first 5 lines of file.txt
.
You can display the first few lines of multiple files by providing multiple file names as arguments. The output for each file will be preceded by a header indicating the file name:
head file1.txt file2.txt
If you want to display a specific number of bytes from the beginning of a file instead of lines, use the -c
option followed by the desired number:
head -c 25 file.txt
This command will display the first 25 bytes of file.txt
.
You can also use head
to display the first few lines of an input stream by piping the output of another command to head
:
ls -l | head -n 5
This command will display the first 5 lines of the output generated by the ls -l
command.
In summary, the head
command is a useful tool for displaying the first few lines or bytes of a file or input stream in Linux. By understanding the various options and arguments, you can easily control the number of lines or bytes displayed, work with multiple files, and read input from pipes.
How to use the head command in Linux:
head
is a command-line utility in Linux used to display the first few lines of a file. It is particularly useful for quickly previewing the contents of a file.
Example code:
head filename
Displaying the first lines of a file with head:
To display the first 10 lines of a file (default behavior), you can simply use the head
command followed by the filename.
Example code:
head file.txt
Specifying the number of lines with head in Linux:
You can specify the number of lines to display using the -n
option.
Example code (displaying the first 5 lines):
head -n 5 file.txt
Viewing the beginning of multiple files with head:
To view the beginning of multiple files, you can pass multiple filenames to the head
command.
Example code:
head file1.txt file2.txt
Displaying headers and file names with head command:
Use the -c
option to display the first N bytes, and the -q
option to suppress headers when displaying multiple files.
Example code:
head -q -c 100 file1.txt file2.txt
Using head to preview log files in Linux:
Previewing log files is common with the head
command, especially when you want to quickly check the recent entries.
Example code:
head -n 20 /var/log/syslog
Skipping header lines with head in Linux:
To skip a specific number of header lines, you can use the -n
option with a value greater than the number of lines you want to skip.
Example code (skipping the first 3 lines):
head -n +4 file.txt
Customizing output format with head command:
While head
doesn't provide extensive formatting options, you can use other commands like awk
or sed
in conjunction to customize the output further.
Example code (using awk to display the first and second columns):
head file.txt | awk '{print $1, $2}'