Finding Files with the CLI

Find is a command available on OSX and Linux to find things like files and directories.

find [path] [expressions]

Searching by name

You can use -name which will try and find exact matches in the filename, or to ignore case use -iname instead.

Running find . -iname ‘readme’ will search the current directory for files matching ‘readme’ (e.g. README, readme, but not README.md).

To search with wild cards use an asterisk. find . -iname ‘readme*’ will return any file starting with ‘readme’ (e.g. README, readme.txt, READMELATER.txt)

Searching by type

You can search for files using -type f or directories using -type d. Running find . -type f will search the current directory for normal files.

Subdirectories

Find will search all subdirectories by default, but you can restrict how deep it searches.

-mindepth sets how deep to start searching. find . -mindepth 2 will only return results from two directories down (including current).

-maxdepth sets how deep to stop searching. find . -maxdepth 2 will only return results up too two directories deep. To only search the current directory use find . -maxdepth 1.

More than one expression

You can search using more than one expression at once. To find all text files in the current directory run the following:

find . -type f -maxdepth 1 -iname '*.txt'
Posted on 11 December 2015 in cli