Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux less Command: Display File Content

The less command in Linux is a versatile and convenient text file viewer that allows you to view the contents of a file while providing advanced navigation features. In this tutorial, we'll cover basic usage examples and navigation shortcuts for the less command.

  • Open a file with less:

To open a file using less, simply type less followed by the file name:

less file_name.txt
  • Navigating through the file:

Once a file is open in less, you can use the following keyboard shortcuts to navigate through the file:

  • Up arrow or k: Move up by one line
  • Down arrow or j: Move down by one line
  • Page Up or b: Move up by one page
  • Page Down or Space: Move down by one page
  • g: Go to the beginning of the file
  • G: Go to the end of the file
  • q: Quit less
  • Search within the file:

You can search for text within the file using the following shortcuts:

  • /search_term: Search forward for the given search term
  • ?search_term: Search backward for the given search term
  • n: Repeat the last search in the same direction
  • N: Repeat the last search in the opposite direction
  • Follow mode:

If you are viewing a file that is being actively written to, such as a log file, you can use less's follow mode to display new content as it is added:

  • Press F while the file is open to enter follow mode
  • Press Ctrl + C to exit follow mode
  • Open multiple files:

You can open multiple files with less and navigate between them using the following commands:

  • Open multiple files:

    less file1.txt file2.txt
    
  • Navigate between files:

    • :n: Go to the next file
    • :p: Go to the previous file
  • Display line numbers:

To display line numbers while viewing a file, press the -N option:

less -N file_name.txt

In summary, the less command is a powerful text file viewer in Linux that provides advanced navigation and search features. By understanding the basic usage examples and shortcuts, you can efficiently view and navigate through text files.

  1. How to use less command in Linux:

    The less command is a pager that allows you to view and navigate through text files.

    Example code:

    less filename
    
  2. Viewing file contents with less:

    Use less to open and view the contents of a file. Scroll using arrow keys.

    Example code:

    less filename
    
  3. Navigating and searching within files using less:

    Navigate through files using arrow keys. Search within files by typing / followed by the search term.

    Example code:

    less filename
    

    Press / and type the search term.

  4. Displaying line numbers and file information in less:

    Show line numbers with the -N option. Display file information with the -M option.

    Example code (with line numbers):

    less -N filename
    

    Example code (with file information):

    less -M filename
    
  5. Piping command output to less in Linux:

    Pipe the output of a command to less for easy navigation.

    Example code:

    command | less
    
  6. Scrolling and paging through large files with less:

    Scroll through files using arrow keys or use spacebar to page down. Press b to page up.

  7. Exiting and quitting less command in Linux:

    Exit less by pressing q. Press Q (Shift + q) to force quit without confirmation.

  8. Setting options and customizing less behavior:

    Customize less behavior using command-line options. For example, set line-wrap with -S.

    Example code:

    less -S filename