RSS Feed Subscribe to RSS Feed



“find” is a unix command-line tool for locating files (and directories). The results can be displayed, passed to another command (e.g. grep, ls etc, see more below), or the find command has its own limited set of actions that can be performed too, such as delete.

Find allows you to specify all manner of search criteria such as name, location, size, permissions, modify date etc. Using regex expressions with those criteria makes it more flexible still.

See the full find manual here.


Tags: , , , ,

Find files in Unix

I frequently end up searching an entire directory for an elusive file and I can never remember the exact command structure, so posting here:

    find /dir/to/search -name "filename.ext" 2>/dev/null

And wildcards are allowed. e.g.

    find . -name "filename.*" 2>/dev/null

The latter command searches the current directory AND all sub directories.

The ‘2>/dev/null’ avoids those annoying “find: cannot read dir …: Permission denied” errors.
You can even simplify it by creating your own script that takes the file name as a parameter:

    echo "Searching for files called $1 in current dir and all sub-dirs"
    find . -name "$1" 2>/dev/null

Meaning you just need to call, for example:

    find filename.*

Also, if you want to search for files containing specific text, try

    find /dir/to/search -exec grep -il "txtToSearchFor" {} \;

The “-il” means ignore case and print only the names of files with matching lines (as opposed to the line contents).

Update 4 May 2014: I have added variations of these scripts to my scripts repo on Github. Specifically findf (find files) and findt (find text in files);

See also:

Tags: , , , ,