14.3. File compression and archiving
Sometimes we need to store a set of files in a single file for backup or transfer to another directory or even another computer. Sometimes we also need to compress files into one file, so they use only a small amount of disk space and can be downloaded faster over the Internet.
Understanding the differences between archive files (archive file) and compressed files (compressed file) is important to users. An archive is a collection of files and directories that are stored in a file. The archive is not compressed-the disk space it uses is the sum of all the files and directories in it. A compressed file is also a collection of files and directories, and the collection is stored in a file, but it is stored so that it consumes less disk space than the sum of all the files and directories in it. If you don't have enough disk space on your computer, you can compress files that you don't use frequently, or that you no longer use but want to keep. You can even create an archive and then compress it to save disk space.
|
Notes |
|
|
|
The archive file is not a compressed file, but a compressed file can be an archive file. |
14.3.1. Using the File Packager
Red Hat Linux includes a graphical compression tool, " File Packager ." It can compress, decompress, and archive files and directories. The file Packager supports common UNIX and Linux file compression and archive formats, and it has a simple interface and rich documentation. It is also integrated into the desktop environment and the graphical file manager, making it easier to work with the archive.
To start the File Packager, click the "main Menu" => " attachment" => "File Packager." You can also start the File Packager by typing File-roller from the shell prompt. Figure 14-1 shows the running File Packager .
|
Trick |
|
If you are using a File manager (such as Nautilus), you can start the File Packager by double-clicking the file you want to release or extract. The Browse window for the File packager appears with folders that show you the files you want to extract or release from the archive so you can extract or browse. |
figure 14-1. running File Packager 14.3.1.1. Use the File Packager to extract or release the archive
To cancel the archive or (and) Extract the file, click the "open button. A File menu pops up, allowing you to select the archive you want to manipulate. For example, if you have a foo.tar.gz file in your home directory, highlight the file, and then click . The file appears as a folder in the main browsing window of the , and you can double-click the folder icon to flip through the contents. The file packager retains all the original directory and subdirectory structures, which makes it easy for you to find a particular file in the archive. You can click the "extract button, select the directory where you want to save the files that were released from the archive, and then click " OK button to extract one of the files or the entire archive. 14.3.1.2. Use the to create an archive
If you need to free up some hard disk space, or send multiple files or all the files in a directory to another user, the file packager allows you to create files and directories. To create a new archive, click the New button on the toolbar. A file browser pops up, allowing you to specify the archive name and compression technology. For example, you can select the tar.gz in gzip format from the Drop-down menu and type the name of the archive you want to create. by Clicking OK, you can add files and directories to the new archive. To add a file to your new archive, click "Add", which will pop up a browse window ( figure 14-2), where you can find files or directories to put in the archive. When you are finished, click OK and close to close the archive.
figure 14-2. Using the File Packager to create the archive
|
Trick |
|
The tasks that the file packager can perform are more than just those mentioned here. Please read the documentation for the File packager (click "Help" => "Manual") for more information. |
14.3.2. Compress files at the shell prompt Compressed files use less disk space and are faster to download than uncompressed large files. The file compression tools you can use in Red Hat Linux are: gzip, bzip2, and zip.
It is recommended that you use the BZIP2 compression tool because it provides maximum compression and can be found on most UNIX-like operating systems. The GZIP compression tool can also be found on UNIX-like operating systems. If you need to transfer files between Linux and other operating systems such as MS windows, you should use zip because the command is most compatible with the compression tools on Windows.
Compression Tools |
file name extension |
Decompression Tools |
Gzip |
. gz |
Gunzip |
Bzip2 |
. bz2 |
Bunzip2 |
Zip |
. zip |
Unzip |
table 14-1. Compression Tools
By conventions, files that are compressed with gzip extensions are. gz; files that are compressed with bzip2 are. bz2, and zip-compressed files with the extension. zip.
Files that are compressed with gzip can be decompressed using gunzip, bzip2 compressed files can be extracted using BUNZIP2, and zip-compressed files can be decompressed using unzip. 14.3.2.1 bzip2 and bunzip2.
To compress the file using BZIP2, type the following command at the shell prompt:
The file is compressed and saved as a filename.bz2.
To extend a compressed file, type the following command:
The filename.bz2 file is deleted, followed by filename.
You can use the BZIP2 command to work with multiple files and directories at the same time by listing them individually and spacing by space:
The above command compresses the contents of the File1, File2, File3, and/usr/work/school directories (assuming this directory exists) and puts them into the filename.bz2 file.
|
Trick |
|
To get more information about the two commands, type man bzip2 and man bunzip2 at the shell prompt to read the instructions page for the bzip2 and BUNZIP2 commands. |
14.3.2.2. Gzip and Gunzip To compress the file using gzip, type the following command at the shell prompt:
The file is compressed and saved as a filename.gz.
To extend a compressed file, type the following command:
Filename.gz will be deleted, followed by filename.
You can use the gzip command to work with multiple files and directories at the same time by listing them individually and spacing by space:
The above command compresses the contents of the File1, File2, File3, and/usr/work/school directories (assuming this directory exists) and puts them into the filename.gz file.
|
Trick |
|
For more information about the two commands, type man gzip and man gunzip to read the instructions page for the gzip and Gunzip commands at the shell prompt. |
14.3.2.3. zip and Unzip To compress the file using Zip, type the following command at the shell prompt:
Zip-r Filename.zip Filesdir |
In this example, Filename.zip represents the file you created, and Filesdir represents the directory where you want to place the new zip file. The-r option specifies that you want to include all files included in the Filesdir directory recursively (recursively).
To extract the contents of a zip file, type the following command:
You can use the zip command to work with multiple files and directories at the same time by listing them individually and spacing by space:
The above command compresses the contents of the File1, File2, File3, and/usr/work/school directories (assuming this directory exists) and puts them into the Filename.zip file.
|
Trick |
|
For more information about the two commands, type man zip and man unzip at the shell prompt to read the instructions page for the zip and unzip commands. |
14.3.3. To file files under Shell hints The tar file is a collection of several files and/or directories in one file. This is the best path to create backup and archive.
The options used in tar are:
-c-Create a new archive.
-f-when used with the-C option, the created tar file uses the filename specified by this option, and the archive specified by this option when used with the-X option.
-t-displays a list of files included in the tar file.
-v-Displays the archive progress of the file.
-x-extracts files from the archive.
-z-uses gzip to compress the tar file.
-j-uses bzip2 to compress the tar file.
To create a tar file, type:
TAR-CVF Filename.tar Directory/file |
In the above example, Filename.tar represents the file you created, Directory/file represents the files and directories you want to put in the archive.
You can use the tar command to work with multiple files and directories at the same time by listing them individually and spacing by space:
TAR-CVF Filename.tar/home/mine/work/home/mine/school |
The above command puts all the files in the work and school subdirectories of the/home/mine directory into a new file called Filename.tar in the current directory.
To list the contents of the tar file, type:
To extract the contents of the tar file, type:
This command does not delete the tar file, but it copies the archived content to the current working directory and retains any directory structure used by the archive. For example, if the tar file contains a file called Bar.txt, and the file is contained in the foo/directory, extracting the archive will cause the foo/directory to be created in your current working directory that contains bar.txt files.
Keep in mind that tar defaults to not compress files. To create a file that uses tar and bzip to archive compression, use the-J option:
TAR-CJVF filename.tbz File |
By conventions, the tar file with bzip2 compression has a. tbz extension. However, sometimes users use the tar.bz2 extension to archive their files.
The above command creates an archive file and then compresses it into a filename.tbz file. If you extract the Filename.tbz file using the BUNZIP2 command, the filename.tbz file is deleted, followed by the Filename.tar file.
You can also use a command to extend and deallocate bzip tar files:
To create a file that is archived and compressed with tar and gzip, use the-Z option:
TAR-CZVF filename.tgz File |
By conventions, the tar file that is compressed using gzip has a. tgz extension.
This command creates the archive file Filename.tar, and then compresses it into a filename.tgz file (the file Filename.tar is not retained). If you use the Gunzip command to extract the Filename.tgz file, the filename.tgz file is deleted and replaced with Filename.tar.
You can extend the gzip tar file with a single command:
|
Trick |
|
Type the command man tar to read the tar command for more information. |