61

I'd like to create a tar file to compress a folder that contains sub folders. I'm trying with the following command int in the terminal:

tar -czf folder directorios.tar.gz

directorios.tar.gz would be the result

andrew.46
  • 37,085
  • 25
  • 149
  • 228
Sam
  • 655
  • 1
  • 5
  • 9

1 Answers1

94

Try:

tar -czvf directorios.tar.gz folder

A few notes:

  1. Recursion is the default, from the tar man pages:

    -c, --create
        Create a new archive.  Arguments supply the names of the files to be archived.
        Directories  are  archived  recursively,  unless  the --no-recursion option is
        given.
    

    Although this can be turned off by using the --no-recursion option...

  2. You need the archive name immediately after the -f option, the correct sequence being:

    tar -c [-f ARCHIVE] [OPTIONS] [FILE...]
             ^^^^^^^^^^
    
  3. For a more flexible command line (particularly if you wanted to use other compression utilities apart from gzip with tar) you could omit the -z option and use -a or --auto-compress option to allow tar to automatically decide which compressor to use based on the archive suffix:

    -a, --auto-compress
        Use archive suffix to determine the compression program.
    

    Recognised suffixes (and their attendant compressing applications) are:

    • .gz : gzip
    • .tgz : gzip
    • .taz : gzip
    • .z : compress
    • .taZ : compress
    • .bz2 : bzip2
    • .tz2 : bzip2
    • .tbz2 : bzip2
    • .tbz : bzip2
    • .lz : lzip
    • .lzma : lzma
    • .tlz : lzma
    • .lzo : lzop
    • .xz : xz
    • .zst : zstd
    • .tzst : zstd

tar is pretty cool :)

References:

andrew.46
  • 37,085
  • 25
  • 149
  • 228
  • Is directorios.tar.gz a gzip tarfile? I have red that -cz creates a gzip and -v(verbose) shows the process. – Sam Oct 09 '16 at 03:28
  • @sam Indeed, my apologies, I have added this in. You could omit the `-z` option and use `-a` to allow tar to guess from the archive suffix.... – andrew.46 Oct 09 '16 at 03:50
  • @sam OK I have bulked up the answer considerably, hopefully some useful additions in there for you :) – andrew.46 Oct 09 '16 at 03:59
  • I used both `-z` and `-a` and didn't notice any difference(i'm working on a remote server). Both tars weight the same. In the exercise I'm asked to create a gzip, then decompress it using gunzip. Thanks for all the help! – Sam Oct 09 '16 at 04:04
  • 1
    **Add date&time&timezone to the zip file name** `tar -zcf "yourzipname_$(date '+%Y-%m-%d_%H-%M-%S%z(%Z)').tar.gz" your_source_folder` the `+` is just a "start of the format"), I've also removed the `v` if you don't need "verbose" output which mainly outputs ALL files being added x) – jave.web Jan 28 '21 at 19:32