Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Linux Archiving and Compression

2025-02-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Linux archive and compression example analysis, has a certain reference value, interested friends can refer to the next, I hope you read this article after a great harvest, the following let Xiaobian take you to understand.

In Linux, learning to package (archive) and compress files or directories is a basic skill for every beginner.

Packaging refers to storing multiple files and directories in a single file, while compression refers to using algorithms to process files to reduce disk space.

File compression and archiving

Sometimes we need to store a set of files as a single file for backup or transfer to another directory or even to another computer. Sometimes we also need to compress files into one file so they use less disk space and can be downloaded faster over the Internet.

It is important for users to understand the difference between an archive file and a compressed file. An archive is a collection of files and directories stored in a file. The archive file is not compressed-it uses disk space equal to the sum of all the files and directories in it. A compressed file is also a collection of files and directories, and this collection is also stored in a file, but it is stored in such a way that it takes up 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 you don't use often or don't use anymore but want to keep. You can even create archive files and compress them to save disk space.

Note that archive files are not compressed files, but compressed files can be archive files. Using File Packager

Red Hat Linux includes a graphical compression tool called File Packer. It can compress, decompress, and archive files and directories. File Packager supports common UNIX and Linux file compression and archiving formats, and it has a simple interface and rich documentation. It is also integrated into desktop environments and graphical file managers to make working with archived files easier.

To launch File Packer, click Main Menu => Attachments => File Packer. You can also type file-roller from a shell prompt to start the file packer. Figure 14-1 shows the file packer in action.

Tip If you are using a file manager (such as Nautilus), you can double-click the file you want to unarchive or unzip to launch the file packer. The File Packer Browse window will appear with folders showing the files you want to extract or unarchive so you can extract or browse them.

Figure 14-1. Running file packager

Use a file packer to decompress or unarchive

To cancel archiving or (and) extract files, click the Open button on the toolbar. A file menu will pop up allowing you to select the archive you want to work with. For example, if you have a foo.tar.gz file in your home directory, highlight the file and click OK. The file will appear as a folder in the main browser window of the file packager, and you can double-click the folder icon to scroll through its contents. The file packager preserves all the existing directory and subdirectory structures, which makes it easy to find a particular file in the archive. You can click the Extract button, select the directory where you want to save the unarchived files, and then click the OK button to extract a file or the entire archive.

Use File Packager to Create Archive Files

File Packager allows you to create archives of files and directories if you need to free up some hard disk space or send multiple files or all files in a directory to another user. 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 technique. For example, you can select "Gzip Tar (tar.gz) format" from the drop-down menu and type the name of the archive you want to create. Click OK and you can add files and directories to the new archive. To add files to your new archive, click Add, which will bring up a browser window (Figure 14-2) where you can find the files or directories you want to add to your archive. Click OK and Close to close the archive.

Figure 14-2. Using File Packager to Create Archives

Tips File Packager is capable of performing more tasks than those mentioned here. Please read the instructions for the file packer (click Help => Instructions) for more information. Compress files at shell prompt

Compressed files use less disk space and download faster than large uncompressed files. In Red Hat Linux, the file compression tools you can use 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 it is most compatible with compression tools on Windows.

Tools File extension decompression tool gzip.gzgunzipbzip2.bz2bunzip2zip.zipunzip

Table 14-1. compression tool

By convention, files compressed with gzip have the extension.gz; files compressed with bzip2 have the extension. bz2; and files compressed with zip have the extension.zip.

Files compressed with gzip can be decompressed using gunzip; files compressed with bzip2 can be decompressed using bunzip2; files compressed with zip can be decompressed using unzip.

bzip2 and bunzip2

To compress files using bzip2, type the following command at a shell prompt:

bzip2 filename

The file is compressed and saved as filename.bz2.

To expand a compressed file, type:

bunzip2 filename.bz2

The filename.bz2 file is deleted and replaced with filename.

You can use the bzip2 command to work with multiple files and directories simultaneously by listing them one by one, separated by spaces:

bzip2 filename.bz2 file1 file2 file3 /usr/work/school

The above command compresses the contents of file1, file2, file3, and/usr/work/school (assuming it exists) into filename.bz2.

Tip For more information about these two commands, type man bzip2 and man bunzip2 at the shell prompt to read the bzip2 and bunzip2 command description pages. gzip and gunzip

To compress files using gzip, type the following command at a shell prompt:

gzip filename

The file is compressed and saved as filename.gz.

To expand a compressed file, type:

gunzip filename.gz

Filename.gz is deleted and filename is added.

You can use the gzip command to work with multiple files and directories simultaneously by listing them one by one, separated by spaces:

gzip -r filename.gz file1 file2 file3 /usr/work/school

The above command compresses the contents of the file1, file2, file3, and/usr/work/school directories (assuming they exist) into filename.gz.

Tip For more information about these two commands, type man gzip and man gunzip at the shell prompt to read the description pages for the gzip and gunzip commands. zip and unzip

To zip a file, type the following command at a 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 * recursively * include all files included in the filesdir directory.

To extract the contents of a zip file, type:

unzip filename.zip

You can use the zip command to work with multiple files and directories simultaneously by listing them one by one, separated by spaces:

zip -r filename.zip file1 file2 file3 /usr/work/school

The above command compresses the contents of the file1, file2, file3, and/usr/work/school directories (assuming they exist) into the file filename.zip

Tip To get more information about these two commands, type man zip and man unzip at the shell prompt to read the zip and unzip instructions pages. File files at shell prompt

A tar file is a collection of several files and/or directories in one file. This is the best way to create backups and archives.

The options tar uses are:

-c -Create a new archive. - f -When used with the-c option, tar files are created with the filename specified by the option; when used with the-x option, archives specified by the option are unarchived. - t -Displays a list of files included in tar files. - v -Displays the archive progress of the file. - x -Extract files from archive. - z -Use gzip to compress tar files. - j -Use bzip2 to compress tar files.

To create a tar file, type:

tar -cvf filename.tar directory/file

In the example above, filename.tar represents the file you created, and directory/file represents the files and directories you want to place in the archive.

You can use tar to work with multiple files and directories simultaneously by listing them one by one, separated by spaces:

tar -cvf filename.tar /home/mine/work /home/mine/school

The above command puts all files in the work and school subdirectories of/home/mine into a new file called filename.tar in the current directory.

To list the contents of a tar file, type:

tar -tvf filename.tar

To extract the contents of a tar file, type:

tar -xvf filename.tar

This command does not delete tar files, but it copies the contents of the dearchived files to the current working directory and preserves any directory structures used by the archived files. For example, if the tar file contains a file called bar.txt that is contained in the foo/directory, extracting the archive will result in creating the foo/directory in your current working directory that contains the bar.txt file.

Remember, tar does not compress files by default. To create a file that is archived using tar and bzip, use the-j option:

tar -cjvf filename.tbz file

By convention, tar files compressed with bzip2 have the.tbz extension. However, sometimes users use the tar.bz2 extension to archive their files.

The above command creates an archive file, which is then compressed into filename.tbz. If you extract the filename.tbz file using bunzip2, the filename.tbz file will be deleted, followed by the filename.tar file.

You can also expand and unzip archive a btar file with one command:

tar -xjvf filename.tbz

To create a file archived and compressed in tar and gzip, use the-z option:

tar -czvf filename.tgz file

By convention, tar files compressed using gzip have the.tgz extension.

This command creates the archive file filename.tar and compresses it to filename.tgz (the file filename.tar is not preserved). If you use gunzip to extract the filename.tgz file, the filename.tgz file will be deleted and replaced with filename.tar.

You can expand gzip tar files with a single command:

tar-xzvf filename.tgz Thank you for reading this article carefully. I hope that the "Linux archiving and compression example analysis" shared by Xiaobian will help everyone. At the same time, I hope that everyone will support it a lot. Pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report