Using find in scripts

Basic find example

Find a file with name myFile in root directory:

find / -name myFile

If the file cannot be found, nothig is output to the console. Create a myFile file in /tmp directory and reexecute the find command:

touch /tmp/myFile
find / -name myFile

As by default find searches in all the subdirectories, the myFile file is found in the /tmp directory. Output:

/tmp/myFile

Find without searching in subdirectories

In order to limit search only to the specified directory, a maxdepth option is used:

find / -maxdepth 1 -name myFile

In this case, nothing is output to the console, as the myFile file is located in /tmp directory