Use Look Command To Search For Lines From Linux Terminal


If you want to search for lines that start with a specific string / words, then look command will be useful. (There are other useful Linux command line tools like aspell that can check for typos which are easy to use and quick.)

Also unlike using grep to find instances of specific words, look is used only for finding lines that begin with a specified string or letters.

Using it is simple, open Terminal and type :

look inputstring filename

So for example, from a text file named plants.txt ,to get the lines that start with the word ‘We‘ (which is common in most sentences), the syntax is :

look We plants.txt

using the look command in Linux Mint

This will display all the instances of lines in that file which start with ‘We’.

The look command can also display lines that start with a specific letter instead of a word/string. The syntax is the same as before :

look P plants.txt

searching for lines starting with a letter using the look command

This will display all the lines that start with the letter ‘P‘ from the file plants.txt.

Searching in default dictionary:

If no file is specified, look will search for instances in the default dictionary which is at /usr/share/dict/words.

So, say to display a list of words from the user dictionary that start with the string ‘help‘, the command is :

look help

searching for all string instances in the default user dictionary when using the look command in Linux Mint

This will show all the possible words that start with that string.

To list words from the user dictionary that start with the string ‘computer‘ :

look computer

searching for all string instances using the default user dictionary

There are also a couple of parameters that can be used when using the look command.

Searching for alphanumeric strings :

The -d flag will force it to only look for alphanumeric strings.

look -d 3 Plants.txt

searching for alphanumeric string instances when using the look command

Ignoring uppercase and lowercase :

The look command is case sensitive by default. So, if you want look to search for strings or letters without being case sensitive, use the -f flag.

look -f the plants.txt

turning off case sensitivity when using  the look command

In the above example, lines beginning with all instances of the string (The and the) will be searched for and be displayed as the upper and lower case is ignored.

Simple and handy, do try it out.


Leave a Comment