Execute command on one file per directory

Could anyone tell me what is wrong with the following:

Print one filename per directory (ok):

find /images/ -type d -exec find {} -maxdepth 1 -type f -print -quit
\;

Print one basename per directory:

find /images/ -type d -exec find {} -maxdepth 1 -type f -print -quit -
exec basename {} png \; \;

Thanks
mathieu.malaterre [ Do, 29 November 2007 15:27 ] [ ID #1881346 ]

Re: Execute command on one file per directory

On Thu, 29 Nov 2007 06:27:03 -0800, mathieu wrote:

> Could anyone tell me what is wrong with the following:
>
> Print one filename per directory (ok):
>
> find /images/ -type d -exec find {} -maxdepth 1 -type f -print -quit \;
>
> Print one basename per directory:
>
> find /images/ -type d -exec find {} -maxdepth 1 -type f -print -quit -
> exec basename {} png \; \;
>
> Thanks

Lots of things. how does the first find know that it needs to skip over
the first semicolon and use the second one?

How does the first find know it should substitute for the first {}, but
not the second?

If you "quit", why do you expect to "exec" later?

I suggest something more along the lines of

find /images -type d -exec \
sh 'find $1 -maxdepth 1 -type f -exec basename {\} png \;' {} {} \;

if this needs to go on a single line. Otherwise write one shellscript
that given a list of directorynames does whatever you want in those
directories, and then invoke that from find.
Icarus Sparry [ Do, 29 November 2007 16:41 ] [ ID #1881351 ]

Re: Execute command on one file per directory

On Nov 29, 4:41 pm, Icarus Sparry <use... [at] icarus.freeuk.com> wrote:
> On Thu, 29 Nov 2007 06:27:03 -0800, mathieu wrote:
> > Could anyone tell me what is wrong with the following:
>
> > Print one filename per directory (ok):
>
> > find /images/ -type d -exec find {} -maxdepth 1 -type f -print -quit \;
>
> > Print one basename per directory:
>
> > find /images/ -type d -exec find {} -maxdepth 1 -type f -print -quit -
> > exec basename {} png \; \;
>
> > Thanks
>
> Lots of things. how does the first find know that it needs to skip over
> the first semicolon and use the second one?
>
> How does the first find know it should substitute for the first {}, but
> not the second?
>
> If you "quit", why do you expect to "exec" later?

Because I want only *one* file per directory. Without the quit it is
not working...
but indeed with the -quit -exec does not work anymore

-Mathieu
mathieu.malaterre [ Fr, 30 November 2007 10:42 ] [ ID #1882423 ]

Re: Execute command on one file per directory

On Fri, 30 Nov 2007 01:42:45 -0800, mathieu wrote:

> On Nov 29, 4:41 pm, Icarus Sparry <use... [at] icarus.freeuk.com> wrote:
>> On Thu, 29 Nov 2007 06:27:03 -0800, mathieu wrote:
>> > Could anyone tell me what is wrong with the following:
>>
>> > Print one filename per directory (ok):
>>
>> > find /images/ -type d -exec find {} -maxdepth 1 -type f -print -quit
>> > \;
>>
>> > Print one basename per directory:
>>
>> > find /images/ -type d -exec find {} -maxdepth 1 -type f -print -quit
>> > - exec basename {} png \; \;
>>
>> > Thanks
>>
>> Lots of things. how does the first find know that it needs to skip over
>> the first semicolon and use the second one?
>>
>> How does the first find know it should substitute for the first {}, but
>> not the second?
>>
>> If you "quit", why do you expect to "exec" later?
>
> Because I want only *one* file per directory. Without the quit it is not
> working...
> but indeed with the -quit -exec does not work anymore
>
> -Mathieu

Let me try and explain another way.

If you want to pick just one apple from a tree, which of the following
two sequences of instructions will work?

go to tree
pick apple
commit suicide

or

go to tree
commit suicide
pick apple

In the second case the apple does not get picked (I discount action hero
movies where the hero manages to appear dead, but then struggles up and
presses the button to save the world and only then dies).

If you use '-quit' before the '-exec' then you will not run the exec even
once. You need to swap the order.
Icarus Sparry [ Sa, 01 Dezember 2007 18:58 ] [ ID #1883393 ]
Linux » comp.unix.shell » Execute command on one file per directory

Vorheriges Thema: Daylight saving time DST and timestamp handling
Nächstes Thema: tricky sed problem