My current directory contains a single file like this-

$ls -l
-rw-r--r--    1 root     staff             8 May 28 09:10 pavan

Now, I want to tar and gzip this file like

$tar -cvf - *  2>/dev/null |gzip -vf9 > pavan.tar.gz 2>/dev/null

(I am aware I am creating the zipped file in the same directory as the original file)

When I run the above tar/gzip commands around 20 times, a few times I observe that the final tarred and zipped file pavan.tar.gz file has a ZERO sized pavan.tar.gz file. I am not sure from where is this zero sized file coming into the archive from.

Note: I am NOT running tar/gzip commands on an already existing tar.gz file. I always make sure that the directory has only one file before running the commands On googling, as described here, I suspected that the tar.gz being created was also part of the file being archived. But in my case, gzip is the one who's creating the final file and by the time gzip runs, tar should be done tarring.

This is happening on AIX but I've used Linux tag too, to draw more attention, as I guess the problem is platform independent.


You are entering a "piped" command. Your interpreter (bash etc.) creates a pipe and executes two commands (spawns 2 processes - tar & gzip). In this procedure bash forks multiple times and it is undefined if your target file pavan.tar.gz is created before or after * expansion in tar command.

| improve this answer | |
  • Thanks for the answer. As gzip is supposed to operate on the output of tar, shouldn't tar execute first? Can you pls point to me some standard docs that say that in a commands that are piped can be spawned in an implementation defined way? – Pavan Manjunath May 28 '14 at 17:16
  • @PavanM Here doc "Each command in a pipeline is executed in its own subshell...". So shell forks multiple times (twice in this case) and executes specified command (and expands anything needed). These new processes are not guarded anyhow for order of execution - they are just connected by the pipe. – palo May 29 '14 at 7:15

If you have only one file and you are worried because it can change in time you can use simply

cat pavan | gzip -f9 > pavan.gz 

Additional comment:

If you work with bash you can try to negate a selection so with !(pavan.tar.gz) you will select all the files but pavan.tar.gz

 tar -zcf pavan.tar.gz !(pavan.tar.gz)  

eventually with --ignore-command-error

| improve this answer | |

I don't understand why you use -v and then redirect that to dev/null ? remove the -v and dev/null like :

tar -cf - * | gzip -f9 > pavan.tar.gz

You won't see anything on the screen.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.