I have an application that is logging to a plain text log file (myapp.log) but it doesn't seem to be writing new line characters at the end of each log entry. If I execute a command like tail -n 50 myapp.log I actually receive hundreds of "lines" of text (log entries).

This log file is very large, roughly 1GB, who knows how long ago the last CR and/or LF was inserterd. How can I just grab say, the last 2MBs for example?


Using the -c switch (2MB = 2 * 1024 * 1024 = 2097152 bytes):

tail -c 2097152 myapp.log

Thanks to Petr Uzel for the suggestion. Some tail implementations allow to add a unit to print the last kilobytes (k) or megabytes (m), like:

tail -c 2m myapp.log

However please note that it is not standard (in none of POSIX, UNIX (SUS) or Linux (LSB)) and not portable. Also note that since the terms "Mega"/"kilo"... and their abbreviations (M, k...) have ambiguous meanings (1000 vs 1024), there's not much guarantee of what this or that implementation of tail will mean by 2m (though the current versions of the current implementations that do support it seem to be going for the 1024 variant).

  • 5
    Tail should be able to accept number with units as argument for the c switch, so tail -c 2M myapp.log should also work. – Petr Uzel Oct 1 '12 at 15:23
  • @PetrUzel: Thanks. Edited the answer to add it. – Birei Oct 1 '12 at 15:33
  • Now I feel silly :) Thanks for the promptness, really appreciate that. Great suggestion! – jwbensley Oct 1 '12 at 15:44
  • 1
    Perfect, Not sure if this will help but i'll add this text-string because it's what i was the keywords i was searching for to get here (took a good while): "cat file data between two byte positions (not lines)" – Torxed Sep 18 '14 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.