shell: delete the last line of a huge text log file

Raptor picture Raptor · Aug 29, 2012 · Viewed 20.6k times · Source

I asked a question regarding popping the last line of a text file in PHP, and now, is it possible to re-write the logic in shell script?

I tried this to obtain the last line:

tail -n 1 my_log_file.log

but I am not sure how can I remove the last line and save the file.

P.S. given that I use Ubuntu server.

Answer

choroba picture choroba · Aug 29, 2012

To get the file content without the last line, you can use

head -n-1 logfile.log

(I am not sure this is supported everywhere)

or

sed '$d' logfile.log