Shaun Abram
Technology and Leadership Blog
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 find.sh script that takes the file name as a parameter:
#!/bin/bash
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:
- http://en.wikipedia.org/wiki/Find
- http://www.unix.com/unix-dummies-questions-answers/3152-find-files-containing-text.html
- Ignore git (or svn) directories in a find
Tags: bash, find, scripts, search, unix