Skip to main content
ubuntuask.com

Back to all posts

How to Read the Last N Lines Of A File In Linux?

Published on
5 min read
How to Read the Last N Lines Of A File In Linux? image

Best Tools to Read Files in Linux to Buy in November 2025

1 SmartQ C368 USB 3.0 Card Reader - Plug & Play, Compatible with Apple & Windows, Supports SD, Micro SD, MS, CF Cards

SmartQ C368 USB 3.0 Card Reader - Plug & Play, Compatible with Apple & Windows, Supports SD, Micro SD, MS, CF Cards

  • FOUR-IN-ONE DESIGN: SUPPORTS MICRO SD, SD, MS, AND CF CARDS.
  • HIGH-SPEED TRANSFER: UP TO 5GB/S FOR QUICK DATA ACCESS.
  • PLUG AND PLAY: COMPATIBLE WITH ALL MAJOR OS, NO DRIVERS NEEDED.
BUY & SAVE
$9.99
SmartQ C368 USB 3.0 Card Reader - Plug & Play, Compatible with Apple & Windows, Supports SD, Micro SD, MS, CF Cards
2 Mind Reader Vertical File Storage, Desktop Organizer, Workspace, Office, Metal Mesh, 12.5" L x 3.75" W x 11.5" H, Black

Mind Reader Vertical File Storage, Desktop Organizer, Workspace, Office, Metal Mesh, 12.5" L x 3.75" W x 11.5" H, Black

  • THREE-TIER DESIGN FOR EASY ACCESS AND CLEAR VISIBILITY.
  • STYLISH METAL MESH COMPLEMENTS MODERN OFFICE OR KITCHEN DECOR.
  • COMPACT SIZE AND LIGHTWEIGHT FOR CONVENIENT DESK OR COUNTERTOP USE.
BUY & SAVE
$17.99 $19.99
Save 10%
Mind Reader Vertical File Storage, Desktop Organizer, Workspace, Office, Metal Mesh, 12.5" L x 3.75" W x 11.5" H, Black
3 SD Card Reader for iPhone, iPad, USB C and Lightning Dual Port SD Card Adapter, Memory Card Reader for Camera Adapter, Support SD, Micro SD, USB 3.0, No App Driver Required

SD Card Reader for iPhone, iPad, USB C and Lightning Dual Port SD Card Adapter, Memory Card Reader for Camera Adapter, Support SD, Micro SD, USB 3.0, No App Driver Required

  • UNMATCHED COMPATIBILITY: WORKS WITH IOS, WINDOWS, MAC, AND ANDROID DEVICES.
  • HIGH-SPEED TRANSFERS: ACHIEVE DATA SPEEDS UP TO 5 GBPS FOR QUICK ACCESS.
  • TWO-WAY DATA TRANSFER: EASILY IMPORT/EXPORT FILES FROM ANY DEVICE.
BUY & SAVE
$9.99 $12.99
Save 23%
SD Card Reader for iPhone, iPad, USB C and Lightning Dual Port SD Card Adapter, Memory Card Reader for Camera Adapter, Support SD, Micro SD, USB 3.0, No App Driver Required
4 SD Card Reader for iPhone/ipad/Android/Mac/Computer/Camera,4 in1 Micro SD Card Reader Trail Camera Viewer, Portable Memory Card Reader SD Card Adapter for SD and TF Cards(White)

SD Card Reader for iPhone/ipad/Android/Mac/Computer/Camera,4 in1 Micro SD Card Reader Trail Camera Viewer, Portable Memory Card Reader SD Card Adapter for SD and TF Cards(White)

  • 4-IN-1 VERSATILITY: CONNECT EFFORTLESSLY ACROSS ALL YOUR DEVICES.

  • CHARGE & TRANSFER SIMULTANEOUSLY: NEVER MISS A MOMENT OF POWER!

  • QUICK DATA MANAGEMENT: SPEEDY TWO-WAY TRANSFERS SAVE YOU VALUABLE TIME.

BUY & SAVE
$19.99
SD Card Reader for iPhone/ipad/Android/Mac/Computer/Camera,4 in1 Micro SD Card Reader Trail Camera Viewer, Portable Memory Card Reader SD Card Adapter for SD and TF Cards(White)
5 Mind Reader Hanging File Folder Organizer, Desktop Organizer, Storage, Office, Metal Mesh, 13" L x 9.75" W x 10.25" H, Black

Mind Reader Hanging File Folder Organizer, Desktop Organizer, Storage, Office, Metal Mesh, 13" L x 9.75" W x 10.25" H, Black

  • SLEEK METAL MESH DESIGN ENHANCES ANY DECOR EFFORTLESSLY.
  • TOOL-FREE ASSEMBLY LETS YOU SET UP IN MINUTES!
  • DURABLE, EASY TO CLEAN-PERFECT FOR EVERYDAY USE!
BUY & SAVE
$18.69 $19.99
Save 7%
Mind Reader Hanging File Folder Organizer, Desktop Organizer, Storage, Office, Metal Mesh, 13" L x 9.75" W x 10.25" H, Black
6 Mind Reader Vertical File Storage, Desktop Organizer, Wall Mount, Office, Metal Mesh, 12.5" L x 3.75" W x 11.5" H, Black

Mind Reader Vertical File Storage, Desktop Organizer, Wall Mount, Office, Metal Mesh, 12.5" L x 3.75" W x 11.5" H, Black

  • MAXIMIZE SPACE WITH CONVENIENT VERTICAL THREE-TIER STORAGE!
  • VERSATILE DESIGN: ORGANIZE FILES, MAIL, AND MAGAZINES EFFORTLESSLY.
  • STYLISH METAL MESH COMPLEMENTS ANY MODERN OFFICE OR KITCHEN DECOR.
BUY & SAVE
$17.53 $20.99
Save 16%
Mind Reader Vertical File Storage, Desktop Organizer, Wall Mount, Office, Metal Mesh, 12.5" L x 3.75" W x 11.5" H, Black
+
ONE MORE?

To read the last N lines of a file in Linux, you can use various commands and techniques. Here is how you can do it:

  1. Using tail command: The tail command allows you to display the last N lines of a file. Open the terminal and type the following command, replacing "N" with the desired number of lines: tail -n N filename This will display the last N lines of the specified file.
  2. Using head and tac commands: You can combine the head and tac commands to achieve the same result. Open the terminal and execute the following command: tac filename | head -n N | tac This will reverse the lines in the file using tac, then use head to display the first N lines, and finally reverse it back using tac to get the original order of lines.
  3. Using sed command: The sed command is a powerful stream editor that can be used to manipulate text. To read the last N lines of a file using sed, execute the following command: sed -n '{N}p' filename Replace "{N}" with the desired number of lines you want to read.
  4. Using awk command: The awk command is another text processing tool. To read the last N lines of a file using awk, open the terminal and type: awk 'NR > (total_lines - N)' filename Replace "N" with the number of lines you want to read and replace "total_lines" with the total number of lines in the file.

These methods will allow you to read the last N lines of any file in Linux, providing you with flexibility and control over your file reading operations.

What is the option to display line numbers while reading the last N lines of a file with tail?

The option to display line numbers while reading the last N lines of a file with tail is -n.

The syntax is as follows:

tail -n N filename

This will display the last N lines of the file with line numbers.

How to read the last N lines of a file and append it to another file in Linux?

To read the last N lines of a file and append it to another file in Linux, you can use the tail command to extract the last N lines, and then use the >> operator to append the output to another file. Here is the syntax:

tail -n N <source_file> >> <destination_file>

Replace N with the number of lines you want to extract, <source_file> with the file you want to read from, and <destination_file> with the file you want to append the lines to.

For example, to read the last 10 lines of file1.txt and append it to file2.txt, you can use the following command:

tail -n 10 file1.txt >> file2.txt

What is the command to read the last N lines of a file in reverse order and save it to a new file?

The tail command can be used to read the last N lines of a file, and the tac command can be used to reverse the order of lines. To save the reversed lines to a new file, you can use the output redirection operator >.

The command to read the last N lines of a file in reverse order and save it to a new file can be written as follows:

tail -n N file.txt | tac > new_file.txt

Replace N with the desired number of lines and file.txt with the name of the file you want to read from, and new_file.txt with the name of the file you want to save the reversed lines to.

What is the option to follow and display new lines while reading a file using tail?

The option to follow and display new lines while reading a file using tail is tail -f.

The -f option stands for "follow" and it emulates the tail command to continuously display the content of a file as it grows, by displaying any new lines that are added to the file in real-time. This is particularly useful when monitoring log files or continuously updated files.

How to read the last N lines of a file without displaying the file name in Linux?

To read the last N lines of a file without displaying the file name in Linux, you can use the following command:

tail -n N filename | sed 's/^.*$/&/'

Here, replace "N" with the desired number of lines to read from the end of the file, and "filename" with the actual name or path of the file.

Explanation:

  • The tail command is used to read the last N lines of a file.
  • The -n option specifies the number of lines to display, and "N" should be replaced with the desired number.
  • The sed command is used to perform a substituion operation that does not change anything. In this case, the regular expression ^.*$ matches the entire line, and & is a special character that represents the entire matched pattern. So, the sed command essentially keeps the line unchanged.

By default, the tail command will prefix each line with the file name. However, the sed command is used here to replace the file name with itself, effectively removing it from the output.

What is the command to read the last N lines of a file in reverse order?

The tail command in Linux can be used to read the last N lines of a file. To read the last N lines in reverse order, you can combine the tail and tac commands using a pipe.

The command to read the last N lines of a file in reverse order is:

tail -n N filename | tac

Replace N with the desired number of lines and filename with the name of the file you want to read. The tail -n N command extracts the last N lines, and the tac command reverses the order of the lines.