brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: tail -c works in bytes, which helps with huge or one line files

Tail
~/tips/tip-tail-bytes-instead-of-lines

tail -c 2000 export.json

Look at the end of a file that has very long lines or none.

A minified JSON export can be a single line of 200 MB. Asking for the last ten lines then prints the whole thing into your terminal.

Count bytes instead:

tail -c 2000 export.json

That prints the last 2000 bytes, whatever the line structure. Suffixes work too, such as K and M:

tail -c 1M big.log > sample.log

The sample file is a handy piece to test a parser on. With a plus sign the meaning flips, and output starts at that byte number:

tail -c +101 firmware.bin > payload.bin

This writes everything from byte 101 onwards, so it skips a header of 100 bytes. I find that useful when a tool adds a fixed size prefix to a file.

A quick check for a missing newline at the end of a file uses the same option:

tail -c 1 config.ini | od -c

If the output shows \n, the file ends with a newline.

Bytes are not characters. The cut can land in the middle of a multibyte UTF-8 character, which then shows as a garbled symbol at the start. It can also split a line or a record, so do not feed the output to something that expects complete lines without dropping the first one. And mind the off by one with the plus form: +101 starts at byte 101, which skips 100.

Further reading: tail invocation

More Tail tips tail -f --pid=1234 build.log tail -f /var/log/nginx/access.log /var/log/nginx/error.log tail -n +2 export.csv tail -F /var/log/nginx/access.log All Tail tips (5) All topics

Link