Find examples

From lippmann wiki
Jump to: navigation, search

Excluding directories from a find query

For my work I regularly have to investigate how deep directory structures go. Some storage vendors like NetApp have default snapshot directories that allow you to access older revisions of files. When I do a query for the depth of a filesystem diretory tree, I don't want to include these hidden .snapshot directories. A quick command to list all directories, excluding the ones in the .snapshot dir is the command below:

find . -print -not \( -name .snapshot -prune \)

Let me explain

(-name .snapshot -prune) means, once you find .snapshot, don't descend into it. This makes sure that any files inside the .snapshot dir that would not be called .snapshot themselves (but hourly_blablabla) are skipped from the find listing/query.

Then this entire area (the .snapshot dir, and everything that's inside it) is flagged as a negative match with the -not. After that, if needed we can filter for folders only by adding -type d .

To run this for just directories, and silence the output, we can do something like this:

sudo find . -type d -print -not \( -name .snapshot -prune \) >/dev/null