Saturday, September 27, 2008

Exclude directory from bash find command


Find command to search all the *.sh files starting from current directory.

$ find . -name "*.sh" -print
./dir1/dir11/help.sh
./dir1/sol.sh
./dir2/dir22/a.sh
./dir2/g.sh
./dir3/dert.sh
./dir3/tty.sh


Now to skip or exclude the directory './dir2' and all files and directories under it, use -prune option with bash find command.

$ find . -path './dir2' -prune -o -name "*.sh" -print
./dir1/dir11/help.sh
./dir1/sol.sh
./dir3/dert.sh
./dir3/tty.sh

7 comments:

Kevin D. Wolski said...

Perfect! Just what I was looking for. Thanks for the article.

Matt said...

I am searching using something like this in TONS of directories and many of them I don't have permission to view. May lines in my output look like:

find: /directory/somewhere: Permission denied

Is there any good way to exclude directories I don't have permission to view?

Great post. Thank you!

Unknown said...

@Zebe, thanks for commenting.

@Matt, well, at present I am not finding a way for the same; did a lot and lot of google too, but no luck. Even I am very curious to know about this. Please let me know if you find a way for the same.

Only one thing I can think of is to 2> the err to /dev/null, which obourse is not the query you are asking :-)

Thanks for commenting. Keep in touch.

// Jadu

RAM said...

add -perm -a+r -perm /a+w ! -perm /a+x to find command will do the trick

RAM said...

"add -perm -a+r -perm /a+w ! -perm /a+x to find will do the trick."

Chaitanya Sharma Yamijala said...

Thanks, that helped a lot to me!

Buck Manhands said...

@RAM - that is exacly what was needed, thanks :)

© Jadu Saikia www.UNIXCL.com