How to use the look command on Linux

The Linux look The command races through a file and lists all lines that begin with a specific 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 command behaves differently

For a simple but useful command, look certainly gave me the overview when researching this article. There were two issues: compatibility and documentation.

This article was verified using Ubuntu, Fedora, and Manjaro. look got bundled with each of these distros which was great. The problem was, the behavior wasn’t the same for all three. The Ubuntu version was very different. According to Ubuntu man pages, the behavior should be the same.

I finally found out. look traditionally used a binary searchwhile Ubuntu look uses a linear search. Ubuntu’s online man pages for Bionic Beaver (04/18), Cosmic Cuttlefish (10/18), and Disco Dingo (04/19) all say the Ubuntu version uses binary search, which it doesn’t.

If we take a look at the local Ubuntu man page, we see that they are clearly theirs look uses a linear search. There is a command line option to force a binary search. None of the versions in the other distributions offer an option to choose between search methods.

man look


If we scroll down through the man page we can see the section that describes this version of the. describes look Use a linear search instead of a binary one.

The moral of the story is to check your local man 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 clear. The disadvantage of binary search is that your file needs to be sorted. If you don’t want to sort your file, sort a copy of it and then use that with you look.

We’ll demonstrate this elsewhere in this article. Just be aware that on Fedora, Manjaro, and I expect the rest of the Linux world to need to make an sorted copy of your file and work with it.

Install words

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

You need to install the words file on Manjaro. Use this command:

sudo pacman -Syu words

Use look

For this article we are working with a text file of the Edward Lear Poem “The Mess”.

Let’s look at the contents with this command:

less the-jumblies.txt


Here is the first part of the poem. Note that we are using Ubuntu, so the file will be left unsorted. To the Fedora and Manjaro we would be working with a sorted copy of the file that we will cover later in this article.

If we search for lines that begin with the word “you,” we will learn what the Jumblies did.

look They the-jumblies.txt

look answers with the listing of these lines:

Ignore upper / lower case

close look If you ignore the differences between upper and lower case, use the -f (Ignore case) option. We used “she” as the search term again, but this time in lower case.

look -f they the-jumblies.txt

This time the results contain an extra line.

The line that begins with “THEY” was overlooked in the last set of results because it is only written in capital letters and does not match our search term “They”.

It is allowed to ignore upper and lower case look to include it in the results.

Use Look with a sorted file

If your Linux distribution has a version of the look which follows traditional binary search behavior, you either need to sort your file or work with a sorted copy of it.


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

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

Let’s make an sorted copy of the file. If you use that -f (Ignore case) or -d (alphanumeric characters and spaces only) options with look, you need to use this when sorting the file.

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

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

Let’s use look in the sorted.txt file and then use the -f and -d Options.

Now we get the expected results.

Only consider spaces and alphanumeric characters

To attract the eye, ignore anything that isn’t alphanumeric character or a space, use the -d (alphanumeric) option.


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

look -f oh the-jumblies.txt

No results are returned by look.

Let’s try again and tell look to ignore everything except alphanumeric characters and spaces. This means that characters and symbols such as punctuation marks are ignored.

look -f -d oh the-jumblies.txt

This time we get a result. We didn’t find this line before because the quotation marks and exclamation mark confused the search.

Specify the terminator

You can say look use a specific character as a terminator. As a rule, spaces and the end of a line are used as terminators.

the -t (End character) allows us to specify the character we want to use. In this example, we use the apostrophe character. We have to quote it with a backslash so that look knows we’re not opening a string.


We also quote the search term because it contains a space. We’re looking for two words.

look -f -t ' "they call" the-jumblies.txt

The results match the search term, terminated by the apostrophe we used as the terminator.

Use look without a file

If you don’t specify a filename on the command line, use the word file.

The command:

gives these results:

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

look no further

That’s all there is look.

It’s pretty easy, knowing that different Linux distributions have different behaviors, and you’ve figured out whether your version uses binary or linear search.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts