Matching file names with regular expressions

You can use the following metacharacters within any shell to create regular expressions that match file names.

   ?       matches any single character
   *       matches any number of any characters
   [nnn]   matches any of the enclosed characters
   [!nnn]  matches any character that is not enclosed
   [n-n]   matches any character in this range

 

Examples of using regular expressions for file names

To list all files with a particular extension:

   ls *.fm

This will list all the files in the current directory that have the extension .fm.


To remove a range of files:

   rm prog.?

This removes all the files in the current directory with a single character extension to their name. Files foo.c and foo.o would be removed as would file foo.3.


To copy all the files containing the same pattern:

   cp [pP]art[0-9] ../book

This copies all files whose name contains a pattern that starts with either p or P followed by the letters art and ending in any number from 0 through to 9.

So the file part1 would be copied to the parent directory book as would the file Part7.

 

 

Using regular expressions with the grep command

The following characters can be used to create regular expressions for searching on patterns with grep.

Always quote the regular expression. This prevents the shell from interpreting the special characters before it is passed to the grep command.

c       any non-special character represents itself
\\c     turns off the meaning of any special character
^       beginning of a line
$       end of a line
.       matches any single character except a newline
[...]   matches any of the enclosed characters
[^...]  matches any character that is not enclosed
[n-n]   matches any character in this range
*       matches any number of the preceding character

 

Examples of using regular expressions

To search using enclosed characters:

   grep -n '[dD]on't' tasks

This uses a regular expression to find and display each line in the file tasks that contains the pattern don't or Don't. The line number for each line is also displayed.


To turn off the meaning of a special character:

   grep '^\\$' money

This lists all the lines in the file money that begin with a $ (dollar) sign. This character is preceded by a \\(backslash) to remove its meaning as a special character.


To search from the beginning of a line:

   ls -l | grep '^d........x'

This lists all the directories in the current directory for which other users have execute permission.


To search for lines ending in a particular pattern:

   ls -l | grep '[^.xdh]$'
This lists all the files and directories in the current directory which do not end in .xdh.

 

Combining other commands with the find command

You can also use the find command to find a file and then carry out a command on that file. To do this use the command:

   find pathname -name filename -print -exec command {}\;

To be prompted for confirmation before the command is executed use the command:

   find pathname -name filename -print -ok command {}\;

The command must be followed by a \ (backslash) and a ; (semi-colon). The {} (pair of braces) substitute the pathname of the file that is found as an argument to the command.

Examples of executing commands with the find command

To remove several files:

   find . -name 'mtg_*' -print -exec rm {} \;

This will search for and remove all files starting with the expression mtg_ from the current directory and its subdirectories.


To find and remove specific files:

   find . -name '*.tmp' -ctime +30 -print -exec rm {} \;

This removes all files in the current directory and its subdirectories with the filename extension .tmp which have not been changed within the last 30 days.


 

 

To find and remove files interactively; 

 find ~/Docs -name '*.ps' -print -ok rm {} \;

This searches for files with the extension .ps starting in the subdirectory "Docs" in the user's home directory. Each time a file is found that matches this expression, the user is prompted to confirm that they want to remove it.

Entering y removes the file.

 

Examples of using the while and until statements

To wait for someone to logout:

   #!/bin/sh
   while who |grep -s $1 >/dev/null
   do
     sleep 60
   done
   echo "$1 has logged out"

This script checks to see if the username given as an argument to the script is logged on. While they are, the script waits for 60 seconds before checking again. When it is found that the user is no longer logged on a message that they have logged out is displayed.


To declare when a file has been created:

   #!/bin/sh
   until test -f $FILE
   do
     sleep 60
   done
   echo "$FILE now exists"

This tests every 60 seconds until the filename represented by the variable $FILE exists. A message is then displayed.


 

 

To watch for someone to log in:

   #!/bin/sh
   # make sure we pick up the correct commands
   PATH=/bin:/usr/bin
   # remember $# is number of positional arguments
   case $# in
   1) ;;
   *) echo 'usage: watchfor username' ; exit 1
   esac
   until who | grep -s "$1" >/dev/null
   do
     sleep 60
   done
   echo "$1 has logged in"

If more than one username is given to the command watchfor the message

   usage: watchfor username

is displayed and the command fails.

 

Example of using the break and continue statements

To prompt for commands to run:

   #!/bin/sh
   while echo "Please enter command"
   read response
   do
     case "$response" in
     'done') break           # no more commands
                       ;;
     "")     continue        # null command
                       ;;
     *)      eval $response  # do the command
                       ;;
     esac
   done

This prompts the user to enter a command. While they enter a command or null string the script continues to run. To stop the command the user enters done at the prompt.