linux how to find file by name

Answer

To search for files on the disk, you can use the find command. The find command has the following syntax:

find /where_to_start -name "name_of_file"

If you do not mention parameter /where_to_start, it will automatically search in the current directory. Current directory, which you currently stand, is available typing pwd command. The second parameter -name "name_of_file" - is shown filter. This filter shows only those files in which there is a string "name_of_file". You can also include an asterisk, for example: "name_of_file.*".

If you want to recursively overview /etc directory and find all files that have the extension ".conf", you can do it this way:

find /etc -name "*.conf"

If the find command does not access to any folder, it write error about it. If you do not run the command as super user, it is better to redirect error messages to $HOME/find_errors or to trash /dev/null.

In next example we redirect errors to file find_errors that will be situated in our home folder:

find /etc -name "*.conf" 2> $HOME/find_errors

In next example we redirect errors to the system trash:

find /etc -name "*.conf" 2> /dev/null

To find file by a name, ignoring the case use option -iname:

find /etc -iname "name_of_file"

If you want to find all files these don't match pattern:

find /etc -not -name ".*.conf"

Was this information helpful to you? You have the power to keep it alive.
Each donated € will be spent on running and expanding this page about UNIX Shell.