×

Search anything:

look command in Ubuntu

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

We use the look command to display lines in a file starting with a specified string. In this article, we learn about the look command through various examples.

Table of contents.

  1. Introduction.
  2. Syntax.
  3. Usage.
  4. Summary.
  5. References.

Introduction.

In Linux, the look command is used to print lines in a file that start with a string. We pass this string to the look command.

This command uses linear search by default to search for the line starting with the string, however, if a file is not specified, it uses the /usr/share/dict/words file. This file is a dictionary of words that look command will use to perform its search.

In other Linux distributions such as Fedora, the look command uses binary search, this algorithm is much faster than linear search, that is, it is logarithmic as it breaks the search space into two per iteration while linear search takes linear time. A catch to binary search is that the file must be sorted, if not, we can create a copy of the file and sort it y executing the following command:

sort -f -d [ORIGINAL] -o [SORTED COPY]

Syntax.

We write look commands using the following syntax:

$ look [options] <string> [<file>...]

look1

Commonly used options with this command are;

  • -a, to use another dictionary file other than the default.
  • -d, to use a normal dictionary character set and order. This is the default if not file is specified.
  • -f, to ignore the case of characters.
  • -t, with this option, only strings up to and including the first occurrence of a character are compared.

Usage.

  1. A basic use of the look command is to check for the correct spelling of a word. We do this by passing the prefix of the word as an argument to the command:
$ look [string]

look2
From the output we have all words starting with the specified prefix(onomato) are printed. This command uses the default dictionary word located in /usr/share/dict/ directory.

  1. Apart from using the default file, we can specify our custom file and pass it as an argument. In this case, the look command will check spelling from the user-defined file:
$ look [string] [dictionary]

look4
In the above example, we create our mini dictionary of three words by redirecting the output of the previous command into a file. We pass this file as the dictionary in the above command.

  1. To ignore the case as we search for the spelling of a word, we use the -f option as shown:
$ look -f [string] [dictionary]

look5

  1. To find lines in a file that begin with a certain text we write:
$ look [string] [file]

For example to print all lines in dict1.txt file that start with a text Contr.. we write:

$ look Contra dict1.txt

look6

  1. When we apply the -d option to the look command, we ignore everything that is not an alphanumeric character or space.
$ look -d contra dict1.txt

look7
In the above example, we use the -f to ignore the case.

  1. We can specify a terminating character by using the -t option, this means that only characters in a string up to, including the first occurrence of the character are compared.
    For example:
$ look -ft y Contra dict1.txt

look8
In the above example, we are looking for lines starting with the text Contra, we are also looking for lines starting with a text Contrary, this is specified by the y character. In such a case lines with a string Contradiction, Contrary, Contract, etc will be printed.

As stated earlier, if no file is passed as an argument, this command uses the /usr/share/dict/words dictionary file.
look9

Summary.

In other Linux distributions such as Fedora, the look command uses binary search. In this article, we have learned about the look command according to Ubuntu distribution.

References.

For a comprehensive guide on the look command, we can check out its manual by executing the command $ man look.

look command in Ubuntu
Share this