While working with remote servers it is not unusual to require moving large numbers of files at once. As such, using compression and archival tools can be extremely useful. There are a number of archival/compression tools available.
A few of the most popular ones and the ones we’ll be looking at today in this tutorial are Tar, Tar+Gunzip, BZip2, and Zip.
Each of these compression algorithms can be run remotely and locally on Linux and Mac servers and local machines in an SSH session. Allowing for mass file migration on the fly.
Note: The following command line examples are platform independent, except for Zip commands.
Tar is an excellent tool for compiling and compressing content. With this command, in combination with a number of options, you can create, maintain and extract files archived in the tar format. Tar actually stands for tape archive. Developed in the early days of Unix, Tar was in backing up computer files to tape-based storage devices. Today it is used to collect, distribute and archive files while preserving file system attributes.
To use the tar command, navigate to the working directory containing the folder you wish to archive and issue the command tar followed by the relevant options, the name of the archive you wish to create and then the name of the folder you wish to archive. Assuming you wish to compress the folder “/collapse” in the root folder “/example”, enter the following command:
tar -c -f collapse.tar collapse/
In the example above the options used were c and f. The “c” switch creates a new archive and the “f” switch declares an archive file.
Other options are as follows:
|Append tar files to an archive.
|Calculate any differences between the archive and the file system.
|Delete from the archive.
|Append files to the end of a tar archive.
|List the contents of an archive.
|Test the archive label, and exit.
|Append files, but only those that are newer than the copy in the archive.
|Extract files from an archive.
The above command will compress the folder collapse (with everything inside it) that’s in the current working directory to create a single file called collapse.tar.
In instances where you would like to archive several files instead of a single folder, slightly alter the syntax of the same command, listing the desired files separated from each other with a single space instead of the single folder.
tar -c -f collapse.tar file1.doc file2.php file3.c
This command will compile the three files (file1, file2, and file3) into a single file called collapse.tar.
To extract a tar file simply state the command followed by the “x” switch and the archive file. For example:
tar -x -f collapse.tar
Tar+Gunzip provides more advanced options with better compression. To use it we’ll simply add two new options. The Tar+Gunzip syntax is as follows:
tar -cvzf collapse.tar.gz collapse
As you can see, we’re still using the tar command. However, we now have the “v” and “z” switches. The “z” switch is what actually instructs tar to create the archive as a gzip archive. The “v” switch tells tar to compress the folder in verbose form.
What’s happening here is that tar is packaging all the files in the folder into one collective file, while Gzip is applying its superior compression algorithm to the file.
Extracting gzip files which have been created in this manner is, however, a two-fold process.
First, the file needs to be decompressed. This is done by executing the gunzip command on the file. Example:
This removes the compression from the file and renders is a .tar file. Now the tar extract function must be run to separate the files:
tar -xvf collapse.tar
The Bzip archive tool while being a little different actually uses the same command as Tar file: tar. To use the tar command for Bzip files simply include the “j” option. Now the example command changes to:
tar -c -f -j collapse.bz2 collapse/
And, just as stated before, to extract the file simply change the “c” switch to “x”.
tar -x -f -j collapse.bz2
The classic archive format is .zip. It is most popular with Windows users given programs like Winzip and Windows native support of zip files. You can uncompress files in the ZIP format using the command unzip and, respectively, you can compress content into a zip file with the command zip. The compression power of the ZIP command is, however, less efficient when compared to GZIP or BZIP2, so a file in the zip format containing the same contents will be larger in size. To pack a zip archive just use the zip command followed by the name of the file (including the .zip extension).
Note: Zip is not native to Windows so the follows commands may not apply, unless a Zip command line feature has been installed. The native function for Windows is Compress, however that function is outside the scope of this article.
zip collapse.zip collapse/
As mentioned before, to extract a zip file, change the syntax to run the unzip function instead.
Using this function will uncompress the zip file, placing its contents, in the current working directory.
Check out the top 3 Best web hosting services
- You can discover new info about Best website hosting by clicking this link.