16 October 2026 · One command a day
How do I read the last 50 lines of a log file on Linux?
The tail command prints the end of a file. Log files add new entries at the bottom, so the newest entries are always in the last few lines. You use tail when something has just gone wrong and you want to see the latest errors without scrolling through the whole file.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
The command
sudo tail -n 50 /var/log/nginx/error.logtail reads from the end of the file. -n 50 tells it how many lines to show. Without it you get the last 10. You can use any number you like, such as -n 200. The last part is the path to the file. Here it is the nginx error log. On Ubuntu, nginx logs belong to the adm group and other users cannot read them, so you usually need sudo. You can leave sudo out for files your own user can read, such as logs inside your app's folder.
What you will see
2026/10/16 09:14:02 [error] 1234#1234: *5678 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: example.com, request: "GET /api/health HTTP/1.1", upstream: "http://127.0.0.1:3000/api/health", host: "example.com"
2026/10/16 09:14:07 [warn] 1234#1234: *5681 an upstream response is buffered to a temporary file /var/lib/nginx/proxy/1/00/0000000001 while reading upstream, client: 127.0.0.1, server: example.com, request: "GET /export.csv HTTP/1.1", host: "example.com"
2026/10/16 09:15:30 [error] 1234#1234: *5702 open() "/var/www/sitename/favicon.ico" failed (2: No such file or directory), client: 127.0.0.1, server: example.com, request: "GET /favicon.ico HTTP/1.1", host: "example.com"This shows the last three of the 50 lines. Each line is one event. The oldest is at the top and the newest is at the bottom. Check the timestamp first to see whether the error is recent. Then check the level, such as [error] or [warn]. The rest of the line says what failed. In the first line, Connection refused to 127.0.0.1:3000 means the app nginx forwards requests to was not running or not listening on that port.
When to use it
- A site returns a 502 error and you want to see why nginx could not reach the app behind it.
- An app has crashed and you want to read the last things it wrote to its own log file before it stopped.
- You have just changed a config and want to check that no new errors have appeared since.
Watch out for
Log rotation. Ubuntu rotates most logs, often once a day. The current file is renamed to error.log.1, older ones are compressed to error.log.2.gz and so on, and a new, empty error.log is started. If you get almost nothing back, or nothing from the time of the problem, run ls -lh /var/log/nginx/ and check the older files. tail works on error.log.1 as it is. For a .gz file, run zcat on it first and pipe the output into tail.
Questions people ask
How do I keep watching new lines as they arrive?
Add -f, as in sudo tail -n 50 -f /var/log/nginx/error.log, and press Ctrl+C to stop. There is more on this in how to watch the nginx error log in real time.
Is tail safe to use on a very large log file?
Yes. tail starts reading at the end of the file, so it stays fast even on files several gigabytes in size. To look for a particular message across the whole file, use grep to search inside files.
You can only read raw server logs like these on a server you control, not on shared hosting. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.
Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.
Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.
Affiliate link.