Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

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).

share|improve this answer
1  
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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