How to Use the look Command on Linux

Linux terminal running on an Ubuntu themed desktop environment.Fatmawati Achmad Zaenuri / Shutterstock

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

The look of Ubuntu behaves differently

For a simple but useful order, look at me certainly gave the flyby when I was researching this article. There were two problems: compatibility and documentation.

This article has been verified with Ubuntu, Fedora and Manjaro. look was delivered with each of these distributions, which was great. The problem was that the behavior was not the same in all three cases. The Ubuntu version was very different. According to Ubuntu man pages, the behavior should be the same.

I finally understood. look traditionally uses a binary search, while the Ubuntu look uses a linear search. The Ubuntu online man pages for Bionic Beaver (18.04), Cosmic Cuttlefish (18.10) and Disco Dingo (19.04) all indicate that the Ubuntu version uses a binary search, which is not the case.

If we look at the local Ubuntu man page, we find that it clearly indicates that their appearance uses a 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 the possibility to choose between the search methods.

man's look

While browsing the manual page, we see the section that describes this version of look using a linear rather than binary search.

Ubuntu search man page in a terminal window.

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

Linear search versus binary search

The binary search method is faster and more efficient than a linear search. Working with large files makes this very obvious. The disadvantage of the binary search is that your file needs to be sorted. If you do not want to sort your file, sort a copy, then use it with look.

We will demonstrate it elsewhere in this article. Just be aware that in Fedora, Manjaro and, I hope, in the rest of the Linux world, you will need to create a sorted copy of your file and work with it.

Word installation

look can work with any text file of your choice, or with the local word dictionary file.

On Manjaro, you need to install the "words" file. Use this command:

sudo pacman -syu words

Using look

For this article, we will work with a text file of Edward Lear poem "The Jumblies".

Let's look at its contents with this command:

less the-jumblies.txt

Here is the first part of the poem. Note that we use Ubuntu, so the file is not sorted yet. For Fedora and Manjaro, we would use a sorted copy of the file, which we will discuss later in this article.

If we look for lines that begin with the word "They", we'll discover some of what the Jumblies did.

watch them the-jumblies.txt

look responds by listing these lines:

Ignore the character case

To ignore the differences between upper and lower case, use the -f option (ignore case). We still used "they" as a search word, but this time it is lowercase.

look at them-the-jumblies.txt

Look -fils the-jumblies.txt in a terminal window.

This time, the results include an additional line.

The line starting with "THEY" was omitted from the last set of results because it is capitalized and does not match our search term, "They".

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

Use look with a sorted file

If your Linux distribution has a look version that follows the traditional behavior of using a binary search, you must sort your file or use a copy of it.

Repeat the command to search for "They", but this time on Manjaro.

look at the-jumblies.txt in a terminal window.

As you can see, no results have been returned. But we know that there are lines in the poem that start with the word "They".

Let's make a sorted copy of the file. If you want to use the -f (ignore case) or -d (alphanumeric characters and spaces only) options, you must use them when sorting the file.

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 the file "sortis.txt".

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

sort -f -d the-jumblies.txt -o sorted.txt in a terminal window.

Let's look at the sorted.txt file, and then use the -f and -d options.

sort -f -d the-jumblies.txt -o sorted.txt in a terminal window.

We are now achieving the expected results.

Consider only spaces and alphanumeric

Pretend to ignore everything that is not a Alphanumeric character or a space, use the -d option (alphanumeric).

Let's see if there are words that start with "Oh".

look at -f oh the-jumblies.txt

Look at -f oh the-jumblies.txt in a terminal window.

No results were returned by look.

Let's try again and say look to ignore anything other than alphanumeric characters and spaces. This means that characters and symbols, such as punctuation, will be ignored.

look at -f -d oh the-jumblies.txt

Look at -f -d oh the-jumblies.txt in a terminal window.

This time we get a result. We did not find this line before because the quotation marks and the exclamation point disrupted the search.

Specify the end character

You can look to use a specific character as an end character. Usually, spaces and end of lines are used as end characters.

The -t (end-character) option allows us to specify the character we want to use. In this example, we will use the apostrophe character. We must quote it with a backslash so that we know that we do not open a chain.

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

look at -f "" they call "the-jumblies.txt

look at -f -t  "they call" the-jumblies.txt in a terminal window.

The results match the search term, ending with the quote we used as the final character.

Use look without file

If you do not provide a file name on the command line, look uses the word file.

The command:

gives these results:

These are all words in the file that begin with the word "circle".

look no further

That's all there is to watch.

It's quite easy once you know that there are different behaviors between different Linux distributions and that you determine if your version uses a binary or linear search.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.