The Linux command
look searches a file and lists all lines starting with a particular word or phrase. But watch out! It behaves differently on different Linux distributions. This tutorial will show you how to use it.
Ubuntu's Look Behaves Differently
For a simple but useful command
Look certainly provided an overview when I looked at this article. There were two problems: compatibility and documentation.
This article has been reviewed with Ubuntu, Fedora and Manjaro.
look was bundled with each of these distributions, which was great. The problem was that the behavior was not the same for all three. The Ubuntu version was very different. According to the Ubuntu man pages, the behavior should be the same.
At some point I found out.
look traditionally uses a binary search, while Ubuntu
look uses a linear search. The online Ubuntu man pages for Bionic Beaver (1
appearanceuses a linear search. There is a command line option to force the use of a binary search. None of the versions in the other distributions have the option to choose between search methods.
If you scroll down the man page, you will see the section that describes this version of
look using a linear instead of a binary search.
The moral of the story is to check the local manpages 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 clear. The disadvantage of binary search is that your file needs to be sorted. If you do not want to sort your file, sort a copy of it and then use it with
This is demonstrated elsewhere in this article. Note, however, that under Fedora, Manjaro, and most of the rest of the Linux world, you'll need to make a sorted copy of your file and work with it.
look works with any text file you choose or the words dictionary dictionary file.
In Manjaro you need to install the words file. Use this command:
sudo pacman -Syu words
In this article, we will work with a text file of the poem "The Jumblies" by Edward Lear.
Let's look at the content with this command:  less the-jumblies.txt
Here is the first part of the poem. Note that we use Ubuntu, so the file remains unsorted. For Fedora and Manjaro we would work with a sorted copy of the file, which we'll cover later in this article.
If we search for lines with the word "They," we'll find out what the Jumblies have done.
look They the-jumblies.txt
look prints the following lines:
Ignore uppercase and lowercase
-f (ignore case). We used "her" as the search term again, but this time in lowercase.
Look in a terminal window for "-f the-jumblies.txt"
This time the results contain an extra line.
The line beginning with "THEY" was overlooked in the last result set because it is a total of uppercase letters and does not match our search term "They."
By ignoring case
look can be included in the results.
Using look with a sorted file
If your Linux distribution has a version of
look that follows the traditional behavior of a binary search. You must either sort your file or work with a sorted copy of it.
Repeat the command to look for "They," but this time on Manjaro.
As you can see, no results were returned, but we know that the poem contains lines that start with the word "you".
Let's create a sorted copy of the file if you use the options
-f (case insensitive) or
-d (only alphanumeric characters and spaces) with
look you must use them when you sort the file.
-o option (output) you can specify the name of the file to which the sorted rows are to be added, in this example it is "sorted.txt".
sort -f -d the-confusion.txt -o sorted.txt
look for the sorted.txt file, and then use the options
Now we get the expected results.
Include only spaces and alphanumeric characters.
Until Look ignores anything that is not an alphanumeric character or space, use the option
Let's see if there are any words that start with "Oh".  Look -f oh the-jumblies.txt
No results are returned by
Let's try again, and have look, ignore characters other than alphanumeric characters and spaces. This means that characters and symbols, eg. Punctuation marks are ignored.
look -f -d oh the-jumblies.txt
This time we get a result. We did not find this line before because the quotes and exclamation point confused the search.
Specify the terminating sign
You can say
looks like to use a particular character as a terminator. Normally, spaces and end-of-line are used as terminators.
With the option
-t (terminator) we can specify the character we want to use. This example uses the apostrophe. We have to quote it with a backslash so
Look knows we are not opening a string.
We also quote the search term because it contains a space. We are looking for two words.
look -f -t "you call" the-jumblies.txt.
The results match the search term completed with the apostrophe we used as the terminator.
Use look without file
If you do not specify Do not specify a file name on the command line. Look uses the word file.
returns these results:
These are all words in the file that start with the word "circle".
Do not keep looking
That's all there is to do
It's pretty easy to know that different Linux distributions behave differently, and you've found out if your version uses a binary or linear search.