look Command in Linux with Examples

This tutorial explains Linux “look” command, options and its usage with examples.

look – display lines beginning with a given string

Description :

The look utility displays any lines in file which contain string as a prefix. As look performs a binary search, the lines in file must be sorted (where sort got the same options -d and/or -f that look is invoked with).
If file is not specified, the file /usr/share/dict/words is used, only alphanumeric characters are compared and the case of alphabetic characters is ignored.

Usage :

look [-dfab ] [-t termchar ] string [file ]

Options :


Use binary search
Dictionary character set and order, i.e. only alphanumeric characters are compared.
Ignore the case of alphabetic characters.
Use the alternate dictionary /usr/share/dict/web2
Specify a string termination character, i.e. only the characters in string up to and including the first occurrence of termchar are compared.

Examples :

1. A Basic Example

Suppose you want to verify the spelling of the word rendezvous. You can do it easily using look command.

$ look rendez

2. Searching for a string in a file

For example, if I want to display all the header files that are included in a c file, I’d use the look command in the following way :

$ look "#include" test.c 
#include <stdio.h>
#include <stdlib.h>

3. Ignoring case while searching


When you want to know the spelling of a word which has the prefix ‘excl’.

$ look excl myfile

This displays the word that starts with the given prefix (case sensitive) from file myfile.

$ look -f excl myfile

4. Use Binary Search

$ look -bf excl myfile

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.