How to compress files in linux-general Linux technology-Linux technology and application information. For many users, it is easier to use tool software WinZip and ARJ to compress or decompress files in DOS and Windows environments. However, in Linux, how does one compress and decompress files?
Create an archive with tar
Tar is short for "tape archive" (tape archive), which appeared in early stages of a computer that does not have a floppy drive, hard drive, or disc drive. At that time, the release and backup of software required a large volume of tape, and one of the first programs running on the computer had to be a tape reading program. Over time, the tar command gradually becomes a tool for archiving many files. Currently, many programs for Linux operating systems are packaged as tar files.
The format of the tar command is:
Tar [option]
The tar command has many parameters. The f option, device, or file name can be used to indicate that the tar command places the file in a specific device or file. When a file is created for a tar file, the extension of the file name is usually if a directory name is specified, all its subdirectories will be included in the file.
The format of the tar command and the f option is as follows:
# Tar opionsf archive-name.tar directory-and-filenames
To create an archive, use the c option, which, together with the f option, creates the archive in the device or file. Enter this option on the left of the f option. In the following example, all directories of mydirand Taobao are stored in myarch.tar.
# Tar cf myarch.tar mydir
If you want to change the files in the archived directory, you can use the u option to make tar update files in the file. Tar compares the last modification time of each archive file with the files in the corresponding user directory, and then copies the files modified after the last archive to the archive file. Files created in the user directory will also be added to the file. In the following example, the user updates the myarch.tar file with the latest modified or modified file in the mydirdirectory.
# Tar uf myarch.tar mydir
To view which files are stored in an archive, run the tar command and the t option. The following command lists all files stored in the myarch.tar file:
# Tar tf myarch.tar
To back up a file to a specific device, you only need to use the device name as the file. In the following example, you create an archive in the/dev/fdo disk and copy all the files in the mydir directory to the archive.
# Tar cf/dev/fdo mydir
To restore files on a device disk, use the xf option:
# Tar xf/dev/fdo
If the size of the backup file exceeds the memory available for the device, such as a floppy disk, you can create a tar file with Multiple labels. When you use the m option to archive data to a floppy disk, the tar command will remind you to put a new floppy disk when a floppy disk is full.
# Tar cmf/dev/fdo mydir
To restore the files on several disks, you only need to put the first floppy disk into the soft drive, and then enter the tar command with the x and m options. If necessary, you will be reminded to put it on another floppy disk.
# Tar xmf/dev/fdo
The tar operation does not compress files. If you want to compress an archive file, you can instruct tar to activate the gzip utility to compress the file before archiving. With the z option, tar uses gzip to compress the file before archiving the file. When the file is restored, the same Z option will activate gzip to decompress the file.
# Tar czf myarch.tar mydir
It is worth noting that a single file in the compressed file is different from the whole compressed file. Usually, file files are combined into a tar file to facilitate transmission. To shorten the transmission time, the smaller the file, the better. You can compress it and then transfer the compressed version. The recipient can decompress the package and restore the tar file. Use gzipto upload a file with the .tar.gz extension on the tarfile. The extension name .gz is added to the compressed gzip file name.
The example below creates a compressed example of myarch.tarwith the extension of .gz.
# Gzip myarch.tar
# Ls
Using myarch.tar.gz
The tar command has many parameters, but it is not difficult to use. It can quickly and simply generate archive files for any required sub-directories.
First, create a subdirectory with three files, and then create a subdirectory with the other three files, as shown below:
# Mkdir mydir
# Cd mydir
# Touch file1 file2 file3
# Mkdir mydir2
# Cd mydir2
# Touch file21 file22 file23
# Cd ../..
# Tree mydir
Mydir
File1
File2
File3
Mydir2
File21
File22
File23
Now you have a sub-directory and a file in it. Use the c (generated) and f (File) parameters of this command to generate a tar file:
# Tar cf mydir.tar mydir
# Ls-l *. tar
-R w-r--1 bball u s e r s 10240 Jan 5 15: 01 mydir. t a r
Note that the original subdirectories have not changed. By default, the tar command does not delete the original subdirectories and files. If you want to see the command execution process, you can use the v parameter as follows:
The tar command displays the subdirectories and file names that are being added to the file. Use the w parameter, that is, the interaction parameter, so that the tar command will ask if you want to add each file during execution. When you want to selectively back up subdirectories with few contents, this is very convenient, as shown below:
# Tar cwf mydir.tar mydir
Add mydir? Y
Add mydir/file1? N
Add mydir/file2? Y
Add mydir/file3? N
Add mydir/mydir2? Y
Add mydir/mydir2/file21? Y
Add mydir/mydir2/file22? N
Add mydir/mydir2/file23? Y
In the preceding example, the files file1, file3, and file22 are not archived. You can use the t parameter of the tar command to list the content in the file. The f parameter defines the tar file used for the operation, as shown below:
# Tar tf mydir.tar
Mydir/
Mydir/file2
Mydir/mydir2/
Mydir/mydir2/file21
Mydir/mydir2/file23
Note that if the order of parameters is incorrect, the tar command displays the error message and exits.
Next let's take a look at how to release the entire file or one of the files. If you want to release all the files, you can use the-x release parameter and-f. To understand the command execution process, you can also add the-v parameter:
# Tar xvf mydir.tar
Mydir/
Mydir/file2
Mydir/mydir2/
Mydir/mydir2/file21
Mydir/mydir2/file23
If you only want to release several files from the file, you can use the w parameter again:
# Tar xvwf mydir.tar
Extract mydir /? Y
Mydir
Extract mydir/file2? Y
Mydir/file2
Extract mydir/mydir2 /? Y
Mydir/mydir2/
Extract mydir/mydir2/file21? Y
Mydir/mydir2/file21
Extract mydir/mydir2/file23? Y
Mydir/mydir2/file23
The preceding example shows that the file is viewed and the file is released interactively. If you only want to release a file from the file, you can specify the file in the command line. As an example, I first deleted the original mydir sub-directory and then used an empty sub-directory to perform the following operations:
# Tar xf mydir.tar mydir/mydir2/file23
# Tree mydir
M y d I r
--Mydir2
--File23
1 directory, 1 file
Note: As you can see, only one file is released. Although the tar command does not overwrite the entire subdirectory, it will overwrite the files with the same file name.
It is worth mentioning that other programs, such as BRU-2000 or taper script programs, can also be used to back up the system or selected files and subdirectories. The OpenLinux operating system can also use cron schedules to automatically archive files.
Create a cpio File
Cpio commands can import or copy files from tar or cpio files. Because the cpio command is compatible with the tar command, I will not detail how it works here. However, this command has some functions not available for tar commands, as shown below:
◆ Supports cpio and tar file formats;
◆ Supports many old-fashioned tape data formats;
◆ File names that can be read through an MTS queue.
Only a few Linux software packages are released in cpio format. If you are interested in the details of the cpio command, read its user manual.
Use gzip to compress files
The gzip command is used to compress files. It can be used not only to compress large and rarely used files to save disk space, but also to form popular compressed file formats in the Linux operating system together with the tar command. According to statistics, the gzip command has 60% ~ 70% compression rate.
The format of the gzip command is:
Gzip [option] [file]
Gzip is easy to use. To compress a file or tape file, enter the following content:
# Gzip mydir.tar
In the initial state, gzips will compress the file and add a .gz extension, and then delete the original file. If you want to decompress the file, you can use the corresponding program command gunzip of gzip or the-d decompression parameter of the gzip command. However, ensure that the uncompressed file has the extension .gz (or. Z,-gz,. z,-z, or _ z). Otherwise, the gzip command and the gunzip command will display error information. If you want to use your own extension, you can use the-S extension parameter, as shown below:
# Gzip-S. gzipped mydir.tar
Gzip can also process file packages compressed by zip, compress, and pack commands. If you want to see more information during the compression or decompression process, you can use the-l column list parameter to see the file length when the file is compressed or decompressed. In the previous example, after compressing the subdirectory mydir, you can use the gzip command to obtain the relevant data as follows:
In addition, gzip also has a very useful parameter-t, which can be used to test the integrity of the compressed file. If the file is normal, gzip will not display it. If you want to see the OK letters, you can use the-TV parameter when testing a file.
Compress files
The compress command is used to compress files just like the file name.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.