How to tar all files in a directory into one compressed file

Tricks and tips for new and novice UNIX and Linux users. Please feel free to add your own favourite command or tip here.

How to tar all files in a directory into one compressed file

Postby UnixMan » Tue Feb 02, 2010 9:30 am

Sometimes, for instance for the purposes of a backup, it is useful to 'archive' all files in a directory into one big file, which can then be copied onto an external harddrive, DVD, etc. Better still if the 'one big file' can be compressed at the same time, to save space.

If you have a directory named 'data' within the current working directory, use:
Code: Select all
tar -cvzf data.tar.gz data/


The tar options in use are as follows:

-c : create an archive
-v : verbose - print filenames as they are done
-z : zip - compress the archive
-f : the filename of the resulting archive, which in this case will be data.tar.gz. The 'tar' extension indicates that it's a 'tar' archive, and the 'gz' indicates that it's compressed.

To restore an archive, go to the directory where you want the data to be restored to, and use:
Code: Select all
tar -xvzf data.tar.gz newdata/


The data will be extracted (the -x option) into the specified directory. Be careful to make sure you don't overwrite any data that is already in that directory - be careful when using powerful commands such as this!
UnixMan
Site Admin
 
Posts: 103
Joined: Sat Jul 31, 2004 11:13 am

Advertisement

Return to Tips and Tricks - beginners / novice

Who is online

Users browsing this forum: No registered users and 1 guest

cron