Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Softpanorama Search

Unix find tutorial

Prev | Contents | Next

Part 3: Finding files using file name or path

Find development processes thru several generations and each left certain mark on its ability to use regular expressions. As of August 2009 it still cannot use Perl regular expression, though.

Name predicate and shell patterns

The first and probably the most popular option is -name shell_pattern It is true if the basename of the file (with the path removed) matches the shell pattern specified. Predicate -iname pattern  is the same thing only case insensitive. For example to find files with the extension .conf in /etc/ directory:

          find /etc -name '*.conf'

Note: To ignore a subtree you can use ‘-prune’ .

Searching fully qualified file name and path

Predicated  -path shell_pattern gives the ability to search path of the file while predicate -wholename pattern searches the fully qualified file name. Predicates  -ipath pattern and -iwholename pattern are similar but the match is case-insensitive.

In the context of the tests ‘-path’, ‘-wholename’, ‘-ipath’ and ‘-wholename’, a “ path” is consists of all the directories traversed from find's start point to the file being tested, followed by the base name of the file itself. These paths are often not absolute paths; for example

     cd /tmp
     mkdir -p foo/bar/baz
     find foo -path foo/bar -print foo/bar # first find command
     find foo -path /tmp/foo/bar -print # the second find command (prints nothing)
     find /tmp/foo -path /tmp/foo/bar -print /tmp/foo/bar # the third find command

Notice that due to search starting point foo the second find command prints nothing, even though /tmp/foo/bar exists.

Unlike file name expansion on the command line, a ‘*’ in the pattern will match both ‘/’ and leading dots in file names:

     $ find .  -path '*f'
     ./quux/bar/baz/f
     $ find .  -path '*/*config'
     ./quux/bar/baz/.config

Regular expressions

Type of regular expression can be specified with option -regextype. In best GNU traditions you need to select from several option only half of which are useful



Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Disclaimer:

Created: May 16, 1997; Last modified: August 25, 2009