Piping find into tar...

Robert Bonomi bonomi at mail.r-bonomi.com
Wed May 4 13:04:05 UTC 2011

> From owner-freebsd-questions at freebsd.org  Wed May  4 02:26:32 2011
> Date: Wed, 4 May 2011 01:25:39 -0600
> From: Modulok <modulok at gmail.com>
> To: FreeBSD Questions <freebsd-questions at freebsd.org>
> Subject: Piping find into tar...
> List,
> I've been playing with the find command lately. Is there a way I can pipe 
> the putput list of files from find, into the tar command to create an 
> archive which contains the files which find lists? I tried the following, 
> but it didn't work
> (obviously).
> find -E . '.*\.txt$' -print | tar -cjf result.tgz

You're asking 'the wrong question'.  

tar _requires_ the filenames to  be listed as parameters to the command.

There are at least four ways to accomplish this, given the specific 
example you show.
1) The simplest: tar -cjf result.tbz .*.txt *.txt
2) in-line substitution: tar -cjf result.tbz `find -E . '.*\.txt$' -print` 
3) using the '-T' option:
     find -E . '.*\.txt$' -print0 | tar -c -j --null -T - -f result.tbz
3) using xargs:  
     find -E . '.*\.txt$' -print0 | xargs tar -rjf result.tar; bzip2 result.tar

Options 1) or 2) will fail 'immediately', if the pattern expands to an 
excessively long set of filenames.

More information about the freebsd-questions mailing list