========================
== Artur's Tech Notes ==
========================
Artur's tech notes

Reading and monitoring files on Linux

linux basics

Let’s start with something super basic. To print the contents of a file you can use cat:

cat file

This tool is simple but really powerful when combined (piped) with other commands. However, cat is not very practical if you just want to see the contents of a larger file. In that case you can use less, which also allows you to conveniently browse your file:

  • press space for page down or b for page up,
  • type /pattern to search for pattern in the file (n jumps to the next occurence, while N jumps to the previous one),
  • press q to quit.

If you need to watch a certain file (typically a log file), you can simply use tail -f:

tail -f /var/log/messages

Alternatively, you can use less +F, which can also be used to search through the file. Let’s say that you are watching the /var/log/messages logfile:

less +F /var/log/messages

When you want less to stop waiting for new lines, you can just hit ^C and scroll or search through the file. Then, when you are done, you can just press F.