Tar compress folder without specifying the filename

8,787

Solution 1

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.

Solution 2

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:
8,787

Related videos on Youtube

Mark Robinson
Author by

Mark Robinson

Updated on September 18, 2022

Comments

  • Mark Robinson
    Mark Robinson over 1 year

    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

    • Admin
      Admin about 10 years
      Unless playing with a wrapper around tar, you cannot do that. tar has usage and options: this is not your average winzip.
  • Ouki
    Ouki about 10 years
    For tar to use STDOUT, you must specify it: tar -zcvf - /home/jerry/prog : as the -f option has been set.
  • lgeorget
    lgeorget about 10 years
    You're right! I meant without the -f option. If you give the -f option and no file, tar will just fail.