Grep chokes on huge sorted files: look finds every line starting with a prefix almost instantly with a binary search. The examples use the installed manpage from Ubuntu's bsdextrautils package, version 2.39.3-9ubuntu6.6. The command found first in this shell is util-linux 2.41.3, so check your own version when exact release behaviour matters.
Allow about ten minutes. You need a shell and a text file whose ordering you control. This guide only reads files and writes a temporary example under /tmp; no elevated privileges are needed. Do not point a command that creates a sorted file at an important path until you have checked the output and destination.
Confirm which implementation will run and read the concise help:
$ command -v look
$ look --version
$ look --help
look as a binary-search utility, not something that reads arbitrary text line by line.look compares it. Change the comparison options and you must give sort the matching options too.Checkpoint: record the path and version if a script or support note must be reproducible. A different executable earlier in PATH can explain a version mismatch without any change to the operating system package.
Use a harmless temporary file so the ordering and result are visible:
$ printf '%s\n' \
'apple 10' \
'application 20' \
'banana 30' \
'band 40' \
'zebra 50' > /tmp/look-example.txt
$ sort -d /tmp/look-example.txt -o /tmp/look-example.txt
$ sed -n '1,5p' /tmp/look-example.txt
apple 10
application 20
banana 30
band 40
zebra 50
The redirection creates or replaces only the named temporary file. The final sort command updates that same file after sorting. With a real data file, write to a separate temporary path first, inspect it, then replace the original only if that change is genuinely intended.
Pass the prefix first and the file second:
$ look appl /tmp/look-example.txt
apple 10
application 20
Both lines come back because their beginnings match appl. This is not a regular expression: characters such as ., * and ? do not gain pattern syntax here. Reach for grep when you actually need regular expressions or the file isn't sorted.
Check the no-match case and its status too, because scripts live or die on this:
$ look kiwi /tmp/look-example.txt
$ printf 'status=%s\n' "$?"
status=1
Capture $? immediately, before running another command, or you'll be capturing the wrong thing's exit code.
The -t option tells look where the searchable prefix ends, which matters for a file sorted by a delimited key:
$ sort -d -t: -k1,1 /etc/passwd > /tmp/passwd-by-name
$ look -t: root:anything /tmp/passwd-by-name
root:x:0:0:root:/root:/bin/bash
Only the text through the first colon in the search string is compared, including that colon itself. The remainder, anything here, is ignored for the lookup. Use a separate output file: the example does not touch the original /etc/passwd and does not need root access.
Do not assume a colon-separated file is ordered correctly just because it looks tidy. The sort key and comparison mode have to match the lookup. If the file came from another system, check its documented ordering before trusting a status 1 as proof that a key is absent.
Leave off a filename and look falls back to /usr/share/dict/words. The manpage says this default mode compares only alphanumeric characters, ignores case and uses dictionary ordering. The WORDLIST environment variable overrides that default path.
$ if [ -r /usr/share/dict/words ]; then
> look -d -f net
> else
> printf '%s\n' 'No readable default dictionary at /usr/share/dict/words' >&2
> fi
A missing dictionary is not fixed by adding sudo to look. Check the path, the WORDLIST value and the package that supplies your dictionary. For a portable command, pass an explicit file rather than relying on a host's default dictionary.
For a custom file, use -d and -f consistently on both commands:
$ sort -d -f /tmp/look-example.txt -o /tmp/look-example.txt
$ look -d -f APP /tmp/look-example.txt
These options change comparison. Sort without them and search with them, or the other way round, and you can get missed results even when the file looks sorted to a person.
sort options, then test a prefix whose expected lines are known.grep -F for fixed-string scanning, or grep for regular expressions. look is for ordered prefix lookup only.rm -- /tmp/look-example.txt /tmp/passwd-by-name. This cleanup is optional and does not touch the source files.look.WORDLIST apply to your command.