I have never needed to use any other options for tar other than -cvzpf. Is there some way to set that as the default behavior?

While I recognize that it is often impossible to say why someone wrote a program one way as opposed to another, I do not understand why tar doesn't do things like most other command line utilities, i.e.:

command -options /source/path /target/path

Why is it instead:

command -options /target/path /source/path
  • 4
    "I have never needed to use any other options for it" So you never use the archives you create? – fkraiem Nov 27 '14 at 15:09
  • I never use tar to open them. – CommaToast Nov 27 '14 at 15:12
  • You can do tar -cvzp /source/path -f /target/path if you so desire, or even tar -cvzp /source/path > /target/path. Please first investigate what the options you're typing mean before complaining about them. – wurtel Nov 27 '14 at 15:13
  • I know what it can do, and I've investigated quite a bit. My question is: why is it the default behavior? I'm not trying to complain, nor am I trying to find out if there's a way to have it not do the default. I'm assuming there is a valid reason for it to be the default, which would be useful for me to know. Why don't you at least attempt to answer the question instead of curmudgeonly downvoting it without even trying? – CommaToast Nov 27 '14 at 15:15
  • 2
    Your "target path" here is an argument to the -f option, that is why the order is this way. A usual way to use tar is to pipe its output to whatever compression program you want to use, that's why the default is to not require a target file. Not everyone want Gzip (or one of the few provided as options). – Leiaz Nov 27 '14 at 16:03

-cvzpf is not the default behavior for at least the following reasons.

  1. -c specifies creating an archive, it is at least equally likely that one will want to extract an archive or view the contents of an archive.
  2. -v specifies verbose operations, some people don't want to see everything
  3. -p this is irrelevant for creating archives.
  4. -f in case the user wants to pipe the output to a different device/program instead of a file (or to the default tape device in traditional Unices).

Regarding why it is not how you suggest it should be, it is historic reasons dealing with its use with tape drives and the original authors coding.

Regarding making that the default behavior, you could create an alias however, you would need a separate one for extracting files.

A separate way to change the default options with the GNU implementation of tar is by setting the TAR_OPTIONS environment variable. Though I have found that it does not like it when you try to specify -f as one of the options.

export TAR_OPTIONS=-tvzp

Note that while you can set the options, this will cause an error if you pass tar a conflicting option. For instance, if you have TAR_OPTIONS set as above and you try to extract an archive, you will get the following error.

tar: You may not specify more than one `-Acdtrux' or `--test-label' option
Try `tar --help' or `tar --usage' for more information.
| improve this answer | |
  • Awesome answer! That's just exactly what I was looking for. It's funny how many things make a lot more sense when you consider tape. Like for example, how C has .h and .m files. Consider that it takes forever to read very far into the tape, and now you're super glad that the developer kept the qty of publicly exposed stuff to a bare minimum. – CommaToast Nov 27 '14 at 21:07

I do not know the answers to the two questions about tar, but I can tell you how to set the default behaviour of any command line tools.

Using the alias command, you can replace any word in a shell by a string. The exact use of alias depends on the shell you use. The following example works in the bash shell:

alias tar='tar -cvzpf'

This will replace the word tar with "tar -cvzpf". In order for bash to "remember" this, the alias command needs to be placed in a script that is executed at the start of the bash shell, for example .bashrc. (Some users like to collect all aliases in a seperate script, e.g. .alias, and call that script from .bashrc.)

In the csh/tcsh shell, you omit the = and use double quotes " instead of single quotes. To find out which shell you are using, type echo $SHELL.

| improve this answer | |
  • Single quotes are find with tcsh. Actually they're fine with most shells while double quotes for instance are non-special characters in shells of the rc family (which don't have alias though). – Stéphane Chazelas Nov 27 '14 at 15:28

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.