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]
Dictionary Search
If no file is specified, look searches the system dictionary.
Find words starting with "anti"
look anti
anti
antibiotic
antibody
...
Case Insensitive (-f)
Ignore case. "f" stands for "fold case".
look -f LINUX
# Matches: linux, Linux, Linux's
File Search
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
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
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:
0if found,1if not found. - Essential for spell-checking scripts.