Bash Shell Tips - find
Created //
The bash find
command
Show all files in this folder and all subfolders
$ find . -type f
Show all folders (directories) in this folder and all subfolders
$ find . -type d
Delete all those .DS_Store
files on MacOS
This will delete all .DS_Store
files in your home folder, and any subfolder.
$ find . -name '.DS_Store' -type f -delete
Find all files that changed recently
Assume we're looking for changed files anywhere in the current folder, or any subfolder.
Files that changed in the last day:
$ find . -mtime -1
Files that changed in the last 5 days:
$ find . -mtime -5
Files that changed in the last 60 minutes:
$ find . -mtime -60m
unit | meaning | example | description |
---|---|---|---|
s | second | find . -mtime -60s |
files modified in past 60 seconds |
m | minute | find . -mtime -60m |
files modified in past 60 minutes |
h | hour | find . -mtime -24h |
files modified in past 24 hours |
d | day | find . -mtime -7d |
files modified in past 7 days |
w | week | find . -mtime -2w |
files modified in past 2 weeks |
Find all files that are bigger than a certain size
For example, find all files in this folder or any subfolder that are 1mb or larger in size.
$ find . -type f -size +1M
Find all files in this folder or any subfolder that are 1gb or larger in size.
$ find . -type f -size +1G
unit | meaning | example | description |
---|---|---|---|
k | kilobyte | find . -type f -size +1k |
files >= 1kb |
M | megabyte | find . -type f -size +1M |
files >= 1mb |
G | gigabyte | find . --type f -size +1G |
files >= 1gb |
Putting options together
Find all jpg files larger than 1mb that were changed in the past day.
$ find . -type f -name "*.jpg" -size +1M -mtime -1d
Find all files with a specific extension
Find all ".jpg" files in this folder, or any subfolder.
$ find . -name "*.jpg"
Find all ".txt" files this folder, or any subfolder.
$ find . -name "*.txt"
To make sure you're only getting files, and not folders that might match as well, add the -type f
parameter. E.g.:
$ find . -name "*.txt" -type f
Note that -name
is case-sensitive. If you don't want it to be case-sensitive, use -iname
instead.
Find files without a specific extension
Find all files that do NOT end in ".jpg" in this folder, or any subfolder.
$ find . -not -name "*.jpg"
You can combine these. Find all files that do NOT end in ".jpg" and do NOT end in ".webp".
$ find . -not -name "*.jpg" -not -name "*.webp"
Find files or folders with a specific keyword
Let's say I'm looking for all files that have the keyword "test" in them. That could be filename.test
, it could be testfile.js
, it could be path/to/tests/myfile.js
, etc.
$ find . -name "*test*"
Find files or folders with multiple keywords
I want all files/folders that have "test" and "queue" in them. Also use -iname
so it's not case-sensitive. (Meaning, this will find "test" and "Test" and "TEST", etc.)
$ find . -iname "*test*" -iname "*queue*"
Learn how to use find
Remember, most unix/linux commands already have documentation built into the operating system.
$ man find
... will show the documentation for find
.
For more about 'man pages' see:
// ka