Linux servers and many other servers with Unix-like operating systems rely on archiving and compression tools like tar and gzip to rotate logs, store backups, and even cache websites. In those cases, dedicated server’s software will automatically make use of those tools. If, however, you ever need to extract data from those archives or make your own, it is important to know how to properly use them.
To create a simple tar archive, you would use the following command:
tar cf archive_name.tar filename
That will archive a single file. To archive a directory, enter:
tar cf archive_name.tar directory
If you ever want to archive all of the files within a directory, navigate to the directory and then type:
tar cf archive_name.tar *
Archiving is great for creating collections of two or more files, but it leaves you with an uncompressed file that is the same size as the sum of all the files. To compress the files, you will need to use something like gzip, and you can call up gzip from within the tar command. For example, to compress a directory, you would type:
tar zcvf archive_name.tar.gz directory
The “c” tells it to create a tar archive. The “v” simply tells it to be verbose and show you what it is doing. The “z” tells it to compress the archive, and the “f” instructs it to use the filename you specify.
Uncompressing archives is just as easy. To extract the files of the archive_name.tar.gz file you just created, just type:
tar zxvf archive_name.tar.gz
Notice that we replaced the “c” for “create” with an “x” for “extract”.
- How to Uncompress Multiple Archives at Once
- How to Extract Specific Files from a Tarball
- How to Create Compressed Archives
- How to Delete All the Files in a Directory
- How to Remove Multiple Files from the Command Line