
look Command in Linux
Linux provides a variety of commands for handling and searching text. These commands make it easy to find and manipulate information in files. The look command is one of them that helps us locate lines that start with a specified word or string. This command lets users quickly search large files by displaying entries that match the given prefix.
In this tutorial, we'll explain the look command and its several use cases in Linux.
Table of Contents
Here is a comprehensive guide to the options available with the look command −
- What is look Command in Linux?
- Syntax of look Command
- look Command Options
- look Command Manual Page
- look Command Help Page
- Checking Version of look Command
- Examples of look Command in Linux
What is look Command in Linux?
look is a command line utility in Linux that shows lines from a file or standard input that begin with a specified string. It's especially useful for finding words in files that begin with a certain prefix, making it handy for things like finding dictionary words that match the start of what you're searching for.
Syntax of look Command
To use the look command in Linux, you must follow the syntax provided below −
look [OPTION]... STRING [FILE]
Here, "STRING" represents a string that you want to search for in the target "FILE". If you omit the file, the look command searches through the system's dictionary.
look Command Options
The look command supports several options to customize the search process. The most common options of the look command are discussed in the following table −
Options | Description |
---|---|
-a, --alternative | It uses an alternative dictionary file. |
-d, -alphanum | It searches using only letters and numbers. |
-f, --ignore-case | It searches for a string regardless of the letter case. |
-h, --help | It shows the help page for the look command. |
-t, --terminate | It limits the search by setting an end/termination character. |
-version | It shows the version of the look command. |
look Command Manual Page
Let's run the command below to access the look command manual page −
man look

look Command Help Page
The help of any Linux command contains detailed information about the command's usage, options, and examples. We can use the -h option with the look command to check the help page of the Linux look command −
look -h

Checking Version of look Command
We can check the version information of the look command, by executing the following command −
look -V

Examples of look Command in Linux
In this section, we'll demonstrate how to use look command with or without any option −
Finding Strings Starting with a Specific Word
Let's use the look command to search strings started with a specific word. In this example, we will find the strings in the system dictionary −
look man
This command searches lines in the system dictionary that start with the word "man" and displays them −

Finding Strings in a Specific File
To search for a specific string in a file using the look command, we first need to ensure that the input file is sorted. This is because the look command is designed to display lines in a sorted file that begin with a given string. Therefore, first, we can use the sort command to sort the targeted file −
sort myDictionary.txt -o sortedDictionary.txt
This command will sort the specified file alphabetically −

Now we can use the look command to find the desired strings in the sorted file −
look "hello" sortedDictionary.txt
This command will return all those strings that start with the word hello −

Finding Strings in a File Ignoring Letter Case
By default, the look command in Linux is case-insensitive, so it doesn't take letter case into account when displaying results. However, you can use the -f option to ignore the case of alphabetic characters, allowing you to search for strings without regard to letter case.
Sort the file first, and then use the following command to search for all entries in the specified file that contain the word "hello" −
look -f "hello" sortedDictionary.txt
This command ignores the letter case and displays any lines that start with "hello" from the sorted file −

Specifying String Termination with the -t Option
You can use the -t option with the look command to specify a character that marks the end of a string. This means the command will only compare the characters in the string up to and including the first occurrence of that character −
look -t h hello sortedDictionary.txt
This command searches for lines in a sorted file that begin with a string, "hello". The -t h option sets h as the "tab stop", causing the command to ignore everything before the first h in each line. The file being searched must be sorted, and in this case, it's named sortedDictionary −

Searching for Prefixes in a Dictionary File
We can execute the look command with the -d option to search for lines in a specific file that begin with the specified words and treat the search like a strict dictionary check. Here's a simple example −
look -d ap sortedDictionary.txt
This command searches for lines that start with ap word in the sortedDictionary.txt file. The -d option ensures that the search is strict, meaning it will only match entries that exactly begin with ap −

This sums up the look command and its usage in Linux.
Conclusion
The look command in Linux is a useful tool that enables users to efficiently search for lines starting with a specified string in files or the system dictionary. Its ability to handle large files and provide quick results makes it a valuable resource for text manipulation and searching tasks.
In this tutorial, we explored the look command's syntax, common options, and various practical examples, which demonstrate its versatility in locating strings that meet specific criteria.