[lug] Appending a file to files

rm at mamma.varadinet.de rm at mamma.varadinet.de
Wed Jan 17 02:49:53 MST 2001


On Tue, Jan 16, 2001 at 08:34:56AM -0700, Michael J. Pedersen wrote:
> On Tue, Jan 16, 2001 at 08:23:58AM -0700, Glenn Murray wrote:
> > I want to append the file foo to all the files in
> > the bar directory tree.  Why doesn't this work?
> > 
> > find bar -exec cat foo >> {} \;
> > 
> > What does work? 
> 
> The reason this fails is that the >> is considered part of the shell command,
> not part of the find command, nor even part of the exec. You should prolly
> have a file named {} in your directory (bar) by now. The way to prevent the
> shell from looking at it is actually very easy: eascape those characters. You
> can do this in one of two ways:
> 
> find bar -type f -exec cat foo \>\> {} \;
> 
> find bar -type f -exec cat foo '>>' {} \;
> 
> The first is the more likely to work (sorry, these are both untested, but
> should both work). You'll notice the '-type f' parameter. This is because if
> you don't have that, but do have a subdirectory under bar, it will try to cat
> the file foo as an append onto the subdirectory, which will fail. The '-type
> 'f will tell it to only do this with files.

Yet another solution:

  find .  -type f | xargs  -l1 bash -c 'cat  foo >> $0'
           ^                ^  ^
           |                |  |
	   1                2  3

Some comments: 

  1)  We don't want to mess with directories
  2) -l1 limits the number of arguments given to the program that it
      starts. Without this option xargs will put as many arguments onto
      the commandline as the OS supports. CAVEAT: There should be no space
      inbetween the switch and the number.
  3)  We do need a shell for the redirection. While this might look a bit
      strange i think it's much less messy than trying to get the escape-
      ing right without an expicit shell.

 Ralf






More information about the LUG mailing list