Linux look Command Tutorial for Beginners (with Examples)
Although the Linux find command does a fabulous job for searching on the command line, there may be situations where a dedicated tool may be more convinient. One such case is to find lines in a file that start with a particular word. There exists a command - dubbed look - that does this for you.
In this tutorial, we will discuss this command using some easy to understand examples. But before we do that, it's worth mentioning that all examples in the article have been tested on an Ubuntu 18.04 LTS machine.
Linux look command
The look command in Linux displays lines beginning with a given string. Following is its syntax:
look [-bdf] [-t termchar] string [file ...]
And here's what the man page says about the tool:
The look utility displays any lines in file which contain string as a
If file is not specified, the file /usr/share/dict/words is used, only
alphanumeric characters are compared and the case of alphabetic charac?
ters is ignored.
Following are some Q&A-styled examples that should give you a good idea on how the look command works.
Q1. How to use look command?
Simple, just provide the word you want to search and the file in which you want to search as inputs to the look command.
Here's an example:
look The test.txt
The above command searches for lines beginning with 'The' in the file 'test.txt'.
For reference, here's the test.txt file:
And here's the output produced:
Q2. How to narrow down search within given string?
The look command also allows you to narrow down your search by allowing you to provide a terminating character. Here's how the man page explains the -t option:
-t, --terminate termchar
Specify a string termination character, i.e., only the characters
in string up to and including the first occurrence of termchar
To test this option, I updated test.txt with a few more lines. Take a look:
And then executed the following command:
look -t n Fund test.txt
So we are basically asking look to search for lines beginning with the word 'Fund' but also include those that start with 'Fun'. And here's the output:
Q3. Is look search case sensitive?
Yes, it is. For example, if you run the following command (which is same as the one we used in last Q&A, save for the lower case of the first character in the word 'fund'):
look -t n fund test.txt
No output will be produced.
However, you can force look to ignore case by using the -f option.
Q4. What else look can be used for?
You can use the look command to search for all words beginning with a set of characters. For example, to see what all words start with the character sequence 'love', just use the look command in the following way:
Here's a sample output:
So all in all, look is a useful little command line utility that you should be at least aware of, for you never know when it may turn out to be a saviour for you. It doesn't offer many command line options, and majority we've already discussed here. To learn more about look, head to its man page.