Remove the First Line of a Text File in Linux

There are several ways to remove the first line of a text file in Linux. In this article, we will explore four different methods that can be used to accomplish this task, each with its own advantages and syntax.

Method 1: Using the head Command

The head command displays the first few lines of a text file. To remove the first line, we use the -n option with a negative value. The -n -1 option tells head to display all lines except the last one, which effectively removes the first line when we want all but the first.

Example

To remove the first line of a text file called file.txt, use the following command

$ head -n -1 file.txt > newfile.txt

This command creates a new file called newfile.txt containing all lines of file.txt except the first line.

Method 2: Using the sed Command

The sed command is a stream editor used for text transformations. It can delete specific lines using the d command. To remove the first line, we specify 1d, which means "delete line 1".

Example

To remove the first line of file.txt using sed

$ sed '1d' file.txt > newfile.txt

This command deletes the first line and redirects the remaining content to newfile.txt.

Method 3: Using the tail Command

The tail command displays the last few lines of a file. Using the +n option, we can specify to start displaying from line n onwards. By using +2, we start from the second line, effectively skipping the first.

Example

To remove the first line using tail

$ tail -n +2 file.txt > newfile.txt

This command outputs all lines starting from line 2 to newfile.txt.

Method 4: Using the awk Command

The awk command is a powerful text processing tool that can perform complex pattern matching and data manipulation. It uses built-in variables like NR (Number of Records) to track line numbers.

Example

To remove the first line using awk

$ awk 'NR>1' file.txt > newfile.txt

This command prints all lines where the line number (NR) is greater than 1. Alternatively, you can use

$ awk 'FNR>1' file.txt > newfile.txt

The FNR variable (File Number of Records) works similarly but resets for each file when processing multiple files.

Performance Comparison

Command Speed Memory Usage Best For
head Fast Low Large files, simple operations
tail Fast Low Large files, simple operations
sed Moderate Low Complex text transformations
awk Moderate Moderate Complex pattern matching

The head and tail commands are generally faster for simple operations on large files because they read only the necessary portions. The sed and awk commands offer more flexibility for complex text processing but may use more resources.

Conclusion

Linux provides multiple efficient methods to remove the first line of a text file. For simple tasks, tail -n +2 or head -n -1 are the fastest options. For more complex text processing requirements, sed and awk offer greater flexibility and power.

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

30K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements