0

I want to redirect stdop of bzip command to logfile using tee command but its not working and giving error for '-a' in tee command. Please see error below,

> bzip2 file -c 1> tee -a logfile

bzip2: Bad flag `-a'
bzip2, a block-sorting file compressor.  Version 1.0.5, 10-Dec-2007.

   usage: bzip2 [flags and input files in any order]

   -h --help           print this message
   -d --decompress     force decompression
   -z --compress       force compression
   -k --keep           keep (don't delete) input files
   -f --force          overwrite existing output files
   -t --test           test compressed file integrity
   -c --stdout         output to standard out
   -q --quiet          suppress noncritical error messages
   -v --verbose        be verbose (a 2nd -v gives more)
   -L --license        display software version & license
   -V --version        display software version & license
   -s --small          use less memory (at most 2500k)
   -1 .. -9            set block size to 100k .. 900k
   --fast              alias for -1
   --best              alias for -9

   If invoked as `bzip2', default action is to compress.
              as `bunzip2',  default action is to decompress.
              as `bzcat', default action is to decompress to stdout.

   If no file names are given, bzip2 compresses or decompresses
   from standard input to standard output.  You can combine
   short flags, so `-v -4' means the same as -v4 or -4v, &c.

What is the issue? why bzip is considering the '-a' flag of tee command.

3 Answers3

0

Try:

bzip2 -c file | tee -a logfile

The | (pipe) is redirecting the stdout of the left command to the stdin of the right command.

-c is is an option from bzip2 that says Compress or decompress to standard output.. see man bzip2

mstruebing
  • 1,674
  • 1
  • 16
  • 29
  • Probably need `bzip2 -c` to output to stdout(the pipe). – nos Oct 23 '15 at 08:27
  • Its not working >bzip2 -c file | tee -a logfile BZh9rE8P . The output is BZh9rE8P and not compressing the file. In real scenario I can't change the '1> tee -a logfile' portion of command the reason is it used in a function ( as, $@ 1> tee -a logfile ) $@ is used to execute many commands in function. –  Oct 23 '15 at 08:30
  • If you want to compress the file in-place you must not use '-c', just `bzip2 file | tee `... Furthermore `1>tee -a logfile` is blatantly wrong and will always cause `-a logfgile` to be given to bzip2. – secolive Oct 23 '15 at 09:51
0

Your problem is that 1>does not pipe output of the bzip2 command to the tee command, but instead redirects the output to a file which will be named tee. Furthermore you probably don't want to use -c. You should be using the pipe | instead, as follows:

bzip2 file | tee -a logfile

Also, the reason why bzip2 is complaining is because the command as you mentioned above will be interpreted exactly as this one:

bzip2 file -a logfile 1> tee

And hence all arguments after the teeare actually added to the bzip2 command.

secolive
  • 499
  • 2
  • 7
  • Thanks for explanation. The command `bzip2 file -c | tee -a logfile` is also not working. It print some garbage line and does not create a compressed file. In real scenario I can't change the '1> tee -a logfile' portion of command above, the reason is it used in a function ( as, $@ 1> tee -a logfile ) $@ is used to execute many commands in function –  Oct 23 '15 at 09:46
  • You shouldn't be using -c I guess; updated post accordingly. Furthermore you should correct this function because it is obviously wrong. – secolive Oct 23 '15 at 09:53
0

As others have pointed out, you want a pipe, not output redirection:

bzip2 file | tee -a logfile

However, bzip2 doesn't produce any output; it simply replaces the given file with a compressed version of the file. You might want to pipe standard error to the log file:

bzip2 file 2>&1 | tee -a logfile

(2>&1 copies standard error to standard output, which can then be piped.)

chepner
  • 497,756
  • 71
  • 530
  • 681