0

I have the following command in my bash script and want to redirect the output to /dev/null but it is not working. What is the right way to achieve this?

tar cvf - -C / $SOURCE_DIR | lz4 --best - $ARCHIVE > /dev/null

Edit:
Example output (I'm creating an archive of the /home/philip/test folder):

home/philip/test/  
home/philip/test/test.txt

I want the above output to be redirected to /dev/null.

--$SOURCE_DIR is the folder I'm archiving. Ex: home/philip/test
--$ARCHIVE is the destination path where the archive is saved. Ex: /home/philip/temp.tar.lz4

  • It's probably outputting to stderr. Try adding `2>&1` to the end of that line – Emanuel P Mar 07 '23 at 09:30
  • What's the point of using `tar -C /`? it's superflous when `$SOURCE_DIR` is an absolute path, and gives unexpected results when the `$SOURCE_DIR` is a relative one. – Fravadona Mar 07 '23 at 09:33
  • @Fravadona I was getting `remove leading '/'`, so I'm using `-C /`. After this there is no unexpected results. – Trevor Philip Mar 07 '23 at 12:02
  • @EmanuelP I am checking the exit code of the command, and it is 0, so I don't see why output would be redirected to stderr. Nevertheless, I did try `2>&1` and this also didn't work. – Trevor Philip Mar 07 '23 at 12:06
  • @TrevorPhilip Some programs write generic status messages to stderr, because they use stdout for the output file. It's a bad practice, but I've seen it many times. – Emanuel P Mar 07 '23 at 12:19

1 Answers1

1

Both commands might also errput messages when a problem occurs, so I don't recommend muting their stderr completely; I would just rework the command line in a way that limits their stderr to warnings/errors only:

tar cf - "$SOURCE_DIR" | lz4 -9 - > "$ARCHIVE"
Fravadona
  • 13,917
  • 1
  • 23
  • 35
  • I did try this as well, but I can still see the file names in the output that will be archived. The output is not being redirected to /dev/null. – Trevor Philip Mar 07 '23 at 12:12
  • 1
    @TrevorPhilip I don't think that you tried the command... removing the `-v` option of `tar` disables the file listing (and the `remove leading '/'` message); not providing a file name to `lz4` makes it silent. – Fravadona Mar 07 '23 at 12:44
  • 1
    Thanks, I was stupid not to execute the command. I didn't notice that `-v` option was removed. – Trevor Philip Mar 07 '23 at 15:35