Posted by & filed under Linux.

  • DZone
  • Reddit
  • HackerNews
  • Twitter
  • Facebook
  • Google Plus
  • Pinterest
  • StumbleUpon
  • LinkedIn
  • Tumblr
  • BlinkList
  • Mister Wong
  • Add to favorites
  • Email

Looking on my block at the end of the year i see than nearly two years ago i wrote about extracting archives under Linux but not about putting files in to archives. Now a have some time to continue.

Tar.gz

Here are some common way to create your archives.

#Creates simple targetfile.tar without compression.
tar cvf targetfile.tar sourcedir/*

#Zip everything beneath sourcedir to targetfile.tar.gz
tar cvzf targetfile.tar.gz sourcedir/*

#Bzip2 everything beneath sourcedir to targetfile.tar.bz2
tar cvjf targetfile.tar.bz2 sourcedir/*

Parameters explanation:

  • c  or –create create a new archive
  • v or –verbose  verbosely list files processed
  • z or –gzip  usage of gzip compression (or also decompression, context dependent)
  • j or –bzip2 usage of bzip2 compression
  • f or –file  use archive file

Alternative with pipe usage:

tar -cf - sourcedir | gzip -c > filename.tar.gz

Zip

Some examples

#Zip every file in current directroy to file.zip.
#But hidden files like (.htaccess) are not included.
zip file.zip sourcedir/*

#also includes hidden files.
zip file.zip sourcedir/* .*

The above examples include directories but still not their content recursively, -r option is required.

#Adds all files and directories recursivly.
zip file.zip -r /sourcedir/*

#Same as abowe with addtional enryption and password lock.
#Password is prompted on the terminal.
zip file.zip -re /sourcedir/*

#Splitts creted archive to parts not bigger than 2 Gigabytes.
zip -s 2g -r test.zip ./*

Hope that helps someone.

Happy new year!!!

0 comments