How to Open a Text File in Terminal in Ubuntu

Topic: Ubuntu / LinuxPrev|Next

Answer: Use the less Command

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.

less /path/to/filename

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:

less info.txt

Alternatively, you can provide the absolute path of the file like this:

less /var/www/info.txt

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:

less -N /var/www/info.txt

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.

cat /var/www/info.txt

Similarly, to display line numbers with the cat command you can use -n option like this:

cat -n /var/www/info.txt

Related FAQ

Here are some more FAQ related to this topic:

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties