Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
1  
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 at 13:10
add comment

3 Answers 3

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
add comment

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
1  
For tar to use STDOUT, you must specify it: tar -zcvf - /home/jerry/prog : as the -f option has been set. –  Ouki Mar 6 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 at 13:18
add comment

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

#!/bin/bash

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

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

return 0
}
EXPECTED_ARGS=( 1 )
E_BADARGS=65

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

FILEPATH=${1%/}
filename="${FILEPATH##*/}"

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

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

share|improve this answer
add comment

Your Answer

 
discard

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.