tags:

views:

148

answers:

2

I wonder how to specify to the command "find" for searching files under current directory but skipping some specific sub-directories.

For example, I would like to skip sub-directories that match "./dir1/*.1/"

Thanks and regards!


EDIT:

Thanks for your help! It works with -prune.

If I would like to exclude subdirectories that match "./dir1/train*.1/", "./dir2/train*.3/", "./dir1/dir3/train*.2/", ..., how can I specify all of them? I tried "-path '*/train*.*' -prune" but it does not work.

Thanks again!

+1  A: 

Check this out. EDIT: Straight from the man page: `To ignore a directory and the files under it, use -prune'.

nc3b
Thanks for your help! It works with -prune.If I would like to exclude subdirectories that match "./dir1/train*.1/", "./dir2/train*.3/", "./dir1/dir3/train*.2/", ..., how can I specify all of them? I tried "-path '*/train*.*' -prune" but it does not work.Thanks again!
Tim
+3  A: 

You need to use the -path option and the -prune option, like this:

find . -type d -path "./dir1/*.1" -prune -o -print
Donal Fellows
Thanks for your help! It works with -prune.If I would like to exclude subdirectories that match "./dir1/train*.1/", "./dir2/train*.3/", "./dir1/dir3/train*.2/", ..., how can I specify all of them? I tried "-path '*/train*.*' -prune" but it does not work.Thanks again!
Tim
@Tim: If you want to exclude every directory whose name matches `train*.[0-9]`, then you can do that with: `-type d -name "train*.[0-9]" -prune"`
Donal Fellows
Thanks Donal! So the path specified right before -prune will be excluded? What do -o and -print mean here? I found that if I need to search some files with some filename but not under some directory, I have to put the filenames between -o and -print. Thanks again!
Tim
@Tim: They're basic find features: `-print` means “print the current filename” and `-o` means “or” (in a boolean short-circuiting sense). It turns out that `-print` is actually the default behavior if things are simple enough, but what you're after is beyond that so you need to write it explicitly. And yes, you can chain together complex stuff with several `-o` options; 'find' is a clever power tool.
Donal Fellows
BTW, did you know that originally “find” didn't print anything unless you'd included a `-print` somewhere? An early noob mistake was to leave it out and wonder why you didn't get anything. GNU find was the first (IIRC) that printed if there wasn't anything else "active" done.
Donal Fellows