From: Jerry Fleming on
Hi,

I want to use find to list my files, but exclude those in a directory
containing a certain file. In the latest version of tar, there is
--exclude-tag-all
<http://www.gnu.org/software/tar/manual/html_section/exclude.html> to do
this. I was wondering how to achieve the same effect using find.

[quote]
'--exclude-tag-all=file'
Omit directories containing file file entirely.
[/quote]
Thanks.
From: Thomas 'PointedEars' Lahn on
Jerry Fleming wrote:

> I want to use find to list my files, but exclude those in a directory
> containing a certain file. In the latest version of tar, there is
> --exclude-tag-all
> <http://www.gnu.org/software/tar/manual/html_section/exclude.html> to do
> this. I was wondering how to achieve the same effect using find.

find $PATHS \
-path $(find $PATHS -type d -exec test -e "{}/$FILE" \; -print) \
-prune -o -print

should work (tested on Debian) if there is only one directory below $PATHS
containing a file with name $FILE. You can perhaps use sed(1) or awk(1) to
generate several -path expressions if there is more than one directory.


PointedEars
From: Jerry Fleming on
On 2010-05-14 10:21, Thomas 'PointedEars' Lahn wrote:
> Jerry Fleming wrote:
>
>> I want to use find to list my files, but exclude those in a directory
>> containing a certain file. In the latest version of tar, there is
>> --exclude-tag-all
>> <http://www.gnu.org/software/tar/manual/html_section/exclude.html> to do
>> this. I was wondering how to achieve the same effect using find.
>
> find $PATHS \
> -path $(find $PATHS -type d -exec test -e "{}/$FILE" \; -print) \
> -prune -o -print
>
> should work (tested on Debian) if there is only one directory below $PATHS
> containing a file with name $FILE. You can perhaps use sed(1) or awk(1) to
> generate several -path expressions if there is more than one directory.
>
>
> PointedEars

Thanks. That does the trick. But I was wondering if there is anything
builtin to find that can do the same thing. It turns out both -name and
-path test only the current file system entry been stat'ed. Is there
anything in find to conditionally -prune a directory based on its content?

Jerry
From: Stephane CHAZELAS on
2010-05-14, 08:58(+08), Jerry Fleming:
[...]
> I want to use find to list my files, but exclude those in a directory
> containing a certain file.
[...]

find . -type d -exec sh -c '[ -e "$1/file" ]' sh {} \; -prune -o ...

--
Stéphane
From: Thomas 'PointedEars' Lahn on
Stephane CHAZELAS wrote:

> Jerry Fleming wrote:
> [...]
>> I want to use find to list my files, but exclude those in a directory
>> containing a certain file.
> [...]
>
> find . -type d -exec sh -c '[ -e "$1/file" ]' sh {} \; -prune -o ...

Clever solution, but ISTM this can be simplified to

find . -type d -exec sh -c '[ -e "$1/file" ]' : '{}' \; -prune -o ...

then

find . -type d -exec sh -c '[ -e "$0/file" ]' '{}' \; -prune -o ...

and then

find . -type d -exec test -e '{}/file' \; -prune -o ...

You may also use

find . -type d -exec [ -e '{}/file' ] \; -prune -o ...

instead.


PointedEars