find command
The Linux find command
find commandThe find command can be used to find files or folders matching a particular search pattern. It searches recursively.
Let's learn how to use it by example.
Find all the files under the current tree that have the .js extension and print the relative path of each file that matches:
find . -name '*.js'It's important to use quotes around special characters like * to avoid the shell interpreting them.
Find directories under the current tree matching the name "src":
find . -type d -name srcUse -type f to search only files, or -type l to only search symbolic links.
-name is case sensitive. use -iname to perform a case-insensitive search.
You can search under multiple root trees:
find folder1 folder2 -name filename.txtFind directories under the current tree matching the name "node_modules" or 'public':
find . -type d -name node_modules -or -name publicYou can also exclude a path using -not -path:
find . -type d -name '*.md' -not -path 'node_modules/*'You can search files that have more than 100 characters (bytes) in them:
Search files bigger than 100KB but smaller than 1MB:
Search files edited more than 3 days ago:
Search files edited in the last 24 hours:
You can delete all the files matching a search by adding the -delete option. This deletes all the files edited in the last 24 hours:
You can execute a command on each result of the search. In this example we run cat to print the file content:
Notice the terminating \;. {} is filled with the file name at execution time.
Last updated