Linux Fundamentals 2: Viewing Log Files

In this post we will run through some Linux commands which can be used to view files and logs.

To start we look in the folder var/log, this is where the Linux logs are kept. From the root enter.

ls /var/log

Less Command

The less command opens a file at the top and prints the whole log file, use spacebar or mouse to scroll up and down. This is not great if the log file is long as the file needs loaded into memory, also you have to read the whole log to get to the last error.

less alternatives.log

More Command

This opens the log file at the top, it lets you scroll down one step at a time, again this is not great if the log file is long as you have to read the whole log to get to the last error.

more alternatives.log

Cat Command

The cat command can be used to combine files, create new files and it can also print the whole file.

cat alternatives.log

Tail Command

The tail command will show the last 10 lines of the log file - this is useful when the file is large and you want to view the latest logs. using the -n parameter will allow you to specify the number of lines. The 'head' command is the same as tail but starts at the top of the file.

When working with AEM tail is probably the most useful command for viewing log files.

tail alternatives.log -n 10000

Grep
grep can be used to search files for patterns. For example

Search for the word link in a file 'link'

grep 'link' alternatives.log

Piping Grep

less alternatives.log  | grep 'link'
more alternatives.log  | grep 'link'

Using grep with tail to search for errors.

tail alternatives.log -n 10000 | grep link

Next up we discuss, how to edit text files.