Remove the Last N Lines of a File in Linux

There may be times when you need to remove the last few lines of a file on Linux. For example, you may have a log file that is constantly being added and you want to keep only the most recent entries. In this tutorial, we'll explore different methods to remove the last N lines of a file on Linux.

Use the head Command

The head command can display the beginning of a file. By using the -n option with a negative number, we can exclude the last N lines from the output.

To remove the last N lines of a file, use the following command

head -n -N file.txt > temp && mv temp file.txt

Let's analyze this command

  • The head -n -N command displays all lines except the last N lines

  • The > operator redirects the output to a temporary file called temp

  • The && operator ensures the mv command only runs if head succeeds

  • The mv command replaces the original file with the modified content

Example

To remove the last 7 lines from a file

head -n -7 file.txt > temp && mv temp file.txt

This command will remove the last 7 lines from file.txt and save the result back to the same file.

Use the sed Command

The sed command is a powerful stream editor that can perform various text processing tasks, including removing lines from files.

To remove the last line of a file using sed

sed -i '$d' file.txt

To remove the last N lines, you can combine sed with other commands

sed -i "$(( $(wc -l < file.txt) - N + 1 )),\$d" file.txt

Let's break down the sed approach

  • The -i option modifies the file in-place

  • The $d command deletes the last line

  • For multiple lines, we calculate the starting line number and delete from there to the end

Example

To remove the last 3 lines from a file

sed -i "$(( $(wc -l < file.txt) - 3 + 1 )),\$d" file.txt

Use the tac and tail Commands

Another approach combines the tac command (which reverses line order) with tail and tac again to remove the last N lines.

tac file.txt | tail -n +$((N+1)) | tac > temp && mv temp file.txt

Let's analyze this approach

  • The first tac reverses the file so the last lines become first

  • The tail -n +$((N+1)) skips the first N lines (originally the last N lines)

  • The second tac reverses the file back to original order

  • The result is redirected to a temporary file and then moved back

Example

To remove the last 3 lines from a file

tac file.txt | tail -n +4 | tac > temp && mv temp file.txt

Comparison of Methods

Method Advantages Disadvantages Best For
head Simple, efficient Requires temporary file General use, small to medium files
sed In-place editing, no temp file Complex syntax for multiple lines Single line removal
tac + tail Conceptually clear Multiple commands, temp file needed Learning purposes

Conclusion

We explored three different methods for removing the last N lines from a file on Linux. The head command approach is the most straightforward and efficient for most use cases. Choose the method that best fits your specific requirements and comfort level with Linux command-line tools.

Updated on: 2026-03-17T09:01:38+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements