# Remove the Last N Lines of a File in Linux

## Introduction

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 a few different methods to remove the last N lines of a file on Linux.

## Use the head and tail commands

The head and tail commands are two very useful utilities for displaying the beginning and end of a file, respectively. By combining these commands, we can easily remove the last N lines of a file.

To remove the last “N” lines of a file, we can use the following command −

$head -n -N file.txt > temp && mv temp file.txt  Let's analyze this command − • The “head” command is used to view the beginning of a file. The -n option is used to specify the number of lines to display and the -N option is used to specify the number of lines to skip from the end of the file. • The > operator is used to redirect the output of the head command to a new file called temp. • The && operator is used to concatenate multiple commands. In this case it is used to move the temporary file to the location of the original file, overwriting it. Here is an example of this command in action − $ head -n -7 file.txt > temp && mv temp file.txt


This command will remove the last 7 lines of the file.txt file and save the edited file in the same location.

## Use the sed command

Another option to remove the last N lines of a file is to use the sed command. The sed command is a powerful text processing utility that can be used to perform a variety of tasks, including removing lines from a file.

To remove the last N lines of a file using sed, we can use the following command −

$sed -i ':a;$q;N;$d;ba' file.txt  This command may seem intimidating at first, but it's actually quite simple once you break it down. • Instead, the -i option is used to modify the file, which means that the modified file will be saved in the same location as the original. • The :a and ba commands are used to create a loop in sed. • The$q command is used to exit the loop when we reach the last line of the file.

• The N command is used to add the next line to the pattern space.

• The $d command is used to strip the space from the pattern, which contains the last N lines of the file. Here is an example of this command in action − $ sed -i ':a;$q;N;$d;ba' file.txt


This command will remove the last line of the file.txt file and save the edited file in the same location.

## Use the tac command

Another option to remove the last N lines of a file is to use the tac command. The tac command is used to concatenate and print files backwards, which can be useful for removing the last N lines of a file.

To remove the last N lines of a file using tac, we can use the following command −

$file tac.txt | head -n -N > temp && mv temp file.txt  Let's analyze this command − • The tac command is used to concatenate and print the file file.txt in reverse. • The head command is used to see the beginning of the reversed file. The -n option is used to specify the number of lines to display and the -N option is used to specify the number of lines to skip from the end of the file. • The > operator is used to redirect the output of the head command to a new file called temp. • The && operator is used to concatenate multiple commands. In this case it is used to move the temporary file to the location of the original file, overwriting it. Here is an example of this command in action − $ tac.txt file | head -n -3 > temp && mv temp file.txt


This command will remove the last 3 lines of the file.txt file and save the edited file in the same location.

## Conclusion

In this tutorial, we explore three different methods of removing the last N lines of a file on Linux. Each method has its advantages and disadvantages, so you can choose the one that best suits your needs. Whether you use the head and tail commands, the sed command, or the tac command, you should now have the tools you need to remove the last N lines of a file on Linux.