I am trying to take a directory and create an archive of it in my home directory from another location. I know that the -C option can be used for this, but tar seems to be ignoring it. I've tried $(basename $DEST)_$(date +%F_%H%M%S).tar.gz -C $HOME $(basename $DEST) where $DEST is the path to the directory I want to archive. Instead of creating the archive in my home directory, tar keeps creating it in the directory I'm executing it from. Is there anything wrong with the way I'm executing tar? Everything else seems to work properly, it's just that the -C flag is being completely ignored. I'm on Linux Mint 18.3 XFCE edition.

Edit: The full command is tar czf Pictures_$CREATION_TIMESTAMP.tar.gz -C /home/$USER Pictures. I was executing it from /home/$USER/coding/python_code/.


3 Answers 3


-C doesn't affect where the archive is created. It only affects which files are added to the archive. So, for example, given tar cvf foo.tar a -C /b c -C /d e, tar will add a from the current directory, switch to /b and add c, switch to /d and add e. foo.tar itself will be created in the current directory (where a was).

If no files are given on the command line for adding, but -C /some/dir is used, then tar will switch to /some/dir and add everything in it to the archive.

(Correspondingly, when extracting, -C doesn't affect where tar looks for the archive file. It only affects where the extracted files go to.)

So: tar czf Pictures_$CREATION_TIMESTAMP.tar.gz -C /home/$USER Pictures from /home/$USER/coding/python_code/ will always create the archive in /home/$USER/coding/python_code/, with the Pictures directory from /home/$USER.

If you want the Pictures directory from /home/$USER in an archive created in /home/$USER, you'd have to either cd to /home/$USER and create the archive:

cd "/home/$USER"; tar czf "Pictures_$CREATION_TIMESTAMP.tar.gz" Pictures

Or, specify the path to the archive:

tar czf "/home/$USER/Pictures_$CREATION_TIMESTAMP.tar.gz" -C "/home/$USER" Pictures
  • tar czf "/home/$USER/Pictures_$CREATION_TIMESTAMP.tar.gz" -C "/home/$USER" Pictures is what worked for me.
    – alyms108
    Commented Jun 14, 2018 at 2:50

From a more recent tar manual page:

-C, --directory=DIR

Change to DIR before performing any operations. This option is order-sensitive, i.e. it affects all options that follow.

So the correct way to archive a directory, for example /srv/www, is:

tar -C /srv/www -czf www.tgz

(Note that since you must begin with -C <directort> you need to say -czf with a - so that they are understood as options.)

Of course, you can always do the cd explicitly:

(cd /srv/www; tar czf - .) > www.tgz
  • I tried with the -C option first, but the archive still doesn't get made in my home directory. And I don't think I can use your second suggestion since I ultimately plan on using this in a script and the directory needing to be archived is already chosen.
    – alyms108
    Commented Jun 14, 2018 at 2:13

Thanks for the edit.

It looks like you're using shell variables and command substitution to specify the filename and the directory that you want to archive.

This will do what you need:

tar czfv $HOME'/Pictures_'$CREATION_TIMESTAMP'.tar.gz' "$DEST" -C Pictures

I added v to make it verbose so that you can see what's happening. The $HOME variable obviously expands to your home directory and I'm taking it that $CREATION_TIMESTAMP is a variable for the time. You stated that $DEST specifies path to the directory that you are trying to archive so it's at the end where it would normally be. I put double quotes around the $DEST in case the path contains spaces but if it doesn't then you can remove them. If $CREATION_TIMESTAMP is equal to what you have above $(date +%F_%H%M%S) then it won't have spaces so no double quotes are needed. The -C removes the nesting so that only Pictures is compressed.

  • This does work. The problem is with $DEST at the end; because it's an absolute path, the resulting archive has extra empty directories above "Pictures". Basically, instead of just archiving "Pictures" I end up archiving "/home/$USER/Pictures". That's why I wanted to change directories first so I could just use the directory name. The nesting isn't a huge issue, I just want to avoid it for the sake of neatness.
    – alyms108
    Commented Jun 14, 2018 at 2:24
  • @alyms108 You already accepted another answer but I added the -C in order to remove the nesting since you want it to be cleaner in your case. The command now only zips the Pictures directory and nothing else. Commented Jun 14, 2018 at 3:58
  • @alyms108 As you're running this from a script, you can add code to move into the directory that contains the directory that you want to archive and in that case, you won't need the -C. Commented Jun 14, 2018 at 10:26
  • An earlier iteration of the script did involve explicitly changing directories. I took it out because I wanted to see if everything (change directories and archive) could be done in the same command. Regardless of which method I go with, at least I've learned more about tar.
    – alyms108
    Commented Jun 15, 2018 at 21:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .