Compression using Gzip and Zip
Compressed files take up less disk space and load faster from the network than large, uncompressed files. You can compress Linux files using the free Gzip or Zip compression utilities; this format is recognized by most operating systems.
By default, compressed files receive the .gz extension. The Gzip command creates a compressed file with the .gz extension; Gunzip extracts compressed files and removes the .gz file.
To compress a file, at the shell prompt, enter the following command:
gzip filename.ext
The file will be compressed and saved as filename.ext.gz.
To decompress a compressed file, enter:
gunzip filename.ext.gz
The file filename.ext.gz is deleted and replaced by the file filename.ext.
If you exchange files with non-Linux users, it is probably better to use zip to avoid compatibility issues. Red Hat Linux can easily open zip or gzip files, but non-Linux operating systems may not handle gzip files.
To compress a file using zip, enter:
zip -r filename.zip files
In this example, filename specifies the name of the file to be created, and files specifies which files you want to include in the archive:
To extract the contents of a zip file, enter:
unzip filename.zip
You can compress multiple files at once using zip or gzip. List the file names, separated by spaces.
gzip filename.gz file1 file2 file3 /user/work/school
The above command will compress file1, file2, file3, and the contents of the /user/work/school directory and place them into the file filename.gz.
Archiving with Tar
Tar saves multiple files or the contents of a directory or directories into a single file. This is a good way to create backups and archives. Typically, files created with tar have the .tar extension.
To create a tar file, enter:
tar -cvf filename.tar files/directories
In this example, filename.tar specifies the name of the file to be created, and files/directories specifies which files or directories you want to include in the new archive.
You can use absolute or relative paths for these files or directories (to learn more about paths, refer to the Changing Directories with cd section in Chapter 10). Separate file and directory names with spaces.
The example below shows the creation of a file with absolute paths:
tar -cvf foo.tar /home/mine/work /home/mine/school
The above command will place all files from the /work directory and the /school subdirectory into the new file foo.tar in the current working directory.
The command tar -cvf foo.tar file1.txt file2.txt file3.txt will place the files: file1.txt, file2.txt, and file3.txt into the new file foo.tar.
To view the contents of a tar file, enter:
tar -tvf foo.tar
To extract the contents of a tar file, enter:
tar -xvf foo.tar
This command does not delete the .tar file; it extracts the .tar contents into the current working directory.
The tar command does not automatically compress files. You can compress tar files by executing:
tar -czvf foo.tar
Compressed files usually have the .tgz extension and are compressed using gzip.
To decompress a compressed tar file, enter:
tar -xzvf foo.tgz
