Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
locate command in Linux with Examples
Next article icon

less command in Linux with Examples

Last Updated : 08 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The ‘less’ command in Linux is an indispensable utility for browsing the contents of text files interactively. Unlike traditional text editors, ‘less’ allows you to view text files page by page without loading the entire file into memory. This approach not only speeds up the file loading process, especially for large files, but also enhances your ability to manage and review lengthy documents or logs efficiently.

What is the ‘less’ command?

At its core, the ‘less’ command operates as a powerful pager program, optimized for quick and resource-efficient viewing of text data. ‘less’ provides a pragmatic solution for exploring and searching text without the overhead of file editors like vi or nano.

Syntax:

The basic syntax of the less command is as follows:

less [options] filename

Here, `filename` represents the name of the file we want to view using the `less` command.

Using ‘less’ with Pipelines

The less command can also be used in conjunction with other commands through pipelines. This allows us to view the output of a command directly in the less pager.

Note: I’m using dmesg output as input to less command in the following examples. 

For Example: If you want to read the contents of dmesg command, it’s better to use it with fewer command  

dmesg | less

Output:  

348Commonly Used Options in`less`command

The less command provides several options that modify its behavior. Here are some commonly used options:

Options

Description

-E

Automatically exit when reaching the end of the file.

-f

Force non-regular files to be opened.

-F

Exit if the entire file can be displayed on the first screen.

-g

Highlight the string that was found by the last search command.

-G

Suppress highlighting of search matches.

-i

Ignore cases when searching.

-n

Suppress line numbers.

-p pattern

Start at the first occurrence of the specified pattern in the file.

-s

Squeeze consecutive blank lines into a single line.

We can combine these options to customize the behavior of the less command according to our needs.

Examples of `less` command in Linux

Let’s look at a few examples to illustrate the usage of the less command with different options.

1. Searching for a pattern

dmesg | less -p "fail"

The above command tells less to start at first occurrence of pattern “fail” in the file and displaying the file from that point.

Output:  

3492. Displaying line number

dmesg | less -N

The -N option displays line numbers along with the file content, allowing you to reference specific lines easily.

Output:

3503. Checking a small file

less -F /home/Mandeep/test/first.erl 

In this example, the file `/home//Mandeep/test/first.erl` is small enough to fit on a single screen. The `-F` option causes less to exit immediately without displaying the file since it can be fully shown in one go.

Conclusion

In this article we have discussed `less` command which allows users to read large text files efficiently by loading and displaying them incrementally. We can say that by utilizing less with pipelines, users can also directly view the output of other commands, enhancing its usefulness. Overall, less command is a valuable tool for managing and exploring the contents of text files in Linux.



Next Article
locate command in Linux with Examples

M

msdeep14
Improve
Article Tags :
  • Linux-Unix
  • linux-command
  • Linux-text-processing-commands

Similar Reads

  • iotop Command in Linux with Examples
    The 'iotop' command is a powerful Linux tool designed for monitoring disk Input/Output (IO) usage in real-time. It provides a comprehensive view of the current disk IO activities by processes, making it invaluable for system administrators who need to track down processes that are causing high disk
    3 min read
  • ip Command in Linux with Examples
    The ip command in Linux is a powerful utility for network configuration and management. It allows users to interact with various networking components such as network interfaces, routing tables, addresses, and more. Here, we will look into the 'ip' command, covering each aspect with examples, code,
    7 min read
  • ipcrm command in Linux with examples
    ipcrm command in Linux is used to remove some IPC(Inter-Process Communication) resources. It eliminates the IPC objects and their associated data structure form the system. One must be a creator or superuser or the owner of the object in order to remove these objects. There are three types of System
    2 min read
  • 'IPCS' command in Linux with examples
    ipcs shows information on the inter-process communication facilities for which the calling process has read access. By default, it shows information about all three resources: shared memory segments, message queues, and semaphore arrays. Without options, the information shall be written in short for
    3 min read
  • iptables command in Linux with Examples
    The iptables command in Linux is a powerful tool that is used for managing the firewall rules and network traffic. It facilitates allowing the administrators to configure rules that help how packets are filtered, translated, or forwarded. On using this iptables, you can set up security policies to c
    8 min read
  • iptables-save command in Linux with examples
    The information transfer from a remote computer to your local computer and the vice-versa are viewed as the transfer of data packets by the firewall. The firewall has the control of data packets that are both incoming and outgoing.iptables is a utility to create a rule-based firewall that is pre-ins
    3 min read
  • iwconfig command in Linux with Examples
    Wireless networking plays a crucial role in modern computing, enabling devices to connect and communicate without the need for physical cables. In the Linux ecosystem, the iwconfig command stands as a powerful tool for configuring and managing wireless network interfaces. This article will delve int
    5 min read
  • join Command in Linux
    The 'join' command in UNIX is a powerful command-line utility that allows you to merge lines from two files based on a common field, effectively combining related data into a more meaningful format. For instance, if you have one file with names and another with IDs, the join command can combine thes
    7 min read
  • journalctl Command in Linux with Examples
    The `journalctl` command is part of the systemd suite of utilities and is used to query and display log messages from the systemd journal. The systemd journal is a centralized logging system that collects and stores log data from various sources, including system services, kernel events, and user ap
    7 min read
  • How to Kill a Process in Linux | Kill Command
    kill command in Linux (located in /bin/kill), is a built-in command which is used to terminate processes manually. kill command sends a signal to a process that terminates the process. If the user doesn't specify any signal that is to be sent along with the kill command, then a default TERM signal i
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences