Re: [nottingham] Recursively act on a certain filetype?

From: Robert Davies (rob_davies@ntlworld.com)
Date: Wed 31 Jul 2002 - 15:14:47 BST


On Tuesday 30 July 2002 09:16, you wrote:
> On Tue, 30 Jul 2002, Paul Sladen wrote:

> > The `xargs' program takes the contents of stdin (data `piped' to it) and
> > put them on the command line, eg:
>
> > $ find ./ -name '*.o' | xargs rm
>
> Or you could just make use of the -exec switch to find, which allows
> clever things like:
>
> [root]# find /usr/src/widget/ -name \*.c -exec rm -f {} \; -name \*.o
> -exec mv {} /usr/src/widget-objects/
>
> ...and (this may vary from distro to distro) Paul missed the "-i" and "{}"
> from xargs, too:
>
> $ find ./ -name '*.o' | xargs rm
>
> becomes
>
> $ find ./ -name '*.o' | xargs -i rm {}
>
> and if you want to see what's being done:
>
> $ find ./ -name '*.o' | xargs -i -t rm {}

But you don't want to use the -i option and '{}' if you can help it as it
then run's one rm per file, which negates whole point of xargs(1).

The best way IMHO (print0 to permit \n in filenames) :

find dir -name '*.o' -print0 | xargs -t0 rm -f

When I had problems with some commands in past, little shell scripts to move
arguments about helped. Then mv(1), cp(1) etc got enhanced by some kewl
dude, to have the --target-directory=dir option, thus allowing you to have a
file list at _end_ of the line, and not have to prat about with -i and '{}'.

Rob
--------------------------------------------------------------------
http://www.lug.org.uk http://www.linuxportal.co.uk
http://www.linuxjob.co.uk http://www.linuxshop.co.uk
--------------------------------------------------------------------



This archive was generated by hypermail 2.1.3 : Wed 31 Jul 2002 - 15:16:22 BST