Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore/prune hidden directories with GNU find command

Tags:

bash

When using the find command, why is it that the following will successfully ignore hidden directories (those starting with a period) while matching everything else:

find . -not \( -type d -name ".?*" -prune \)

but this will not match anything at all:

find . -not \( -type d -name ".*" -prune \)

The only difference is the question mark. Shouldn't the latter command likewise detect and exclude directories beginning with a period?

like image 858
kostmo Avatar asked Feb 01 '10 02:02

kostmo


2 Answers

The latter command prunes everything because it prunes . - try these to see the difference:

$ ls -lad .*
.
..
.dotdir
$ ls -lad .?*
..
.dotdir

You see that in the second one, . isn't included because it is only one character long. The glob ".?*" includes only filenames that are at least two characters long (dot, plus any single character, non-optionally, plus any sequence of zero or more characters).

By the way, find is not a Bash command.

like image 121
Dennis Williamson Avatar answered Nov 03 '22 07:11

Dennis Williamson


The latter command prunes . itself -- the directory you're running find against -- which is why it generates no results.

like image 40
Charles Duffy Avatar answered Nov 03 '22 07:11

Charles Duffy