Linux #2: Search for a File by Filename

Part two of the Linux series which introduces linux shell commands: how to find a file in linux.

If you want to find text within a file read Linux #1: Search for a File containing text.

Find a File in Linux by its Filename

Search for a file in the current directory as well as recursively in all subdirectories:

find -name filename.txt

search in a specific directory and subdirectories for a file by name:

find /directory_to_search -name filename.txt

To find a file or directory containing a certain string in the name use regular expressions in single quotes:

find -name 'filename*'
find -name '*txt'

So basically there are two things you must remember: 1) the order of the find command arguments is different than the order for pretty much every other command (it is: directory first, then the rest) and 2) regular expressions must be enclosed by single quotes.

Much more is possible to narrow down the search. For example only searching for files of a specific size or date. See the find man pages for more details.

Related Posts:

Leave a Reply

Your email address will not be published.