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":
Step 2
We open the content of the file:
cat file.txt
Step 3
To show the first 10 lines (default value) we execute the following:
head file.txt
Step 4
To see only a defined number of lines we execute:
head -n # file.txt
step 5
To delete a number of lines we execute the command:
head -n -# file.txt
step 6
To see a specific number of characters, at the beginning of the file, run:
head -c # file.txt
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.
step 9
To see the name of the file in the header we execute:
head -n # -v file.txt
step 10
We can display the headers in multiple files, we copy the original file first:
cp file.txt file1.txt
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.
step 13
To disable the header and only see the content, we execute:
head -n # -q file.txt file1.txt
We can see how the head command is very helpful to display data in a text without having to open it directly..