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.
To do this as cleanly as possible, first move the existing log file to a temporary location. Then combine it with the previously saved archive file, and keep up to n lines. As for the process which is logging, it should just start a new file the next time it writes a log message. Save this answer.
Tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then terminates. Example 1: By default “tail” prints the last 10 lines of a file, then exits.
The sed command can remove the lines of any range. For this, we just have to enter 'minimum' and 'maximum' line numbers. In this example, we will remove the lines ranging from 4 to 7 numbers. After removing these ranges of lines, our file will look like this.
To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file.
You can do it using tempfile.
tail -n 10 test.log > test1.log
mv test1.log test.log
echo "$(tail -n 10 test.log)" > test.log
Quotes are important. They preserve newline characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With