less Command in Linux
Overview
The 'less' command in Linux is a powerful utility for viewing text files. Unlike the 'more' command, it allows you to scroll through the file in both forward and backward directions, making it easier to navigate and read large text files. In this article, we'll explore the syntax, options, and common use cases of the 'less' command.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Linux less Command Syntax
The syntax for the less command is as follows:
Where:
- options: Options are the flags that modify the behavior of the 'less' command.
- file: The file is the target text file to be viewed using the 'less' command.
less Command Options:
- -N: Display line numbers at the beginning of each line.
- -I: Case-insensitive search.
- -G: Disable colorized output for regular expressions.
- -X: Do not use termcap initialization and deinitialization strings.
Example Usages
-
View a file using 'less':
Explanation: Opens the file 'example.txt' and displays its contents, allowing the user to scroll up and down the text.
-
Search for a pattern within a file:
Explanation: After opening 'example.txt' with 'less', you can search for a pattern by typing '/' followed by the pattern you want to search for, and then press Enter.
Turn Learning into Career Growth
Tips
-
You can navigate through the file using the arrow keys, Page Up/Down, or the spacebar.
-
Press 'q' to exit the 'less' command.
Advanced Use Cases of less Command in Linux
-
View multiple files:
Explanation: Opens both 'file1.txt' and 'file2.txt' in 'less'. You can switch between files using '
' for the previous file.' for the next file and ' -
Follow the output of a file in real-time:
Explanation: Opens 'example.log' and starts following the output in real-time, similar to the 'tail -f' command.
-
View a compressed file:
Explanation: Opens and decompresses the compressed 'example.gz' file on-the-fly, allowing you to view its contents without manual decompression.
Conclusion
-
The 'less' command is a powerful and flexible text file viewer in Linux.
-
It allows you to navigate through large files easily, both forwards and backwards.
-
You can search for patterns, view multiple files, and even follow output in real-time.