How to Open a Text File in Terminal in Ubuntu
Answer: Use the
You can use the
less command if you simply want to open a file in terminal to view its content (not to edit). It will show the file contents from the top. You can scroll down and up to see the contents of a larger file; press the
q key on the keyboard to exit and get back to terminal.
To search inside the file press
/, and type the text you're searching for, and press Enter.
For example, if your current working directory is
/var/www and you want to view the contents of a text file named
info.txt inside of it, you can use the command:
Alternatively, you can provide the absolute path of the file like this:
Both commands will have the same effect, because current working directory doesn't matter when you use absolute path. Any path that starts with a forward slash
/ is an absolute path.
If you also want to show line numbers you can use the
-N option, like this:
Moreover, for smaller files, you can use the
cat command. It displays the entire file contents on the terminal screen without a pause. Therefore, if the file has a large amount of content, the top of the file will not be displayed as there is not enough screen area to do so.
Similarly, to display line numbers with the
cat command you can use
-n option like this:
Here are some more FAQ related to this topic: