Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to use tar to compress a folder without giving the filename of the archive to store in?

Normally you use:

tar -zcvf prog-1-jan-2005.tar.gz /home/jerry/prog

I want to do something like

tar -zcvf /home/jerry/prog

and have it create prog.tar.gz

share|improve this question
Unless playing with a wrapper around tar, you cannot do that. tar has usage and options: this is not your average winzip. – Ouki Mar 6 '14 at 13:10

as far as I know, it is not possible, mainly because the "destination name" argument comes first. If you ommit it, then the "source" argument is placed instead of the destination one and it confuses tar.

If your needs are for a script where you do not know the archive name to create, you could consider parsing your path and create a name to provide to the tar command.

share|improve this answer

You can't use tar this way. Or you need to patch it.

If you don't give a file (you don't use the -f option), tar will use the standard output by default, i.e. the terminal and eventually fail because it will refuse to write compressed data on the terminal.

So, you have to call tar the proper way : tar -zcvf prog.tag.gz /home/jerry/prog.

share|improve this answer
For tar to use STDOUT, you must specify it: tar -zcvf - /home/jerry/prog : as the -f option has been set. – Ouki Mar 6 '14 at 13:13
You're right! I meant without the -f option. If you give the -f option and no file, tar will just fail. – lgeorget Mar 6 '14 at 13:18

Based on @Laurent's suggestion I wrote this bash script:


function isIn()
if [ -z "$1" ]; then

for i in "${EXPECTED_ARGS[@]}"; do
    if [ "$i" == "$1" ];then
        return 1

return 0

if isIn $# ; then
  echo "Usage: $0 filepath"
  exit $E_BADARGS


tar -zcvf "$filename.tar.gz" "$FILEPATH"

I'm new to this so if you see anything wrong please contribute.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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