Ubuntu Basic Terminal commands Reading a text file


Example

Using Ubuntu you have different ways to read a text file, all similar but useful in different context.

cat

This is the simplest way to read a text file; it simply output the file content inside the terminal. Be careful: if the file is huge, it could take some time to complete the printing process! If you need to stop it, you can always press CTRL+C. Note that if you need to navigate through the document, you need to scroll the terminal output.

cat file_name.txt

more

An improved version of cat. If your file is longer than the display of the terminal, you can simply type

more file_name.txt

and you'll have a downwards scrolling display of text, in which you can move down by pressing ENTER.

less

This is the more command with some enhancements, and is typically a better choice than cat for reading medium to big documents. It opens file showing them from the beginning, allowing to scroll up/down/right/left using arrows.

less file_name.txt

Once the document is open, you can type some commands to enable some useful features, such as:

  • q: close immediately the opened file.
  • /word: search 'word' inside the document. Pressing n you can go to the following occurrence of 'word'.
  • ENTER: scrolls down of a single line.
  • r: repaints the file content, if it's changing while reading.

This is the best choice for reading medium to big documents.

tail

This software shows only the last part of the file. It's useful if you need to read just a few lines in the end of a very big document.

tail file_name.txt

The above command will show last 10 lines(default) of the file. To read last 3 lines, we need to write:

tail -3 file_name.txt

There's another use case where this command is extremely useful. Imagine to have a empty document, that is filled while you are watching it; if you want to see new lines in real time while they are written to the file without reopening it, just open the file with the -f option. It's really useful if you are watching some logs, for example.

tail -f file_name.txt

This is the best choice for reading growing documents.

head

This command does the opposite task of tail. For example the following command will show the first 15 lines of the file file_name.txt.

head -15 file_name.txt

tailf

This is an alternative for tail -f filename .It follows the file changes as they occur and shows you the output.

vim

Some of us like vi, some others like vim. This is not just for reading files, you can also edit them! Now let's see only some features that regards reading documents. Please note that vim offers syntax highlighting.

vim file_name.txt

Once the file is opened, be careful! Don't start typing, or you will mess everything up! In fact, even if you can see the cursor, you have to press i to start typing and ESC after you finished typing. By the way, now I'm going to showing you some useful commands that concern reading (not writing):

  • : : you need to type colon before inserting each of the following commands!
  • q! : exit from the file without asking a confirm. It's the same as q if you didn't edit the text.
  • /word : search for 'word' inside the document.
  • 230 : goes to line '230'.

Tip: a shortcut to insert a colon and then type wq! for writing edits to file and quit without asking a confirm, you can hold down SHIFT and press twice z.

This is the best choice for reading code files.