Carrying over from yesterday’s examination of the Ubuntu command line, today’s installment of 30 Days With Ubuntu Linux is dedicated to ‘man’ and ‘grep’. These commands wield significant power, and seem to be two of the most commonly used commands for the Ubuntu Linux command line.
I mentioned ‘grep’ in passing yesterday. In the Glossary of Terms at the end of Linux Nitty Gritty: Working at the Ubuntu Command-Line Prompt, Keir Thomas explains that ‘grep’ is short for “global regular expression print”. That isn’t very helpful, so Thomas goes on to describe it, “In Linux/Unix circles, the word ‘grep’ is synonymous with searching; to ‘grep a file’ is to search through it.”
Thomas’ book is a little short on the ‘grep’ syntax details, though, so I sought out another reference source–the man page. ‘Man’ is short for manual, and virtually every command and app in Linux has a man page. Simply open the Ubuntu Linux Terminal app, and type ‘man <name of command’ to get the details you seek.
Entering ‘man grep’ opens up the man page for the ‘grep’ command, along with tons of details about the syntax and proper usage of the command, as well as the diverse array of switches and filters that can be applied to the ‘grep’ command to customize the results.
I am big fan of search functions. That may sound silly, but I like the Applications and Files & Folders lenses in Unity for the same reason I like the Search field on the Windows 7 Start menu–the ability to conduct a universal search from a single query is awesome and saves me significant time. In Windows 7, I rarely–if ever–invest time seeking files or programs. I just enter what I am looking for in the search field and get straight to it.
With that in mind, I was anxious to check out the capabilities of ‘grep’. I opened a new Terminal session and navigates my way to the folder that stores my various documents I have written . I wanted to search all files for any instance of the word ‘Google’, so I typed ‘grep Google *’. Had I wanted to search a specific file, I would have typed the file name in place of the asterisk, and if the term I was searching for had two words–like Windows 7–it would be enclosed in quotations so that grep understand the whole string is one search term.
I hit Enter. Nada. Nothing. Just returned to the command line prompt. I tried a variety of combinations. I used the ‘-i- switch at the beginning to eliminate case sensitivity and search for either ‘Google’ or ‘google’. I tried narrowing the search to a single document that I am positive contains the word Google a few times. I even tried searching for other words. Still nothing.
Then it dawned on me–what if ‘grep’ can’t search for content within an ODT or DOCX file? I opened up one of the documents and saved a copy as a TXT (text) file. Lo and behold, when I ran the ‘grep’ search again it returned results listing out the lines and sentences where my search term was present.
Apparently, the power of ‘grep’ is limited to searching certain file types, and unfortunately it seems unable to search the files that I would most often be interested in searching using the power of a tool like ‘grep’. Based on the information in the ‘man’ page, it seems like a powerful and versatile command, but if it can’t search DOCX file content it is useless to me.
This may be a case where there is some other app or tool that I am not aware of, and if I just go find the right tool I would be able to search DOCX files. If any Linux gurus have any insight on either A) why my ‘grep’ search of DOCX and ODT files returned no results even though I know my search term is present in the files, or B) what tricks or tools are available that will enable ‘grep’ to search within other file types beyond TXT–like DOCX and ODT, I would appreciate the feedback.