3 Basic Ways To Look At A Text File In Linux
cat: The cat command simply returns the text in a file
The basic syntax is [cat filename]
But what happens if there is more text than fits the window
Notice that a few lines are missing from the top of the screen.
To prevent that you can pipe the output of the cat command to the “more” or “less” command which
will paginate the display.
will paginate the display.
$cat chicago_beef_sandwich.txt |less
Notice the colon at the bottom of the page; this indicates there is more text.
You access it by using the spacebar to move a full page or arrow keys to move a line or more at a time.
You access it by using the spacebar to move a full page or arrow keys to move a line or more at a time.
But in reality there is no reason to pipe the cat results when you can just use the less or more command
directly to show the file.
directly to show the file.
more and less commands:
These two commands are pretty much the same. They let you look at text in a file and paginate
as necessary. You use the spacebar or arrow keys to view lines form the next screen of text.
as necessary. You use the spacebar or arrow keys to view lines form the next screen of text.
The basic syntax is [more filename] or [less filename]
Output of less command:
Of the two commands less is more flexible because more will only move forward a page at a time
while less will move forward or backwards through the file.
while less will move forward or backwards through the file.
One area where less can be inferior to more is the fact that when you exit the less command the text
you are looking at is cleared from the screen. This is good for decluttering the terminal but what if you
want to still see the contents of the file even after exiting.
you are looking at is cleared from the screen. This is good for decluttering the terminal but what if you
want to still see the contents of the file even after exiting.
By passing the -X option less will not clear the screen when exiting the command.
For example you run the less command against a file:
And when you are done you quit the command and the screen clears:
But maybe you want to still see what is in the file so you can use it in another command; if you have
a short memory you might be asking yourself “is the oven supposed to be at 300 degrees Celcius or
Farenheit”?
a short memory you might be asking yourself “is the oven supposed to be at 300 degrees Celcius or
Farenheit”?
If you use the less command with -X your screen won’t be cleared when you exit and you can see that
the oven is supposed to be set to 300 degrees F.
the oven is supposed to be set to 300 degrees F.
Several other ways To look at text in a file:
You can also look at the text in a file with VI or nano, or other CLI text editors. But these are primarily
for editing text. In my opinion it is actually not a good habit to use these for just viewing text
because you could inadvertently modify the file. You would really have to screw up to do that but it is
not unheard of if your one of those multitasking constantly interrupted sysadmins.
for editing text. In my opinion it is actually not a good habit to use these for just viewing text
because you could inadvertently modify the file. You would really have to screw up to do that but it is
not unheard of if your one of those multitasking constantly interrupted sysadmins.
One safe alternative to using VI to look at a file is to use the “view” command which is a VI interface but
started in read only mode.
started in read only mode.
However overall the big advantage of the cat, less, and more commands is that their output is easily
redirected to another command or file; especially the cat command.
redirected to another command or file; especially the cat command.