look
Display lines in a sorted file starting with a given string
By CMD Script Team · 4 min read · Last updated
look [OPTIONS] STRING [FILE]Options
| Flag | Description |
|---|---|
-f | Ignore case when comparing (fold uppercase and lowercase together) |
-d | Dictionary order: compare only letters, digits, and blanks |
-b | Force look to use binary search (assumes the file is properly sorted) |
(no FILE) | Defaults to searching /usr/share/dict/words, the system word list, when no file is given |
Distribution compatibility
- Ubuntu
- Debian
- Fedora (may require the 'bsdmainutils' or 'util-linux' package)
- Arch
- macOS (BSD look, included by default)
What it does
look searches a sorted file for lines that begin with a given string, using a binary
search rather than a linear scan. Because it requires the input to already be sorted,
look can locate matches in a huge file almost instantly, jumping straight to the
relevant region instead of reading every line. Historically its main use was querying the
system dictionary (/usr/share/dict/words) for words with a given prefix, and it remains
mostly a niche/legacy tool today, largely superseded in everyday use by grep.
Beginner examples
look pre— find dictionary words starting with "pre" (searches the default word list)look admin /etc/passwd.sorted— find lines starting with "admin" in a sorted filelook -f Smith names.txt— case-insensitive prefix searchsort file.txt > file.sorted && look prefix file.sorted— sort first, sincelookrequires sorted input
look compu
Advanced examples
- Build a sorted username index once, then repeatedly query it fast with
lookinstead of re-scanning:cut -d':' -f1 /etc/passwd | sort > usernames.sorted && look admin usernames.sorted - Use
-d(dictionary order) to ignore punctuation when matching against text that includes symbols:look -d "co-op" terms.sorted.txt - Combine with a pre-sort step in a one-liner for ad hoc files:
sort logs.txt | look ERROR /dev/stdin(note: manylookimplementations require a real file, not stdin — check your platform) - Compare performance conceptually against
grep "^prefix" fileon a multi-million-line sorted file, wherelook's binary search wins decisively over a linear scan.
cut -d':' -f1 /etc/passwd | sort > usernames.sorted
look adm usernames.sorted
Common mistakes
- Running
lookagainst a file that isn't actually sorted, silently getting incomplete or wrong results because the binary search assumption is violated. - Expecting
lookto search anywhere in a line — it only matches the beginning of lines, not substrings; usegrepfor substring or arbitrary-position search. - Forgetting that with no file argument,
looksearches the system dictionary (/usr/share/dict/words), which may not even be installed on minimal systems. - Reaching for
lookout of habit on modern systems wheregrep "^prefix" fileis just as practical for typical file sizes and doesn't require pre-sorting.
Tips
- Only use
lookwhen you have a large, already-sorted file and need repeated fast prefix lookups — for anything else,grep "^prefix"is simpler and more portable. - Make sure the sort order used to prepare the file matches
look's comparison mode (-ffor case folding,-dfor dictionary order) or lookups can miss valid matches. - Treat
lookas effectively a legacy/niche utility in modern scripting — it's rarely installed by default on minimal container images, unlikegrep.
Best practices
- Prefer
grep "^prefix" filefor one-off or occasional lookups; reservelookfor genuinely performance-sensitive repeated prefix queries against a large sorted dataset. - Always sort the target file with options consistent with how you plan to query it
(matching case sensitivity and locale) before relying on
look's binary search. - Document in scripts why
lookwas chosen overgrep, since it's an unusual choice that later readers may not recognize.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Quickly checking if a word exists in the system dictionary or finding word completions
for a given prefix:
look program. - Fast prefix lookups against a large, presorted username or hostname index built once and queried repeatedly.
- Legacy shell scripts and older Unix tooling that predate the ubiquity of
grepfor simple prefix matching.
Common interview questions
- How does
lookdiffer fromgrep "^prefix" file?lookrequires sorted input and performs a binary search, making it much faster on huge sorted files;grepworks on unsorted input via a linear scan and is more commonly available and used. - What happens if you run
lookon an unsorted file? The binary search assumption is violated, solookcan return incomplete or incorrect results without any warning that the file wasn't properly sorted. - What does
looksearch if you don't give it a file? It defaults to/usr/share/dict/words, the system dictionary, making its classic use case checking whether a prefix matches a valid English word.
Frequently Asked Questions
How is look different from grep '^prefix' file?
look requires the target file to be sorted and uses binary search to jump directly to matching lines, making it far faster on very large sorted files than a linear scan with grep. grep works on unsorted input but must scan every line, which is slower at scale but far more commonly used and available.
Why does look return nothing even though the string clearly appears in the file?
look assumes the file is sorted (in the same collating order look expects) and performs a binary search; if the file isn't actually sorted, or is sorted with a different locale/case sensitivity than look expects, the binary search can miss real matches. Sort the file first with a matching sort -f/-d convention.
What file does look search if I don't specify one?
By default, look searches /usr/share/dict/words, the system's dictionary word list, which is why its most classic use case is checking whether a string is a valid English word or finding words with a given prefix.
Cheat sheet
Download a quick-reference cheat sheet for look.