Command for viewing IP addresses in Linux: ifconfig.
How to compress and decompress files in Linux
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] <File>;
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:
# Tar CVF mydir.tar mydir
Mydir/
Mydir/file1
Mydir/file2
Mydir/file3
Mydir/mydir2/
Mydir/mydir2/file21
Mydir/mydir2/file22
Mydir/mydir2/file23
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]
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:
# Gzip-l mydir.tar.gz
Compressed uncompr. ratio uncompressed_name
312 21330 98.2% mydir.tar
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. This is an early UNIX compression program. It uses adaptive lemple-Ziv encoding to reduce the size of the specified file. Each file is replaced by a compressed file with the. Z extension, but the file owner relationship, access time, and modification time remain unchanged. If no guidance file is provided, the standard input file is compressed and sent to the standard output.
The format of the compress command is:
Compress [Option]
There is an uncompress command corresponding to compress. Its format is as follows:
Uncompress [Option]
It restores the compressed file ". Z.
The options for the compress and uncompress commands are as follows:
◆-C is written to the standard output without changing the file.
◆-F is forcibly compressed, even if the file is not actually reduced or the. Z file already exists, it is compressed.
◆-V shows the percentage of each compressed file.
The following example describes how to use the compress command:
# Compress File
The code above indicates that the compressed file. Z will be generated instead of file. to decompress the file, enter:
# Uncompress file. Z
Note: like using the gzip command, you must provide a file name with the. Z extension when using the uncompress command. Otherwise, the uncompress command displays an error message.