Monday, December 26, 2005

unix tip: find - limit the depth

by default find will search all subdirectories under the parent directory. but you can limit the depth by using maxdepth option.
eg:

- to limit it only to the current directory and not search through any subdirectories, use the -maxdepth 1 option.
$ find . -maxdepth 1 -name test
./test

- to search one level below of the directory use the -maxdepth 2 option.
$ find . -maxdepth 2 -name test
./scripts/test
./test
./tmp/test

- if not using -maxdepth option.
$ find . -name test
./scripts/test
./test
./tmp/test
./.snapshot/hourly.2/test
./.snapshot/hourly.2/tmp/test
./.snapshot/hourly.1/test
./.snapshot/hourly.1/tmp/test
./.snapshot/hourly.0/test
./.snapshot/hourly.0/tmp/test


2 comments:

dfhzn said...

We can also add -xdev where it doesn't descend on other filesystems. This is useful if we want to exclude other HDD partitions and network mounts.

Joey aka Rizal said...

Wow this is truely a geek heaven.:)