kpimov
kpimov

Reputation: 13932

Redirect file to command, whose stdout is redirected to another command

In a bash script, I want to redirect a file called test to gzip -f, then redirect STDOUT to tar -xfzv, and possibly STDERR to echo This is what I tried:

$ gzip -f <> ./test | tar -xfzv

Here, tar just complains that no file was given. How would I do what I'm attempting?

EDIT: the test file IS NOT a .tar.gz file, sorry about that EDIT: I should be unzipping, then zipping, not like I had it written here

Upvotes: 1

Views: 371

Answers (1)

Pavel Gurkov
Pavel Gurkov

Reputation: 757

tar's -f switch tells it that it will be given a filename to read from. Use - for a filename to make tar read from stdout, or omit -f switch. Please read man tar for further information.

I'm not really sure about what you're trying to achieve in general, to be honest. The purpose of read-write redirection and -f gzip switch here is unclear. If the task is to unpack a .tar.gz, better use tar xvzf ./test.tar.gz.

As a side note, you cannot 'redirect stderr to echo', echo is just a built-in, and if we're talking about interactive terminal session, stderr will end up visible on your terminal anyway. You can redirect it to file with 2>$filename construct.

EDIT: So for the clarified version of the question, if you want to decompress a gzipped file, run it through bcrypt, then compress it back, you may use something like

gzip -dc $orig_file.tar.gz | bcrypt [your-switches-here] | gzip -c > $modified_file.tar.gz

where gzip's -d stands for decompression, and -c stands for 'output to stdout'.

If you want to encrypt each file individually instead of encrypting the whole tar archive, things get funnier because tar won't read input from stdin. So you'll need to extract your files somewhere, encrypt them and then tgz them back. This is not the shortest way to do that, but in general it works like this:

mkdir tmp ; cd tmp
tar xzf ../$orig_file.tar.gz
bcrypt [your-switches-here] *
tar czf ../$modified_file.tar.gz * 

Please note that I'm not familiar with bcrypt switches and workflow at all.

Upvotes: 1

Related Questions