Skip to content

Instantly share code, notes, and snippets.

@huzaifa-a
Last active July 6, 2021 08:37
Show Gist options
  • Save huzaifa-a/d4fbf81754753b7cb67567962b307317 to your computer and use it in GitHub Desktop.
Save huzaifa-a/d4fbf81754753b7cb67567962b307317 to your computer and use it in GitHub Desktop.
Compress files on current directory

To tar and gzip a folder, the syntax is:

tar czf name_of_archive_file.tar.gz name_of_directory_to_tar

The - with czf is optional.

If you want to tar the current directory, use . to designate that.

To construct filenames dynamically, use the date utility (look at its man page for the available format options). For example:

cd /var/www &&
tar czf ~/www_backups/$(date +%Y%m%d-%H%M%S).tar.gz .

This will create a file named something like 20120902-185558.tar.gz.

On Linux, chances are your tar also supports BZip2 compression with the j rather than z option. And possibly others. Check the man page on your local system.

-c, --create
      create a new archive
-x, --extract, --get
      extract files from an archive
-v, --verbose
      verbosely list files processed
-z, --gzip
      filter the archive through gzip
-j, --bzip2
      filter the archive through bzip2
-J, --xz
      filter the archive through xz
-f, --file=ARCHIVE
      use archive file or device ARCHIVE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment