find | head | xargs

find | head | xargs

Post by Mike Cast » Tue, 06 Jul 1999 04:00:00



I'm stumped.  I may have to switch to perl for this, but I didn't want to.

Originally I had a script that did:

find dir -print | head -50 > file

xargs -r script < file

xargs -r rm < file

Well, of course this has some problem, especially with things like embeded
spaces in the file names.

So, I switched to using the GNU extension  -print0 for find and -0 for
xargs.  But now, of course, I can't use head to limit the processing to 50
lines.

Things need to be batched for the script to be useful (it formats
information for human review, then the rm gets done based upon positive
feedback), so I can't use find -exec.

Using -l50 for xargs gets me part way there, but it still runs multiple
times.

I guess I could use find -print | head and modify script to read the files
from stdin rather than the command line, but I'm wondering if there are
other solutions (outside of adding a --run-maxtimes option to xargs).

Thanks,
mrc

 
 
 

find | head | xargs

Post by Bruce Woodwar » Wed, 07 Jul 1999 04:00:00


My reply maybe a little to the left..

find dir -print | sed -e 1,50p >file

script $(<file)
or
script $(cat file)

Do the rm the same way... or perhaps ...

find dir -print | sed -e 1,50p | while read
do
    script $REPLY && rm $REPLY
done


>I'm stumped.  I may have to switch to perl for this, but I didn't want to.

>Originally I had a script that did:

>find dir -print | head -50 > file

>xargs -r script < file

>xargs -r rm < file

>Well, of course this has some problem, especially with things like embeded
>spaces in the file names.

>So, I switched to using the GNU extension  -print0 for find and -0 for
>xargs.  But now, of course, I can't use head to limit the processing to 50
>lines.

>Things need to be batched for the script to be useful (it formats
>information for human review, then the rm gets done based upon positive
>feedback), so I can't use find -exec.

>Using -l50 for xargs gets me part way there, but it still runs multiple
>times.

>I guess I could use find -print | head and modify script to read the files
>from stdin rather than the command line, but I'm wondering if there are
>other solutions (outside of adding a --run-maxtimes option to xargs).

>Thanks,
>mrc


 
 
 

1. Efficiently executing multiple commands with find and/or xargs

Suppose I want to run a `find' and perform multiple commands on each file it
returns.  I can do (using the GNU xargs)

  find /some/path -print | xargs -i sh -c 'command1 "{}"; command2 "{}"'

but that's not terribly efficient, because it forks sh for each command.
It seems there should be a better way, but I'm at a loss for finding it.
I've tried variations on

  find /some/path -print | xargs -i 'command1 "{}"; command2 "{}"'

which seems like it *could* work, but doesn't, and variations on

  find /some/path -exec command1 "{}" \; -exec command2 "{}" \;

which only executes the first command and not the second (which puzzles me,
because I don't believe that this is clear in any of the man pages for `find'
that I've seen).

So what is the best way of running a multi-command find sequence?
--


2. Booting Solaris - Urgent please help

3. Find, xargs and spaces

4. Boot managers

5. Using find with xargs

6. How to get an eXceed-like display *and* KDE on Linux

7. find, xargs, grep ???

8. Mounting CD's on AIX with execute permissions on files.

9. ?s on find|sed|xargs cp and rm

10. Difference between xargs and -exec with find

11. find and xargs mv

12. Question: <command> | xargs find [expression] [path...]

13. Ever done 'find / | xargs chmod o= *' Nuts??