>_cmd.script

look

Display lines in a sorted file starting with a given string

Text Processing

By CMD Script Team · 4 min read · Last updated

SYNTAX
look [OPTIONS] STRING [FILE]

Options

Command options and flags
FlagDescription
-fIgnore case when comparing (fold uppercase and lowercase together)
-dDictionary order: compare only letters, digits, and blanks
-bForce 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 file
  • look -f Smith names.txt — case-insensitive prefix search
  • sort file.txt > file.sorted && look prefix file.sorted — sort first, since look requires sorted input
look compu

Advanced examples

  • Build a sorted username index once, then repeatedly query it fast with look instead 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: many look implementations require a real file, not stdin — check your platform)
  • Compare performance conceptually against grep "^prefix" file on a multi-million-line sorted file, where look's binary search wins decisively over a linear scan.
cut -d':' -f1 /etc/passwd | sort > usernames.sorted
look adm usernames.sorted

Common mistakes

  • Running look against a file that isn't actually sorted, silently getting incomplete or wrong results because the binary search assumption is violated.
  • Expecting look to search anywhere in a line — it only matches the beginning of lines, not substrings; use grep for substring or arbitrary-position search.
  • Forgetting that with no file argument, look searches the system dictionary (/usr/share/dict/words), which may not even be installed on minimal systems.
  • Reaching for look out of habit on modern systems where grep "^prefix" file is just as practical for typical file sizes and doesn't require pre-sorting.

Tips

  • Only use look when 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 (-f for case folding, -d for dictionary order) or lookups can miss valid matches.
  • Treat look as effectively a legacy/niche utility in modern scripting — it's rarely installed by default on minimal container images, unlike grep.

Best practices

  • Prefer grep "^prefix" file for one-off or occasional lookups; reserve look for 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 look was chosen over grep, 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 grep for simple prefix matching.

Common interview questions

  • How does look differ from grep "^prefix" file? look requires sorted input and performs a binary search, making it much faster on huge sorted files; grep works on unsorted input via a linear scan and is more commonly available and used.
  • What happens if you run look on an unsorted file? The binary search assumption is violated, so look can return incomplete or incorrect results without any warning that the file wasn't properly sorted.
  • What does look search 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.