5

I have the following folders on a linux machine

./myFolder
    ./tmp
    ./packages
    ./zips
    compress.sh

I run tar -czf ./zips/someFile.tar.gz ./tmp/someFolder from compress.sh. The folder is created, but the resulting nesting occurs inside the zip file:

. -> tmp -> someFolder

How can I zip this file so that when I open it, it's just someFolder, and not someFolder inside tmp, inside .?

5

You can try following:

tar -czf ./zips/someFile.tar.gz -C ./tmp/ someFolder
3

You can use the parameter -C or --directory

tar -czf ./zips/someFile.tar.gz -C ./tmp someFolder

From man tar

-C, --directory DIR
      change to directory DIR

Example

% ls -og
total 4
-rw-rw-r-- 1    0 Mai 21 18:39 bar
drwxrwxr-x 2 4096 Mai 21 18:39 foo

tar -cvf ../sample.tar -C /home/user/tmp .

tar -tvf ../sample.tar 
drwxrwxr-x user/user 0 2015-05-21 18:39 ./
-rw-rw-r-- user/user 0 2015-05-21 18:39 ./bar
drwxrwxr-x user/user 0 2015-05-21 18:39 ./foo/
  • Nope, -C sets the current working directory, someFolder should be written relative to it. – lcd047 May 21 '15 at 16:42
3

The tar command does not zip. Not even with the -z flag. However, it does collect a series of files/folders and optionally compress the result. To contrast, zip compresses each file and adds it to an archive. zip and tar use different compression algorithms.

The man page for GNU tar shows the -C flag (--directory) to change directory, so you could do this

tar czfC zips/someFile.tar.gz tmp someFolder

You could also use the --transform option to remove the leading tmp/

tar czf zips/someFile.tar --transform 's#^tmp/##' tmp/someFolder

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.