Credit: marvod / Getty Images The look command on Linux can be handy for selecting particular lines from text files with sorted contents. Let’s look into how it can be used and where you might run into some problems. Case sensitivity If you type a command such as “look unix”, you should see this: $ look unix UNIX Unix unix Notice that, because no file was specified in the command shown, look reverts to using the words file on the system (probably /usr/share/dict/words or whatever that points to). Also notice that it finds the three lines in the file even though the argument for the command has only lowercase characters. The command is case-insensitive when you don’t provide a file name and instead allow it to default to the words file. For a word like “snow”, you might get a couple hundred responses. Here’s just the beginning of what you should see: $ look snow | column | head -5 Snow snowdrifts snow-nodding snow snow-driven snow-on-the-mountain Snowball snowdrop snowpack snowball snow-dropping snowpacks snowballed snowdrops snow-plough Piping the output to column provides a multi-column display. In the output above, we use the head command to show only the first five lines. Specifying the file to use To pull lines from an arbitrary text file, add its name to the look command like this: $ look 2 myfile 2) I don't like my file In that command, look selects lines from the file that begin with “2”. To be a little more specific, you could do this: $ look "2)" myfile 2) I don't like my file The quotes around the search term are needed in this case since the parenthesis might otherwise be interpreted as a delimiter. If you are looking for content in some file other than the words file, you will also likely need to include a -f (or –ignore-case) option to view contents regardless of case. As mentioned earlier, while the look command is case-insensitive when using the words file, this isn’t so when specifying some other file, so you may need the option to make it do what you want. In the example below, you can see how much difference this can make. $ look k shopping_list kiwifruit $ look -f k shopping_list kiwifruit Klondike bars This can be important to remember. After all, you don’t want to risk forgetting those Klondike bars! The look command expects files to be sorted It’s important to remember that the look command expects files to be sorted alphanumerically. Let’s say we were trying to extract content from a file with this content: $ cat nums 1 2 3 4 6 5 If we ask look to select the line with the “4”, all is well. $ look 4 nums 4 If we ask look to select the line beginning with a “5”, on the other hand, it doesn’t comply because the 5 appears following a 6. In other words, it may stop searching when it encounters an error in the sort order. $ look 5 nums $ You also can't simply sort a file and pipe its contents to the look command. While we may not see any obvious errors in the output shown below, look is ignoring the pipe and using the default words file instead of the "nums" file. Piping just isn't welcome. $ sort nums | look 5 5-point 5-T 5th It's no accident this look command gives the same result: $ look 5 5-point 5-T 5th An alternative to look While the grep command (an excellent choice for searching for lines containing any specified text) will generally look for the text that you specify anywhere in the file, it can be constrained to looking only for lines that begin with that text by using the ^ character. $ grep ^Unix /usr/share/dict/words Unix Since grep isn't case-insensitive by default, add the -i (ignore case) when you want to select content regardless of case. $ grep -i ^Unix /usr/share/dict/words UNIX Unix unix The grep command also doesn't care if the file content is sorted or not. Wrap-Up The look command can be handy, especially when you want to find words that begin with certain characters in the words file or other sorted files. It's somewhat tedious if you have to sort your file contents first and save the sorted contents to a second file before you conduct your search. In that case, grep is a much easier choice and will save you some time. The look command's primary strength is finding all the words in the words file that begin with a particular string. $ look easy | column easy easygoing easy-humored easy-natured easy-spoken easy-fitting easygoingly easylike easy-paced easy-flowing easygoingness easy-mannered easy-rising easy-going easy-hearted easy-minded easy-running Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe