Don't create the tar file in the directory you are packing up:
tar -czf /tmp/workspace.tar.gz .
does the trick, except it will extract the files all over the current directory when you unpack. Better to do:
cd ..
tar -czf workspace.tar.gz workspace
or, if you don't know the name of the directory you were in:
base=$(basename $PWD)
cd ..
tar -czf $base.tar.gz $base
(This assumes that you didn't follow symlinks to get to where you are and that the shell doesn't try to second guess you by jumping backwards through a symlink - bash
is not trustworthy in this respect. If you have to worry about that, use cd -P ..
to do a physical change directory. Stupid that it is not the default behaviour in my view - confusing, at least, for those for whom cd ..
never had any alternative meaning.)
One comment in the discussion says:
I [...] need to exclude the top directory and I [...] need to place the tar in the base directory.
The first part of the comment does not make much sense - if the tar file contains the current directory, it won't be created when you extract file from that archive because, by definition, the current directory already exists (except in very weird circumstances).
The second part of the comment can be dealt with in one of two ways:
- Either: create the file somewhere else -
/tmp
is one possible location - and then move it back to the original location after it is complete.
- Or: if you are using GNU Tar, use the
--exclude=workspace.tar.gz
option. The string after the =
is a pattern - the example is the simplest pattern - an exact match. You might need to specify --exclude=./workspace.tar.gz
if you are working in the current directory contrary to recommendations; you might need to specify --exclude=workspace/workspace.tar.gz
if you are working up one level as suggested. If you have multiple tar files to exclude, use '*
', as in --exclude=./*.gz
.