From our earlier articles of this section you have learnt how to create files and directories. You have also played around the Linux file system. Now its time to read contents of files with some advance options.
Everything in Linux is file. Sometime you may find its very hard to read the entire file. To navigate in the file Linux provide some cool commands.
Linux has two of these commands: more and less. With the more filename command,
you can scroll through the text of a file, from start to finish, one screen at a time.
With the less filename command,
you can scroll in both directions. Use PAGE UP and PAGE DOWN keys to scroll.
Press q anytime to quit from the output of the commands.
During the exam sometime you need to find some text in files. Like in configuration files you need to find some configuration values. Or you may need to locate some error in error log files.
As the less and more commands do not change files, they are an excellent wayto scroll through and search for items.
Let take a example
open /var/log/messages file with less command
Now I would search the term"pwd_max", in forward
To search in the reverse direction, substitute a ? for the /.
Found string would be shown like this
With more command you could only search in forward direction.
One more cool feature of less command is it could read the in Gzip format compress files without uncompressing them. This features only available in less commands
Gzip format, normally shown with the .gz extension.
Viewing parts of a file
Often the data you want to view is located either right at the top or buried at the bottom of a text file. If the information is at the top of a large file, you still need to wait for the cat or more commands to load the entire file before you can view it. If the information is located at the bottom of a file (such as a log file), you need to wade through thousands of lines of text just to get to the last few entries. Fortunately, Linux has specialized commands to solve both of these problems.
The tail command
displays the last group of lines in a file. By default, it will show the last 10 lines in the file.
but you can change it with -n switch. For example to see the 2 line use
The head command
While not as exotic as the tail command, the head command does what you would expect, it displays
the first group of lines at the start of a file. By default, it will display the first 10 lines of text.
Similar to the tail command, you can use -n switch
Search more about
Search in Google for