Lost and find
Looking for a file or directory?
The find command is what you need.
This is one of those rare occasions where a command doesn’t have a slightly cryptic name.
find can be used to find files or directories.
Say you want to find all PDF documents in your home directory.
find ~ -name '*.pdf'
The ~ (tilde) is a shortcut for your home directory.
We tell it to search for a name with -name matching pattern the pattern
enclosed in single-quotes.
The * matches any number of characters, so effectively we are matching
anything that ends with ‘.pdf’.
Note that it searches in all sub-directories of the given path by default.
We can use -iname for a case-insensitive match.
find ~ -iname '*.pdf'
find can also match on other attributes that name.
We can look for all files in any sub-directories that have been modified within a day (24h).
find . -type f -mtime 0
Or within a week:
find . -type f -mtime 7
The -type f flag means to only match files.
You can also do -type d instead for directories.
To look for large files in your “Download” directory, do:
find ~/Downloads -type f -size +100M
It is also possible to execute other commands on the matches.
find . -name '*.md' -type f -exec wc -m "{}" \;
It counts characters (using wc -m) on markdown files.
Where {} is placeholder for file path and \; signals end of parameters.
It invokes wc for each entry.
We can also have it invoke wc just once with all file paths as parameters
with:
find . -name '*.md' -type f -exec wc -m "{}" \+
Notice it ends with \+.