How can I remove all but the last 10 lines from a file?

sbrink picture sbrink · Sep 23, 2010 · Viewed 11k times · Source

Is it possible to keep only the last 10 lines of a lines with a simple shell command?

tail -n 10 test.log

delivers the right result, but I don't know how to modify test.log itself. And

tail -n 10 test.log > test.log

doesn't work.

Answer

Ankit Bansal picture Ankit Bansal · Sep 23, 2010

You can do it using tempfile.

tail -n 10 test.log > test1.log

mv test1.log test.log