Training on managing files with the head command in Linux
With the head command in Linux you can manage your files without the need for editors, and drag specific lines to the output.
Linux commands for a server administrator are a universal wrench. In many cases, more sophisticated methods are needed to manage large files with large lines.
If you are a server administrator you are probably familiar with log files. Large files with long lines that give us vital information on system performance and services.
With these files you can have precise security monitoring on Linux, but checking these files is not easy as they usually contain several thousand lines of text, which you can do very manually using standard methods. It will be hard and complicated.
Of course, in Linux with different commands you can have very precise controls on the textual content, one of which is the head command in Linux.
Displays specified lines with the head command in Linux
By default, the head command only displays the first 3 lines of the file in the output, and how to use it in general is as follows.
head [options] [file]
For example, we can see by the head / etc / passwd command the contents of the first 3 lines of the passwd file, which are also important system files.
Of course, the head command in Linux also has the ability to receive multiple files at once and display the first 6 lines of them in succession.
In this case, just enter the filenames in front of the head command.
head / etc / passwd / etc / shadow
Show more than 1 lines with head command
If we want to show more lines, we can use the n-switch and enter the number of the lines we want in the following way.
head –n20 / etc / passwd
Of course, n-use is not necessary. You can get the output you want by just entering the number of lines below.
head -20 / etc / passwd
It was just as easy to work with head command in Linux. The instruction of the head command ends here, but I would like to start with another command, which is very common, except that it displays lines at the end of the file opposite the head command.
Manage file with tail command
The method of using the tail command in Linux is very similar to the head command, except that it displays the last lines of the file.
The general format for using this command is as follows.
tail [options] [filenames]
For example, we want to check the last line of the Apache web server log file, so just enter the following command into the Linux terminal.
If we want to separate the number of specified lines from the end of the file we can do this by entering the number of lines below.
tail -5 access.log
And a very common way to use this monitoring mode is with Realtime method, where you want to see the lines that are stored at any moment in your log file.
In this case, the f-switch will do this for you.
tail –f access.log