How to Use the look Command on Linux

Share
  • July 22, 2019

Fatmawati Achmad Zaenuri/Shutterstock

The Linux look command races through a file and lists all the lines that begin 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 Command Behaves Differently

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

This article was checked using Ubuntu, Fedora, and Manjaro. look was bundled with each of those distributions, which was great. The problem was the behavior wasn’t the same across all three. The Ubuntu version was very different. According to the Ubuntu Manpages, the behavior should be the same.

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

If we take a look at the local Ubuntu man page, we see it clearly states their look uses a linear search. There is a command-line option to force it to use a binary search. Neither of the versions in the other distributions has an option to choose between search methods.

man look

"man look" in a terminal window.

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

Ubuntu look man page in a terminal window.

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

Linear Search versus Binary Search

Read the remaining 52 paragraphs

Source : How to Use the look Command on Linux