Linux commands: find
A quick guide to the find command, used to find files and folders on the filesystem
The find command can be used to find files or folders matching a particular search pattern. It searches recursively.
Let’s learn it by example.
Find all the files under the current tree that have the .js extension and print the relative path of each file matching:
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 src
Use -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.txt
Find directories under the current tree matching the name “node_modules” or ‘public’:
find . -type d -name node_modules -or -name public
You 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:
find . -type f -size +100c
Search files bigger than 100KB but smaller than 1MB:
find . -type f -size +100k -size -1M
Search files edited more than 3 days ago
find . -type f -mtime +3
Search files edited in the last 24 hours
find . -type f -mtime -1
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:
find . -type f -mtime -1 -delete
You can execute a command on each result of the search. In this example we run cat to print the file content:
find . -type f -exec cat {} \;
notice the terminating \;. {} is filled with the file name at execution time.
download all my books for free
- javascript handbook
 - typescript handbook
 - css handbook
 - node.js handbook
 - astro handbook
 - html handbook
 - next.js pages router handbook
 - alpine.js handbook
 - htmx handbook
 - react handbook
 - sql handbook
 - git cheat sheet
 - laravel handbook
 - express handbook
 - swift handbook
 - go handbook
 - php handbook
 - python handbook
 - cli handbook
 - c handbook
 
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.