Linux Topic
   >  Why use the Command Line?
   >  Common Linux Commands
   >  Directory Structure
   >  Using Regular Expressions
   >  IO Redirection and Pipes


Regular Expressions

Using Regular Expressions

Regular Expressions are another powerful part of the Linux command line. Instead of specifying an exact match to a string or filename, they allow you to specify a pattern mask as to which characters will match and which should be ignored. One very blunt example of a regular expression, is the asterisk character (*) that most people are familiar with when listing files, for example

$ ls *
file1  file3     file.txt  myfile2     myFileFred1
file2  fileFred  myfile1   myfileFred  myfileFred.txt

This works fine in that we get all the files back (in fact the Linux default is "all files" so we do not gain anything by adding the extra "*"). If the directory is a large one, however, it can be difficult to see those we are interested in. Of course, we can use the asterisk in a slightly more refined way, for example:

$ ls myfile*
myfile1  myfile2  myfileFred  myfileFred1 myfileFred.txt

However, Linux gives us so much more control using different symbols to define what we want, for example (-and this is just a tiny subset):

*Any character or number of characters
[a-z]Any letter
[0-9]Any number
[]Any alphanumeric
[a-z0-9]Any alphanumeric
[aA]An "a" or an "A"
^The start of the string
$The end of the string

For example, let's list all the files that start with "myfile" but end in a number:

$ ls myfile[0-9]
myfile1  myfile2

Now, let's list all the files with the string "Fred" in their name:

$ ls *Fred*
fileFred  myfileFred  myfileFred1  myfileFred.txt

Ok, let's list all files that end in a number:

>$ ls *[0-9]
file1  file2  file3  myfile1  myfile2  myfileFred1

Hopefully, you can start to see the power of regular expressions: let's list all files that begin with "my" and end in an "e" followed by a number:

$ ls my*e[0-9]
myfile1  myfile2

However, regular expressions are not limited to filenames! You can use them in most commands that expect string input, such as "grep":

$  grep "Z[a-z][a-z][a-z]" myFile

This will match any 4 letter (!) word found in file myFile beginning with a capital "Z". It will not match "Zen", "Zoltan" or "zarg". If we wanted to pick up both upper and lower case "Z", we could write:

$  grep "[Zz][a-z][a-z][a-z]" myFile

For a great in-depth guide to regular expressions see (-or use your favourite search engine to look for "linux regular expressions").

References and Further Reading:

HomeSite IndexDesktop GuideServer GuideHints and TipsHardware CornerVideo SectionContact Us

 sitelock verified Firefox Download Button