Skip to content

look Command Cheat Sheet

The look command displays lines beginning with a given string. It is famous for looking up words in the English dictionary (/usr/share/dict/words), but can efficiently search any sorted file.


Synopsis

look [options] string [file]

If no file is specified, look searches the system dictionary.

Find words starting with "anti"

look anti
Output:
anti
antibiotic
antibody
...

Case Insensitive (-f)

Ignore case. "f" stands for "fold case".

look -f LINUX
# Matches: linux, Linux, Linux's

Binary Search Speed

look uses binary search. This means it is blazing fast on huge files, but the file MUST be sorted.

look "2024-01-01" logs.sorted.txt
(Finds all log lines starting with that date).


Advanced Options

Only Alphanumeric (-d)

"Dictionary" order. Only letters and numbers are compared; punctuation is ignored.

look -d check

Define Termination Character (-t)

Specify a string termination character, i.e., only match up to this char.

look -t : faruk /etc/passwd
(Searches for lines starting with faruk and stopping comparison at :. Perfect for /etc/passwd).


Notes

  • Prereq: If using on a custom file, sort it first: sort file > file.sorted.
  • Exit Code: 0 if found, 1 if not found.
  • Essential for spell-checking scripts.