Home  >  Article  >  Operation and Maintenance  >  Linux commands for file archiving and compression

Linux commands for file archiving and compression

巴扎黑
巴扎黑Original
2017-08-15 10:51:001800browse

File archiving command tar, file archiving has many benefits, it is easy to use and easy to manage. Next, I will share with you the archiving and compression commands of Linux files through this article. Friends who are interested should take a look.

1. File archiving command tar

Benefits of archiving:

 Convenient to use, query, and read

Easy to manage (deleting files in batches)

Why compression

If transmitted directly, it will occupy a large amount of traffic bandwidth, resulting in slow access to the company's intranet.

Transmission Pre-compression –→Decompression after transmission

Benefits of file compression

Save hard disk resources.

Accelerate the speed of file transfer

Use Command: tar

Function: Packing and compressing files; a tar file is a collection of several files and/or directories in a folder. Is the best tool for creating backups and archives


[root@xuegod72 ~]# tar –help 
[root@xuegod72 ~]# tar -cf archive.tar foo bar #将 foo bar 文件打包 成.tar 
[root@xuegod72 ~]# tar -tvf archive.tar #列出 tar中的所有文件 
[root@xuegod72 ~]# tar -xf archive.tar #提前或者释放 tar中的文件

Packaging

Syntax: tar [parameter] Package name target file/directory

Parameters:

 -c create create file
 -x decompress and restore file
 -v display execution details
 -f specifies the backup file
 -t lists the contents of the backup file
 -P (uppercase) When using an absolute path, do not remove the root sign before the file name,
 -C (uppercase) Specify the decompression location
 -z Compress by gzip
 -j Decompress by bzip2

Note: When starting the name of the package, the suffix should be added according to the type you want to compress. Suffix

[root@xuegod72 mnt]# tar -cvf grub2.tar /boot/grub2/ – Archive
[root@xuegod72 mnt]# tar -tvf grub2.tar|more – View the archive File
[root@xuegod72 mnt]# tar -cvfP grub2.tar /boot/grub2/ – archive root directory (dangerous, the root directory may be overwritten after decompression)
[root@xuegod72 mnt]# tar -xvf grub2 .tar -C /usr/src/ – Extract and release -C specify the location

In Linux, how do you distinguish the type and suffix of the file

For example, sh script aa

file command, check the file type

Linux is not as strict about file extensions as Windows requires, so when using Linux In the process, we often encounter some files that have no extension at all. So how should we judge whether a file without extension is a file or a directory?

Function: Determine the file type

Syntax: file file name


[root@xuegod72 mnt]# file grub2.tar 
[root@xuegod72 mnt]# file /etc/init.d/nginx

file file type

file size

Compare file size:


[root@xuegod72 mnt]# du -sh /boot/grub2/ 
[root@xuegod72 mnt]# ll -h /boot/grub2/

2. Linux file compression

Compression Tools: gzip bzip2 zip tar

3.1 Compression format

Common compression formats: gz, bz2, xz, zip, Z

format (file name format): .tar.gz or .tgz

Syntax format: tar zcvf newfile.tar.gz SOURCE

tar.gz format


[root@xuegod72 mnt]# tar zcf grub2.tar.gz /boot/grub2 #打包压缩 
[root@xuegod72 mnt]# tar zxf grub2.tar.gz #解压

tar.bz2 format


[root@xuegod72 mnt]# tar jcf grub2.tar.gz /boot/grub2 #打包压缩 
[root@xuegod72 mnt]# tar jxf grub2.tar.gz #解压

rar format


[root@xuegod72 mnt]# rar a grub2.rar /boot/grub2 #打包压缩 
[root@xuegod72 mnt]# rar x grub2.tar.gz #解压

zip format


[root@xuegod72 mnt]# zip -r grub2.zip /boot/grub2 #打包压缩 
[root@xuegod72 mnt]# unzip grub2.tar.gz -d /tmp #解压

gzip format


[root@xuegod72 mnt]# gzip grub2.tar #打包压缩 
[root@xuegod72 mnt]# gzip -d grub2.tar.gz #解压

bzip2 format


[root@xuegod72 mnt]# bzip2 -k grub2.tar #打包压缩 
[root@xuegod72 mnt]# bzip2 -d grub2.tar.bz2 #解压

xz format


[root@xuegod72 mnt]# xz -zk grub2.tar #打包压缩 
[root@xuegod72 mnt]# unxz -dk grub2.tar.xz #解压

The above is the detailed content of Linux commands for file archiving and compression. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn