+3 votes
56 views
HEAD command Linux | Examples, Syntax, What is it for

in Linux/Unix by (551k points)
reopened | 56 views

1 Answer

+4 votes
Best answer

How to use Head on Linux

In Linux we find hundreds of commands that help us to have a much more precise and centralized control in multiple tasks in the system, users and applications, one of these commands is Head. Next we are going to see how we can use the Head command in Linux as well as know the syntax and the examples that we can carry out when we are working with a command like head in Linux to know details of a certain file since the commands allow us to know information even about who is using a file in linux..

 

What is Head command
The head command allows us to access the details of a file based on the criteria that we indicate, its use syntax is the following:
 head [OPTION]... [FILE]. 
By default head will display the first 10 lines of each file.

 

 

 

Head Parameters
The global usage parameters are:
  • -c, --bytes=Number: print the first number bytes of each file, we can use -Number to see all but the last specified number of bytes in the file.
  • -n, --lines=Number: allows to see a defined number of lines instead of the default first 10, it is possible to use -Number to see all but the last defined number of lines of the file
  • -q, --quiet, --silent: don't show file name headers
  • -v, --verbose: always generate headers with the name of the file used
  • -z, --zero-terminated: do not create a new line

 

 

TechnoWikis will teach you how to use this head command in Linux and we will use Ubuntu.

 

 

To stay up to date, remember to subscribe to our YouTube channel!
SUBSCRIBE ON YOUTUBE

 

 

How to use Head on Linux

 

 

Step 1

We open the terminal, we access the folder or directory where we have the file and there we list the content with "ls":

 

image

 

Step 2

We open the content of the file:
 cat file.txt 
image

 

Step 3

To show the first 10 lines (default value) we execute the following:
 head file.txt 
image

 

Step 4

To see only a defined number of lines we execute:
 head -n # file.txt 
image

 

step 5

To delete a number of lines we execute the command:
 head -n -# file.txt 
image

 

step 6

To see a specific number of characters, at the beginning of the file, run:
 head -c # file.txt 
image

 

step 7

To remove a number of characters at the end we execute:
 head -c -# file.txt 
step 8

This will delete the spaces based on the number indicated.

 

 

image

 

step 9

To see the name of the file in the header we execute:
 head -n # -v file.txt 
image

 

step 10

We can display the headers in multiple files, we copy the original file first:
 cp file.txt file1.txt 
image

 

step 11

To see the headers we execute the following:
 head -n # file.txt file1.txt 
step 12

With the value # we indicate how many lines have to be displayed.

 

 

image

 

step 13

To disable the header and only see the content, we execute:
 head -n # -q file.txt file1.txt 
image

 

We can see how the head command is very helpful to display data in a text without having to open it directly..

 


by (3.5m points)
edited

Related questions

+5 votes
1 answer
asked Nov 2, 2022 in Linux/Unix by backtothefuture (551k points) | 61 views
+5 votes
1 answer
asked Oct 13, 2023 in Linux/Unix by backtothefuture (551k points) | 30 views
+3 votes
1 answer
+5 votes
1 answer
asked Jul 27, 2023 in Linux/Unix by backtothefuture (551k points) | 39 views
+3 votes
1 answer
asked Feb 6, 2023 in Linux/Unix by backtothefuture (551k points) | 58 views
Sponsored articles cost $40 per post. You can contact us via Feedback
10,632 questions
10,764 answers
510 comments
3 users