“look” Command Usage Examples in Linux

«
»

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

look – display lines beginning with a given string

Description :

advertisement

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 :

advertisement
advertisement

-b
Use binary search
-d
Dictionary character set and order, i.e. only alphanumeric characters are compared.
-f
Ignore the case of alphabetic characters.
-a
Use the alternate dictionary /usr/share/dict/web2
-t
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.

advertisement
$ look rendez
rendezvous
rendezvous's
rendezvoused
rendezvouses
rendezvousing

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

advertisement

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

$ look excl myfile
exclaimed
exclamation

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

$ look -f excl myfile
exclaimed
exclamation
EXCLAMATORY

4. Use Binary Search

advertisement
$ look -bf excl myfile
exclaimed
exclamation
EXCLAMATORY

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.

advertisement
advertisement
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 & technical discussions at Telegram SanfoundryClasses.