help-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question about find(1): How to invoke multiple commands in exec?


From: era+gmane
Subject: Re: Question about find(1): How to invoke multiple commands in exec?
Date: Mon, 26 Jul 2004 10:22:19 +0300
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

On Fri, 23 Jul 2004 15:15:57 +0200, Andreas R.
<not_a_real_address@nowhere.com> posted to gmane.comp.gnu.utils:
 > mike burrell wrote:
 >> find . -type d -exec sh -c "find {} -print | wc -l" \;
 > Yes, thanks.
 > The final result is used to find the directories with most files in
 > them (we have quota limitation on number of files on our university
 > computers):
 > find . -mindepth 1 -maxdepth 1 -type d -exec sh -c 'find {} -print | wc -l  
 > | perl -ane "\$a = \$F[0]; print  \"\$a\t\" " ' \; -printf "%f\n" | sort -n 
 > -r
 > The perl expression just substitues \n by \t in a weird way (I don't know 
 > how to do this in sed).

If you're running Perl on the result anyway, why not use Perl to get
the number of files directly?

  find . -type d -exec perl -e '$dir = q"{}"; @files = <$dir/*>;
                                 print scalar @files, "\t$dir\n";' \; |
  sort -rn

If you have directory names with double quotes in them, you'll need to
use something slightly more creative instead of double quotes in
q"{}". The only really safe choice would be null characters, but it
seems Perl can't cope with them (or doesn't get to see them in the
first place). But something like ctrl-H might be a reasonably safe
alternative (in many shells, type ctrl-V before the key you want to
insert literally), or you can do the -print0 | xargs -0 shuffle.

Using Perl instead of sh somehow makes it all less arcane, at least to
me, but YMMV. Both are a good answer to "how do I invoke multiple
commands in find -exec?" (as are no doubt also awk, python, tk,
scheme, and what not; each with their own strengths and weaknesses).

Hope this helps,

/* era */

-- 
formail -s procmail <http://www.iki.fi/era/spam/ >http://www.euro.cauce.org/
cat | more | cat<http://www.iki.fi/era/unix/award.html>http://www.debian.org/





reply via email to

[Prev in Thread] Current Thread [Next in Thread]