[techtalk] Filename star

Samantha Jo Moore sjmoore at TheTahoeGroup.com
Thu Dec 30 10:23:58 EST 1999


Most of the issues with filenames with metacharacters have been answered.
However, the question

> How do you search for files, with meta characters as their names?

still remains.  You can do this with good ol' trusty find command.  There's
a trick however.  Let's look at the find command syntax:

   find starting_directory -name name_mask -print

   where   startting_directory   is the root of the directory tree where
                                 the search will begin.
           name_mask             is the filename or filename mask to be
                                 used for matches during the search.

So to find a file name "myfile" searching the entire filesystem you would
type the command

    find / -name myfile -print

If you want to find files that end in ".txt" for example you would need to
enter the command line:

    find / -name '*.txt' -print

Note that at this point we are enclosing the filename *.txt in single
quotes.
This is to prevent the shell from expanding the "*" into a list of filenames
before the command gets called.  If you happen to have a file named "*.txt"
and another one named "star.txt", both will be listed.  This shows us
that this command is not quite enough to find only those files with
metacharacters.  To do this we need to double-escape the metacharacters we
want to search for.  For example, to find a file named "*.txt" without
listing
the file "star.txt" we would issue the command:

    find / -name '\*.txt' -print

In a simmilar veign, to find all files with a metacharacter in their name
we would issue the command:

    find . -name '*\**' -print

The dot "." is a very special character which is used to separate filename
segments and cannot be associated with a "*".  Thus to find all files with
metacharacters in their names we need to issue two commands:

    find . -name '*\**' -print
    find . -name '.*\**' -print

Hope this helps understand the find command and metacharacters a little
better.

Samantha Jo Moore
sjmoore at thetahoegroup.com
http://www.thetahoegroup.com


************
techtalk at linuxchix.org   http://www.linuxchix.org




More information about the Techtalk mailing list