How to use the look command in Linux.

The Linux look command walks through a file and lists all the lines that start with a particular word or phrase. But beware! It behaves differently on different Linux distributions. This tutorial will show you how to use it.

Ubuntu’s Look command behaves differently

For a simple, yet useful command, look certainly gave me a shock when I was researching this article. There were two problems: compatibility and documentation.

How to use the look command in Linux

This article has been verified with Ubuntu, Fedora and Manjaro. The skin was included with each of those distros, which was great. The problem was that the behavior was not the same in all three. The Ubuntu version was very different. According to the , the behavior should be the same.

I eventually figured it out. look traditionally uses a , while Ubuntu uses a . The online Ubuntu man pages for Bionic Beaver (18.04), Cosmic Cuttlefish (18.10) and Disco Dingo (19.04) say that the Ubuntu version uses a binary search, which is not the case.

How to use the look command in Linux

If we take a look at the local Ubuntu man page, we see that it clearly indicates that its appearance uses linear search. There is a command line option to force it to use a binary search. None of the versions of the other distributions have an option to choose between the search methods.

man look

How to use the look command in Linux

Scrolling down through the man page, we see the section that describes this version of look using linear instead of binary search.

The moral of the story is to check your local man pages first.

How to use the look command in Linux

Linear Search vs. Binary Search

The binary search method is faster and more efficient than a linear search. Working with large files makes this very apparent. The disadvantage of binary search is that your file must be sorted. If you don’t want to order your file, order a copy and then use it with appearance.

We will demonstrate this elsewhere in this article. Just keep in mind that on Fedora, Manjaro, and hopefully most of the rest of the Linux world, you’ll need to create a tidy copy of your file and work with that.

installing words

look can work with any text file you choose, or it can work with the local “words” dictionary file.

How to use the look command in Linux

In Manjaro you need to install the “words” file. Use this command:

sudo pacman -Syu words

using gaze

For this article, we will be working with a text file of the poem “The Jumblies”.

How to use the look command in Linux

Let’s see its content with this command:


Here is the first part of the poem. Please note that we are using Ubuntu, so the file remains unsorted. For Fedora and Manjaro, we’d work with a neat copy of the file, which we’ll cover later in this article.

If we search for lines that begin with the word “They”, we will discover something of what the Jumblies did.

How to use the look command in Linux

look They the-jumblies.txt

look responds by listing these lines:

Ignore character case

To make the appearance ignore case, use the -f (ignore case) option. We have used “they” as the search word again, but this time it is in lower case.

How to use the look command in Linux

look -f they the-jumblies.txt

This time, the results include an extra line.

The line beginning with “THEM” was omitted from the last result set because it is all caps and does not match our search term, “Them”.

Ignoring the case allows Look to include it in the results.

Use appearance with a sorted file

If your Linux distribution has a skin version that follows the traditional behavior of using a binary search, you should either sort your file or work with a sorted copy of it.

Let’s repeat the command to search for “Them”, but this time in Manjaro.

As you can see, no results were returned. But we do know that there are lines in the poem that begin with the word “They.”

Let’s make a neat copy of the file. If you are going to use the -f (ignore case) or -d (alphanumeric characters and spaces only) options with appearance, you must use them when sorting the file.

How to use the look command in Linux

The -o (output) option allows you to specify the name of the file to which the sorted lines should be added. In this example, it is “sorted.txt”.

sort -f -d the-jumblies.txt -o sorted.txt

Let’s use search in the sorted.txt file, and then use the -f and -d options.

Now, we get the results we expected.

How to use the look command in Linux

Consider only spaces and alphanumerics

To make it appear to ignore anything other than a or a space, use the -d (alphanumeric) option.

Let’s see if there are any words that begin with “Oh.”

look -f oh the-jumblies.txt

No results are returned per look.

Let’s try again and tell look to ignore anything but alphanumeric characters and spaces. That means characters and symbols, like punctuation, will be ignored.

look -f -d oh the-jumblies.txt

This time, we get a result. We didn’t find this line earlier because the quotation marks and exclamation mark were confusing the search.

Specify the termination character

You can tell look to use a specific character as the final character. Usually, spaces and the end of lines are used as the final character.

The -t (terminate character) option allows us to specify the character we would like to use. In this example, we will use the apostrophe character. We need to quote it with a backslash so the eye knows we’re not opening a chain.

We are also citing the search term because it includes a space. We are looking for two words.

look -f -t ‘ “they call” the-jumblies.txt

The results match the search term, terminated by the apostrophe we use as the final character.

Use Fileless Look

If you don’t provide a file name on the command line, search for uses .

The command:

gives these results:

These are all the words in the file that start with the word “circle”.

look no further

That’s all there is to looking for.

It’s pretty easy once you know that different behaviors exist in different Linux distributions, and you’ve hit rock bottom whether your version uses binary or linear search.