Unix Shell - Execute command on one file per directory

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > Execute command on one file per directory





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Execute command on one file per directory
mathieu

2007-11-29, 1:29 pm

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
Icarus Sparry

2007-11-29, 1:29 pm

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.
mathieu

2007-11-30, 7:35 am

On Nov 29, 4:41 pm, Icarus Sparry <use...@icarus.freeuk.com> wrote:
> On Thu, 29 Nov 2007 06:27:03 -0800, mathieu wrote:
>
>
>
>
>
>
> 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
Icarus Sparry

2007-12-01, 1:22 pm

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

> On Nov 29, 4:41 pm, Icarus Sparry <use...@icarus.freeuk.com> wrote:
>
> 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.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com