Linux Basics (5)

File packaging and decompression

1. The common archive format
1) The two most common on Windos *.zip, *,rar;
2) on Linux in addition to the above two, there *.gz, *.xz, *.bz2, *.tar, *.tar.gz, *.tar.xz, *.tar.bz2
*.zip: packaged compressed ZIP file
*,rar: rar compressed program file
*.gz: gzip program (GNU zip) compressed files
*.xz: xz program compressed files
*.bz2: bzip2 program compressed file
*.tar: tar packaged, uncompressed file
*.tar.gz: tar package, gzip program compressed files
*.tar.xz: tar package, xz program compressed file
*.tar.bz2: tar package , bzip2 compressed program files

2. zip compression Packager

(1) using the zip package folder:
the directory / home / shiyanlou / Desktop packaged into a file, and check the file size and type of packaging. -rRecursive parameter indicates the package that contains the entire contents of subdirectories, -qparameter represents the quiet mode, which does not output information to the screen, -orepresents the output file, to be followed later in the package output file name. Use duthe command to view the file size of the package.

$ cd /home/shiyanlou
$ zip -r -q -o shiyanlou.zip /home/shiyanlou/Desktop
$ du -h shiyanlou.zip
$ file shiyanlou.zip

(2) Sets the compression level of 1 and 9 (9 maximum, minimum 1), repackaged. Add a parameter for setting the level of compression -[1-9], a compression represents the fastest and bulky, 9 denotes the smallest but the longest time consuming. -xTo rule out the zip file we created last time, otherwise it will be packaged into a compressed file Note: Use only absolute path here, otherwise it does not work.

$ zip -r -9 -q -o shiyanlou_9.zip /home/shiyanlou/Desktop -x ~/*.zip
$ zip -r -1 -q -o shiyanlou_1.zip /home/shiyanlou/Desktop -x ~/*.zip

Then the ducommand view the default compression level respectively, the minimum and maximum compression level and size of the uncompressed file:

$ du -h -d 0 *.zip ~ | sort

Wherein hparameters: , d-human-readable's; , (depth view files) -max-depth.

(3) Use -ethe parameters to create an encrypted zip package

$ zip -r -e -o shiyanlou_encryption.zip /home/shiyanlou/Desktop

If you want without any problems after the zip file decompression Linux created on Windows, you also need to make some changes to the command:

$ zip -r -l -o shiyanlou.zip /home/shiyanlou/Desktop

Need to add -lparameters LF(Linux line breaks) to CR+LF(Windows newline) to achieve the above purpose.

3. Unzip the zip files using the unzip command

The shiyanlou.zip extract to the current directory:

$ unzip shiyanlou.zip

Use quiet mode, extract the files to a specified directory:

$ unzip -q shiyanlou.zip -d ziptest

When the above specified directory does not exist, it is created.
Just do not want to unpack the archive to see what you can use -lparameters:

$ unzip -l shiyanlou.zip

It is also noted compatibility problems when using the unzip unzip the file, but the concern here is the Chinese encoding problem, compressed files created are usually Windows system above, the default will be used GBK if there is documentation that contains Chinese or Chinese as the file name or other coding, and Linux above default is UTF-8 encoding, without any treatment, directly extract if possible problems Chinese garbled will appear (sometimes it will automatically help you deal), in order to solve this problem, we can when extracting the specified coding type. Used -O(letters, uppercase o) parameter specifies the encoding type:

unzip -O GBK 中文压缩文件.zip
  1. tar package tool

Linux is above the more commonly used tartool, taroriginally just a packaging tool, but is also implemented support for 7z, gzip, xz, bzip2 and other tools, these compression tool itself can only be achieved on a file or directory (file directory compression alone ) compression, * did not realize packed file compression . tar decompression and compression commands are the same, only different parameters, more convenient to use.

First tar command to master some basic use, not just packaged compression (creating the archive) and unpack operations.
(1) create a tar package:

$ cd /home/shiyanlou
$ tar -P -cf shiyanlou.tar /home/shiyanlou/Desktop

The above command, -Pretains absolute path symbol, -crepresents the creation of a tar package file, -fused to specify the file name created, the file name must note immediately -fafter the argument, such as can not be written tar -fc shiyanlou.tar, can be written tar -f shiyanlou.tar -c ~. You can also add -vparameters to output visual manner packaged file.
(2) unpack a file ( -xparameter) to the specified directory path already exists ( -Cparameter):

$ mkdir tardir
$ tar -xf shiyanlou.tar -C tardir

(3) to see only puzzling package file -tparameters:

$ tar -tf shiyanlou.tar

(4) to create a different file compression formats, for tarit is quite simple, just need to change a parameter, here to use the gziptool to create a *.tar.gzfile as an example to illustrate.

Only you need to create taradd files on the basis of -zparameters, gzipto compress files:

$ tar -czf shiyanlou.tar.gz /home/shiyanlou/Desktop

Extracting * .tar.gz file:

$ tar -xzf shiyanlou.tar.gz

(5) To create or decompress the file only need to change a parameter to use other compression tools:
*.tar.gz: -z;
*.tar.xz: -J;
*tar.bz2: -j;

summary:

  • zip:
    Packing: zip something.zip something (please add directory -r parameter)
    unpack: unzip something.zip
    path specified: -d parameter
  • tar:
    Packing: tar -cf something.tar something
    unpack: tar -xf something.tar
    specified path: -C parameter

Disk management and file system operations

File system operations

  • Check the disk capacity and purpose:

Use dfthe command to view disk capacity:

$ df
或者
$ df -h
# 加上`-h`参数,以更易读的方式展示

Use the du command to see the capacity of the directory:

# 默认同样以 块 的大小展示
$ du 
# 加上`-h`参数,以更易读的方式展示
$ du -h

-dCheck parameter specifies the directory depth

# 只查看1级目录的信息
$ du -h -d 0 ~
# 查看2级
$ du -h -d 1 ~
du -h #同--human-readable 以K,M,G为单位,提高信息的可读性。
du -a #同--all 显示目录中所有文件的大小。
du -s #同--summarize 仅显示总计,只列出最后加总的值。

Simple Disk Management

  • dd command Introduction

ddCommands for converting and copying files, but it's different from the copy cp. Mentioned before a very important point, about Linux every file that is , on Linux, hardware device drivers (such as hard disk) and a special device files (such as / dev / zero and / dev / random) are like normal files, is to achieve a function corresponding to the respective drivers, dd files can be read or written to the file. Thus, dd can also be used in the boot sector of the backup hardware, obtaining a certain number of random data or null data tasks. ddCan process both data replication, e.g. endian conversion, or interchanged between ASCII and EBCDIC encoding.

ddThe command-line statement and other Linux program is different because of its command-line options for the format 选项=值, rather than the more standard --选项 值or -选项=值. ddThe default read from standard input and write to standard output, but you can use option if(input file, the input file) and ofchange (output file, the output file).

With ddcommands from a user standard input standard output or input to a file:

# 输出到文件
$ dd of=test bs=10 count=1 # 或者 dd if=/dev/stdin of=test bs=10 count=1
# 输出到标准输出
$ dd if=/dev/stdin of=/dev/stdout bs=10 count=1
# 注
在打完了这个命令后,继续在终端打字,作为你的输入

The above command read from the standard input a user input device (the default value, may be omitted) is then output to a testfile, bs(Block size) for specifying the block size (Default is Byte, may be assigned as 'K', 'M', 'G' units, etc.), countis used to specify the number of blocks. As I read only designated a total of 10 bytes of data, when I typed "hello shiyanlou" after the carriage return plus space total of 16 bytes (an English one byte characters), it is evident exceeds the set size . Du and use the cat command to see the actual content of the file write completion really only 10 bytes, while the other redundant input will be intercepted and retained in the standard input.

ddWhile copying data conversion may also be implemented, cite a simple example: the output of the English characters to upper case and then written to the file:

$ dd if=/dev/stdin of=test bs=10 count=1 conv=ucase
  • Create a virtual image file using the dd command

Created from / dev / zero device with a capacity of 256M empty file:

$ dd if=/dev/zero of=virtual.img bs=1M count=256
$ du -h virtual.img

Then you want the file format (written to the file system), where we have to learn a (accurate to say that a group of) new command to complete this requirement.

  • Mkfs command to format the disk (in our case a virtual disk image that you have created)

You can simply use the following command to our virtual disk images formatted as a ext4file system (actually mkfs.ext4 is accomplished using mke2fs formatting work):

 $ sudo mkfs.ext4 virtual.img

If you want to know which file systems supported by Linux you can enter ls -l /lib/modules/$(uname -r)/kernel/fs(not view our environment) view.

  • Use the mount command to mount the disk to the directory tree

A user opens a file on a Linux / UNIX machines before, including the file system operation must be mounted , then the user to execute instructions to mount to mount the file system. The instructions are typically used on a USB or other removable storage device, and the root directory would remain mounted state. File And because Linux / UNIX file system may not necessarily correspond to a file if the hardware device, it is possible to mount a file system that contains the directory tree.

mount command Linux / UNIX command line tells the operating system, the corresponding file system is ready to be used, and the file system will correspond to a certain point (called a mount point). Mount good files, directories, device special files and can offer users.

We first use the mount to view the next host has mounted file system :

$ sudo mount

The results output in each row represents a device or virtual device, the device name is top of each line, and then later on a mount point, followed by type represents file system type, and then followed by mount options (for example, can be mounted when mount as read only setting, etc.).

Mount the disk to the directory tree, mount command general format is as follows:

mount [options] [source] [directory]

Some common operations:

mount [-o [操作选项]] [-t 文件系统类型] [-w|--rw|--ro] [文件系统源] [挂载点]

Now we direct to mount virtual disk mirroring to create /mntdirectory:

$ mount -o loop -t ext4 virtual.img /mnt 
# 也可以省略挂载类型,很多时候 mount 会自动识别

# 以只读方式挂载
$ mount -o loop --ro virtual.img /mnt
# 或者mount -o loop,ro virtual.img /mnt
  • Use the unmount command to unmount the mounted disk
# 命令格式 sudo umount 已挂载设备名或者挂载点,如:
$ sudo umount /mnt
  • Use fdisk to partition
# 查看硬盘分区表信息
$ sudo fdisk -l
# 进入磁盘分区模式
$ sudo fdisk virtual.img

After the operation is complete enter pto view the results, and do not forget to enter wwrite the partition table.

  • Use losetup command associated with the loop mirror devices
$ sudo losetup /dev/loop0 virtual.img
# 如果提示设备忙你也可以使用其它的回环设备,"ls /dev/loop*"参看所有回环设备

# 解除设备关联
$ sudo losetup -d /dev/loop0

And then use the mkfsformat each partition (in front of us is to format the entire virtual disk image file or disk), but before formatting, we have to establish a virtual device mapping for each partition, use the kpartxtool, you need to install:

$ sudo apt-get install kpartx
$ sudo kpartx -av /dev/loop0

# 取消映射
$ sudo kpartx -dv /dev/loop0

Then re-formatted, we will all be formatted as ext4:

$ sudo mkfs.ext4 -q /dev/mapper/loop0p1
$ sudo mkfs.ext4 -q /dev/mapper/loop0p5
$ sudo mkfs.ext4 -q /dev/mapper/loop0p6

After formatting the new four empty directory under / media directory is used to mount the virtual disk:

$ mkdir -p /media/virtualdisk_{1..3}
# 挂载磁盘分区
$ sudo mount /dev/mapper/loop0p1 /media/virtualdisk_1
$ sudo mount /dev/mapper/loop0p5 /media/virtualdisk_2
$ sudo mount /dev/mapper/loop0p6 /media/virtualdisk_3

# 卸载磁盘分区
$ sudo umount /dev/mapper/loop0p1
$ sudo umount /dev/mapper/loop0p5
$ sudo umount /dev/mapper/loop0p6
$ df -h

source

Published 33 original articles · won praise 1 · views 1240

Guess you like

Origin blog.csdn.net/weixin_44783002/article/details/104704565