Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 -Ncommand displays all lines except the last N linesThe
>operator redirects the output to a temporary file calledtempThe
&&operator ensures themvcommand only runs ifheadsucceedsThe
mvcommand 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
-ioption modifies the file in-placeThe
$dcommand deletes the last lineFor 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
tacreverses the file so the last lines become firstThe
tail -n +$((N+1))skips the first N lines (originally the last N lines)The second
tacreverses the file back to original orderThe 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.
