find
Usage.
Indicates that a file (folder) is found under a path.
-
Entering a single
find
will display all files and folders in the current directory. -
Search in the specified directory
Look in the
. /app/
directory to find files. -
Specify the type of file to find as a directory or file
Use
-type d
to find only directories,d
is short for directory and finds all directories in the current directory.Use
-type f
to find only files,f
is short for file. Find all files in the current directory. -
Specify file (folder) name search
Use
-name <name>
to find files (folders) with the name<name>
. Find the file with the namefile1.txt
in the current directory and subdirectories.Names support wildcards, but must be included in quotation marks.
-
Search by path
-path
means find by path. -
Combined lookup: AND and OR operators
-or
means or, the following command finds the file (folder) with the namefile1.txt
or the namefile2.txt
.-and
means with (and), the following command looks for a folder whose name satisfiesfile1*
and is a folder. -
Ignore case
Suppose you want to find files of type
js
and ignore the suffix case, you can do so.1
$ find . -name "*.js" -or -name "*.JS" -or -name "*.Js" -or -name "*.jS"
Similarly you can simply add an
i
–-iname
beforename
to indicate that case is ignored.1
$ find .-iname "*.js"
All of the previously mentioned options support prefixing
i
to indicate case-insensitive. -
NOT operator
A more complex operation can be added, using
NOT
to mean not (no). Find files (folders) with names*.js
and*.html
but with paths other than*programs*
. -
Delete the results of the search
You can use
-delete
to delete the results of a search, for example, to delete all html files in the current repository.1
$ find . -name "*.html" -type f -delete
Note that using this option to delete folders does not delete non-empty folders. If you want to delete a folder, for example I want to delete all
node_modules
in the directory (I often have this need under large front-end projects), you can do so.1
$ find . -name node_modules -type d | xargs rm -rf