Excluding a list of files from tar with spaces in filenames

Excluding a list of files from tar with spaces in filenames

Amos Shapira amos.shapira at gmail.com
Fri Feb 5 02:15:51 IST 2010


On 5 February 2010 08:11, Dotan Cohen <dotancohen at gmail.com> wrote:
> Thanks, Meir, I just got this to work:
>
> EXCLUDES=`tempfile`
> cat >$EXCLUDES <<EOLIST
> $DONT_COPY
> EOLIST
>
> tar -zcvf - *  --exclude-from $EXCLUDES > out.tar
>
> rm -f $EXCLUDES
>
>
>
> By the way, because of the "v" flag, tar outputs all the directories
> that it copies to the terminal (even though this is running in a
> script). How can I capture that output to a variable or to another
> temp file?

Do

tar -zcvf - *  --exclude-from $EXCLUDES > out.tar 2> tar.err

to redirect stderr into a file.

I just noticed that the way you specify the output tar file is not
conventional. You tell it to use stdout then redirect it to a file
using the shell, instead you can tell tar to open the file directly:

tar -zcvf out.tar *  --exclude-from $EXCLUDES 2> tar.err

(the 'f' in '-zcvf' tells tar to take the next command line argument
as an output (or input, depends on the command) file name, '-' stands
for stdout or stdin).

Another error I noticed is that you provide the --exclude-from after
the '*' which expands to input file names.
Then you can redirect stderr to stdout in order to fetch it using back-ticks:

tar_stdout_and_stderr=`tar -zcvf out.tar --exclude-from $EXCLUDES * 2>&1`

--Amos



More information about the Linux-il mailing list