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

  • 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 '14 at 13:10

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.

  • 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 '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

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.


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.

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.