How to redirect output to a file while creating tar in Solaris?

14,785

The output of tar cvf - is the archive. By specifying - as the argument to f, you're telling tar to output the archive on stdout (and in that case, because of the |gzip, tar's stdout is a pipe to gzip).

The verbose output (the list of files) which you asked for with v goes on stderr like error messages because it can't go on stdout as that would go to gzip and into the tar.gz file. Also note that your 2> error.log only redirects gzip's stderr.

If you don't want the verbose file list (what I assume you mean by output), then just omit the v. And redirect a subshell or command group's stderr if you want the error messages of all of cd, tar and gzip (and the shell's opening of the output file) to go to the log file:

(cd /ebs/datatop &&
   tar cf - . | gzip > /ebs/backup/proddata.tar.gz) 2>> error.log

I also replaced * with . to archive the current directory. * would only expand to the non-hidden files and would cause problems with some file names.

The error.log will be stored in the current working directory prior to that cd. In your approach, that error.log would have ended up being stored in the tar.gz file.

If you wanted to redirect that file list (with v or vv) to some file, and the errors to some other file, you'd need to use an argument other than - to the f flag. For instance using this syntax on systems with support for /dev/fd/n:

(tar cvvf /dev/fd/3 . 3>&1 > ../file.list | gzip > ../file.tar.gz) 2> ../error.log

Above, /dev/fd/3 still refers to the pipe to gzip (as we've taken care of redirecting the file descriptor 3 to it (with 3>&1) before redirecting stdout to ../file.list), but since we're no longer telling tar to send the archive on its stdout, tar is free to write the file list on stdout (which we redirect to ../file.list).

Share:
14,785

Related videos on Youtube

Girish Sunkara
Author by

Girish Sunkara

Updated on September 18, 2022

Comments

  • Girish Sunkara
    Girish Sunkara over 1 year

    I am creating a tar.gz file in Solaris using below command for redirecting output.

    cd /ebs/datatop && tar cvf - * | gzip -c > /ebs/backup/proddata.tar.gz >> /dev/null 2>> error.log
    

    When executed, it is creating /ebs/backup/proddata.tar.gz as an empty file. Does not give any errors. I am assuming tar files are being redirected to /dev/null. I want to redirect only the command output to /dev/null and write errors to error.log.

  • Andrew Henle
    Andrew Henle about 7 years
    Does Solaris tar support multiple v options?
  • Stéphane Chazelas
    Stéphane Chazelas about 7 years
    @AndrewHenle, while it accepts as many v as you'd like, that doesn't change the output (same ouput for vv, v, vvv...). So yes, you're right cvvf is not useful there unless you have GNU tar, star, or bsdtar available. Solaris tar still has a different output between tar tf and tar tvf though.