Continuing my deep dive into shell and editor commands to find, useful tools that I’m not taking full advantage. This week is the Find and Tree commands.
Find is used for traversing a tree of files and performing some operation on
them. It is one of the core Unix utilities and fairly universal on systems. My
big discovery this time is the realization that I can use find for more than
just searching for files. I can use find to perform operations on the results.
There are multiple actions besides the default
-exec both open up a world of possiblities that I would have otherwise
resorted to piping the results into a loop (or resorted to Python) to resolve.
$ find [-H] [-L] [-P] path... [expression]
-P is the default behavior.
find to follow symbolic
-H will follow symbolic links while procesing the command line
path parameter is required and can use globbing similar to
or other terminal commands.
find excepts a number of operators for fine tuning our search.
( expr )
! expr evaluates true if the expression is false,
expr2 only if
expr1 is true,
expr1 -o expr2 evaluates as
true if either expression is true. For example:
find . -name 'fileA*' -o -name 'fileB*'
Searches the current working tree for a file whose names start with “fileA” or “fileB.”
||Searches working directory for files starting with “my”|
||As above, but excludes directories and searches only for “regular files”|
||As above, but pipes the results into the
||As above, but this time we are searching both the working directory and the
||Find all markdown or latex files in the working directory|
||Finds all markdown files in the working directory and executes
||Deletes all empty directories in the working directory. Note, that the delete option can simply be used as a replacement for the default option of
||Case insensitive name search|
||Find allows for searching by file size.|
||Find all files modified in the last month. We can do
||Find all files modified in the last hour.
While reading on
find last week, I stumbled upon
Tree is one of
those commands that I ocassionally recall, think is really cool. Then completely
Tree gives you the ability to generate a visualization of the directory tree, much like the old Windows Explorer provided a tree view of your directory.
In simplest usage, you simple call
tree, and it outputs a tree representation
of the current working directory. If we want to display a different directory,
we can provide that for the first argument:
tree displays symbolic links showing where they point towards.
However, if the link is a directory, it does not, by default recurse into that
||Display hidden files|
||List directories only|
||Display full paths|
||Don’t indent/show tree lines. Use in conjunction with
||That is a lowercase “L,” do recurse into symbolic directories|
||List files that match the pattern, or list files that don’t match the pattern|
||Print the user, group, permissions, size in bytes, or human-readable sizes|