How to View Log Files in Terminal
Viewing the contents of files in terminal can be fiddly but it doesn’t need to be.
“Check the logs” your colleague suggests. You read the docs, fumble around in your terminal and find the log files… now what?
You could try using your text editor of choice…
You’ve attempted to fix the error, you run the app again and get another error. Now you’ve got to refresh the file in your editor, this is clunky.
A Better Way
What if you could view the log file and view any changes as they come in?
tail has got you covered.
# view the last 100 lines of a file and follow changes
tail -f -n100 /the/log/file/location
Instead of opening the file statically you’ll now be viewing the last 100 lines of the file and also any updates as they come in.
You can show more or less lines from the end of the file by adjusting the
-n100 and swapping out the
100 with a number of your choice.
tail you need only hit
C on Linux or Windows, or
C on Mac.
Viewing Multiple Files
If you want to monitor multiple files you can specify more than one file for example:
# view the last 10 lines of each file and follow changes
tail -f -n10 /the/log/file/location /another/log/file/location
I’ve reduced the value for
-n here otherwise it becomes quite difficult to read the output.
Viewing the Start of Files
As you might guess there’s a sister command to
head you can view the start of a file. There’s no option to follow the start of a file because that would be very silly.
# view the first 100 lines of a file
head -n100 /the/log/file/location