Introduction
In this lab, you will learn about file paging in Linux using the less command. The less command is a powerful tool that allows you to view text files one screen at a time, making it easier to navigate through large files without overwhelming your terminal screen with text.
File paging is an essential skill for Linux users, particularly when working with configuration files, logs, or any large text file where you need to examine content methodically. Unlike text editors that load the entire file into memory, less loads only the portions you view, making it efficient for very large files.
By the end of this lab, you will be comfortable using the basic navigation features of less, searching within files, and understanding how to efficiently read through text files of any size in a Linux environment.
Basic Usage of the less Command
In this step, you will learn the fundamentals of the less command for viewing text files in Linux. The less command allows you to view files one page at a time, making it easier to read large files without overwhelming your terminal screen.
Let's start by navigating to your project directory:
cd ~/project
To view the sample file using the less command, type:
less sample-file.txt
When you run this command, you'll see the contents of the file displayed in your terminal. You should see the first few numbers displayed on your screen.
Unlike the cat command which displays the entire file at once, less shows you only one screen of text at a time. This is especially useful for viewing large files.
While in the less interface, you can use the following basic keyboard controls:
- Press the
Spacekey orPage Downto move forward one page - Press
borPage Upto move backward one page - Press
qto quitlessand return to the command prompt
Try using these controls to navigate through the file. Use the Space key to go forward a page, then use b to go back a page. When you're done exploring, press q to exit the less command and return to your terminal prompt.
These basic navigation controls are just the beginning of what less can do. In the next steps, we'll explore more advanced features of this powerful command.
Navigating Within Files Using less
Now that you're familiar with the basic usage of less, let's explore more navigation features that make it a powerful tool for viewing files in Linux.
Let's open the navigation file with less:
less navigation-file.txt
In addition to the basic navigation commands you learned in the previous step, here are some more useful navigation commands to try:
- Press
jorDown Arrowto move down one line - Press
korUp Arrowto move up one line - Press
gto go to the beginning of the file - Press
Gto go to the end of the file - Type a number followed by
gto go to that line number (e.g.,5gto go to line 5) - Type a number followed by
Gto go to that percentage of the file (e.g.,50Gto go to 50% of the file)
Try these commands to navigate through the file. For example:
- Press
Gto go to the end of the file - Press
gto go back to the beginning - Type
5gto go directly to line 5 - Use the arrow keys to move up and down line by line
When viewing large files, these navigation commands can save you a lot of time by allowing you to quickly jump to specific sections of the file.
When you're done exploring, press q to exit less and return to your terminal prompt.
Searching in Files with less
One of the most powerful features of the less command is its ability to search through files. This is especially useful when you're looking for specific information in large files like logs or configuration files.
Let's open the sample log file with less:
less sample-log.txt
To search for text while in less, you can use the following commands:
- Type
/patternand pressEnterto search forward for "pattern" - Type
?patternand pressEnterto search backward for "pattern" - Press
nto find the next occurrence of the search pattern - Press
Nto find the previous occurrence of the search pattern
Let's try searching for error messages in our log file:
- Type
/ERRORand pressEnter - You should see the first ERROR message highlighted
- Press
nto find the next occurrence of "ERROR" - Press
Nto go back to the previous occurrence
You can also search for other patterns. Try searching for:
/WARNINGto find warning messages/INFOto find information messages
The search function in less is case-sensitive by default. To perform a case-insensitive search, you can use the -i option when starting less:
less -i sample-log.txt
With this option, searching for /error will find both "error" and "ERROR" in the file.
When you're done exploring, press q to exit less and return to your terminal prompt.
The search functionality makes less an extremely useful tool for quickly finding information in large text files without having to scroll through the entire file manually.
Summary
In this lab, you've learned how to use the less command, one of the most useful file paging utilities in Linux. Here's a summary of what you've accomplished:
You've learned the basic usage of the
lesscommand to view files one page at a time, which is particularly useful for large files.You've explored various navigation commands within
less, including:- Moving forward and backward by page (
Spaceandb) - Moving up and down by line (arrow keys,
j, andk) - Jumping to the beginning and end of a file (
gandG) - Going to specific line numbers or percentages of a file
- Moving forward and backward by page (
You've learned how to search for text within files using the
/patternand?patterncommands, and how to navigate between search results usingnandN.
These skills will be valuable when you're working with configuration files, log files, or any large text files in a Linux environment. Unlike text editors that load the entire file into memory, less is designed to be efficient with resources, making it ideal for viewing very large files.
The less command has many more advanced features not covered in this lab, including the ability to mark positions in a file, display line numbers, and execute shell commands. As you become more comfortable with the basics, you can explore these advanced features to further enhance your file viewing capabilities in Linux.



