Q:

How to Open a Text File in Terminal in Ubuntu

0

How to Open a Text File in Terminal in Ubuntu

All Answers

need an explanation for this answer? contact us directly to get an explanation for this 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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now