Joseph Hallenbeck
February 09, 2018

Shell Notes: Find and Tree

Filed under: Software Development part of Notes

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

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 -print, e.g. -delete and -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]

The flag -P is the default behavior. -L specifies find to follow symbolic links. -H will follow symbolic links while procesing the command line arguments. The path parameter is required and can use globbing similar to ls or other terminal commands.

find excepts a number of operators for fine tuning our search. ( expr ) forces precedence, ! expr evaluates true if the expression is false, expr1 -a expr2 evaluates 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.”

Example commands:

$ find . -name 'my*' Searches working directory for files starting with “my”
$ find . -name 'my*' -type f As above, but excludes directories and searches only for “regular files”
$ find . -name 'my*' -type f -ls As above, but pipes the results into the ls command.
$ find . ../Done -name 'my*' -type f -ls' As above, but this time we are searching both the working directory and the ../Done directory!
$ find . -name '*md' -o -name '*latex' Find all markdown or latex files in the working directory
$ find . -name '*.md' -type f -exec chmod 664 {} \; Finds all markdown files in the working directory and executes chmod 664 replacing the value {} with the path to the file. Note the required \; at the end of the command and that the command cannot be placed in quotes.
$ find . -type d -empty -delete 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 -print. That is, whatever whould hae been returned without the -delete is what would be deleted.
$ find . -iname "*MD" Case insensitive name search
$ find . -size 100k -a -size -500k Find allows for searching by file size.
$ find . -mtime 30 Find all files modified in the last month. We can do -mtime +50 -mtime -100 to find files modified more than 50 days ago and less than 100 days.
$ find . -cmin -60 Find all files modified in the last hour. find . -mmin -1 does the same thing but with an interval of hours.

Tree

While reading on find last week, I stumbled upon tree. Tree is one of those commands that I ocassionally recall, think is really cool. Then completely forget about.

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 ~/Documents.

By default, tree displays symbolic links showing where they point towards. However, if the link is a directory, it does not, by default recurse into that directory.

Flags:

-a Display hidden files
-d List directories only
-f Display full paths
-i Don’t indent/show tree lines. Use in conjunction with -f to create a file list
-l That is a lowercase “L,” do recurse into symbolic directories
-P pattern or -I pattern List files that match the pattern, or list files that don’t match the pattern
-u, -g, -p, -s, -h Print the user, group, permissions, size in bytes, or human-readable sizes
"Shell Notes: Find and Tree" by Joseph Hallenbeck is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.