• United States
Unix Dweeb

Using the Linux look command to select lines from files

Jan 17, 20224 mins

man with binoculars future it vision horizon by marvod getty
Credit: marvod / Getty Images

The look command on Linux can be handy for selecting particular lines from text files with sorted contents. Let’s look into how it can be used and where you might run into some problems.

Case sensitivity

If you type a command such as “look unix”, you should see this:

$ look unix

Notice that, because no file was specified in the command shown, look reverts to using the words file on the system (probably /usr/share/dict/words or whatever that points to). Also notice that it finds the three lines in the file even though the argument for the command has only lowercase characters. The command is case-insensitive when you don’t provide a file name and instead allow it to default to the words file.

For a word like “snow”, you might get a couple hundred responses. Here’s just the beginning of what you should see:

$ look snow | column | head -5
Snow                    snowdrifts              snow-nodding
snow                    snow-driven             snow-on-the-mountain
Snowball                snowdrop                snowpack
snowball                snow-dropping           snowpacks
snowballed              snowdrops               snow-plough

Piping the output to column provides a multi-column display. In the output above, we use the head command to show only the first five lines.

Specifying the file to use

To pull lines from an arbitrary text file, add its name to the look command like this:

$ look 2 myfile
2) I don't like my file

In that command, look selects lines from the file that begin with “2”. To be a little more specific, you could do this:

$ look "2)" myfile
2) I don't like my file

The quotes around the search term are needed in this case since the parenthesis might otherwise be interpreted as a delimiter.

If you are looking for content in some file other than the words file, you will also likely need to include a -f (or –ignore-case) option to view contents regardless of case. As mentioned earlier, while the look command is case-insensitive when using the words file, this isn’t so when specifying some other file, so you may need the option to make it do what you want. In the example below, you can see how much difference this can make.

$ look k shopping_list
$ look -f k shopping_list
Klondike bars

This can be important to remember. After all, you don’t want to risk forgetting those Klondike bars!

The look command expects files to be sorted

It’s important to remember that the look command expects files to be sorted alphanumerically. Let’s say we were trying to extract content from a file with this content:

$ cat nums

If we ask look to select the line with the “4”, all is well.

$ look 4 nums

If we ask look to select the line beginning with a “5”, on the other hand, it doesn’t comply because the 5 appears following a 6. In other words, it may stop searching when it encounters an error in the sort order.

$ look 5 nums

You also can't simply sort a file and pipe its contents to the look command. While we may not see any obvious errors in the output shown below, look is ignoring the pipe and using the default words file instead of the "nums" file. Piping just isn't welcome.

$ sort nums | look 5

It's no accident this look command gives the same result:

$ look 5

An alternative to look

While the grep command (an excellent choice for searching for lines containing any specified text) will generally look for the text that you specify anywhere in the file, it can be constrained to looking only for lines that begin with that text by using the ^ character.

$ grep ^Unix /usr/share/dict/words

Since grep isn't case-insensitive by default, add the -i (ignore case) when you want to select content regardless of case.

$ grep -i ^Unix /usr/share/dict/words

The grep command also doesn't care if the file content is sorted or not.


The look command can be handy, especially when you want to find words that begin with certain characters in the words file or other sorted files. It's somewhat tedious if you have to sort your file contents first and save the sorted contents to a second file before you conduct your search. In that case, grep is a much easier choice and will save you some time.

The look command's primary strength is finding all the words in the words file that begin with a particular string.

$ look easy | column
easy            easygoing       easy-humored    easy-natured    easy-spoken
easy-fitting    easygoingly     easylike        easy-paced
easy-flowing    easygoingness   easy-mannered   easy-rising
easy-going      easy-hearted    easy-minded     easy-running
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author