Lesson 17 - Searching for files
Lesson 17: Searching for files using find
The command find automates
traversing the file system from the given starting point to search for
files (or directories) that match test criteria such as filename, owner
or time range. If a starting point is not passed to find
, all files
and directories in and below the current directory are printed (the
current working directory) is assumed.
An action is required to tell find
what to do with each file that
matches the test criteria. The default behaviour is to print matches to
the terminal (identical to -print
) however, other actions can be used
such as -delete
(to delete matching files) and -exec
(execute
command on matching files), etc. See the manual page for more options.
Let's find the file earth.txt
whilst the current working directory is
~
. The test criteria for matching filenames is -name
.
[learner planets]$ cd ~
[learner ~]$ find -name earth.txt
./learning_linux/planets/earth.txt
Wildcards can be used if parts of the test are known, for example a partial filename.
[learner ~]$ find -name e*h.txt
./learning_linux/planets/earth.txt