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 12:00:01 IST 2010


On 5 February 2010 18:51, Dotan Cohen <dotancohen at gmail.com> wrote:
>> 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).
>>
>
> Actually, I am piping it through openssl to encrypt it first. That is
> the reason for the non-conventional output. See here:
>
> tar -zcvf - *  --exclude-from $EXCLUDES  | openssl des3 -salt -k $1 |
> dd of=$(hostname)-$(date +%Y%m%d).tbz
>
> I tried appending " > OUTPUT" to the end in the hope that it would
> capture the output to a variable with the name OUTPUT but that did not
> work. So, How should I be doing that?

OK, fair enough, but why do you need the 'dd' at the end?:

tar -zcvf - --exclude-from $EXCLUDES * 2>STDERR | openssl des3 -salt
-k $1 > $(hostname)-$(date +%Y%m%d).tbz

should output tar's stderr to STDERR and tar's output will be piped to openssl.
Notice how I moved the "*" to the end of tar's command line, btw.

If you want to redirect the entire command line output to STDERR use
parenthesis around it:

(tar -zcvf - --exclude-from $EXCLUDES * | openssl des3 -salt -k $1 >
$(hostname)-$(date +%Y%m%d).tbz) 2>STDERR


>
>
>> 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`
>>
>
> This is something that I cannot do because of the pipe to encryption.

I don't see the connection between piping vs. specifying an output
file and putting the list of files to include in the tar at the end of
the line.

Cheers,

--Amos



More information about the Linux-il mailing list