ArchLUG Linux By Example - www.archlug.org

Download the RSS XML Feed for this site

Subscribe to this channel with Amphetadesk

Subscribe to this channel with RadioUserland

LBE- grep


Q: What's the Linux equivalent for the 'find.exe' program in DOS/Windows?

A: The linux upgrade for that function is a program called 'grep'.

The funny name comes from the syntax used in the earliest text editor for *nix, simply named 'ex' (for 'external editor').

The syntax of the command to search for text in that editor was to specify the range of lines in which you were searching (the 'g' is a shorthand for "global", meaning from the first to the last line), followed by a "regular expression" (where the 're' comes from), which is a pattern for what you're looking for, and finished off with an editor command for what you want to do when it finds a line that matches the "regular expression", in this case, we just want it to 'print' the matching line (and hence the trailing 'p').

Put that all together and you get 'g/re/p'. It was so useful, it became a standalone command, and the regular expressions are quite, well, expressive.

REs, or RegExes, are a special syntax to describe text that matches a pattern, similar to the way certain characters match filenames in the shell. However, the shell filename expansion syntax is very different than regular expression syntax.

In the shell, filename that start with 'A', 'B', or 'c', and end with '.txt' would be specified as '[ABc]*.txt' on a command line.

In a text editor, or for the 'grep' command, the same match would be expressed as:

^[ABc].*\.txt$

where we are indicating the line must consist only of such 'words'.

The '^' matches the "beginning" of the line, the '$' matches the "end" of the line, and the '.' matches "any" character. The '*' matches 0 or more of whatever the previous character was, in this case "any" character. Finally, because we want to actually match the '.' character, we have to mark it so that it no longer has its special meaning of the "any" character. To do that you put a '\' (backslash) in front of any character that would normally be special to make it no longer have its special, magical properties.

And that, in a nutshell, is 'grep'.

More examples

If you are using the bash shell, which you most likely are if you don't know what that means, then you will have a prompt where you can execute textual commands. For example, the scp program is pretty darn cool, but hard to remember how you used it last. Whenever you type a command it is stored in your history file, so you can easily execute the command again by hitting the up-arrow. In fact, older commands are stored, too. Let's say that the last time you used the scp command was a while back... you could easily find how you used it by saying:

history | grep scp

Which means run the history program (see LBE- history) then pipe its output into grep, and only look for lines containing scp. You will then see a list of all the scp commands you have recently executed.

Note: grep isn't really an equivalent of Windows find.exe. grep will not search file names, unless you pipe the list in from some other command (like ls). See updatedb and locate for an alternative to Windows find.exe.


Valid XHTML 1.0! Valid CSS!