Cheat Sheet #day53 - less

Cloud TunedCloud Tuned
3 min read

less Command Cheatsheet

The less command in Unix/Linux is a terminal pager program used to view (but not modify) the contents of a file one screen at a time. It allows for both forward and backward navigation through the file.

Basic Syntax

less [OPTIONS] [FILE]

Navigation Commands

  • Space or f: Move forward one screen.

  • b: Move backward one screen.

  • Enter: Move forward one line.

  • y: Move backward one line.

  • d: Move forward half a screen.

  • u: Move backward half a screen.

  • g: Go to the beginning of the file.

  • G: Go to the end of the file.

  • ng: Go to line number n.

  • /pattern: Search forward for pattern.

  • ?pattern: Search backward for pattern.

  • n: Repeat previous search (forward).

  • N: Repeat previous search (backward).

Options

  • -N: Display line numbers.

  • -S: Chop long lines (don't wrap).

  • -X: Disable termcap initialization/deinitialization (useful in scripts).

  • -F: Automatically exit if the file fits on one screen.

  • -R: Display raw control characters.

  • -i: Ignore case in searches.

  • -m: Display a more verbose prompt.

Useful Commands

  • h: Display help screen.

  • q: Quit less.

  • &pattern: Display only lines matching the pattern.

  • m<letter>: Mark the current position with <letter>.

  • 'letter: Return to a previously marked position.

  • =, Ctrl+g: Display the current file name, line number, and percentage through the file.

  • F: Follow the end of the file as it grows (like tail -f).

Examples and Use Cases

View a File

less filename.txt
  • Opens filename.txt for viewing.

View with Line Numbers

less -N filename.txt
  • Opens filename.txt with line numbers displayed.

Search for a Pattern

less filename.txt
  • Inside less, type /pattern and press Enter to search for pattern.

Ignore Case in Searches

less -i filename.txt
  • Opens filename.txt and ignores case in searches.

Follow File Growth

less +F filename.txt
  • Opens filename.txt and follows the end of the file as it grows.

Display Long Lines Without Wrapping

less -S filename.txt
  • Opens filename.txt and does not wrap long lines.

View Command Output

command | less
  • Pipes the output of command to less for easier viewing.

View Multiple Files

less file1.txt file2.txt
  • Opens file1.txt, and after quitting, file2.txt will be opened.

Jump to a Line Number

less filename.txt
  • Inside less, type 50g to jump to line 50.

This cheatsheet provides a quick reference to the most common usages and options for the less command. For more detailed information, you can always refer to the less man page by typing man less in your terminal.

0
Subscribe to my newsletter

Read articles from Cloud Tuned directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Cloud Tuned
Cloud Tuned