一、生成测试文件
1、生成测试文件命令dd
基本用法
1
| dd if=<输入源> of=<输出目标> bs=<块大小> count=<块数量> [status=progress]
|
示例:
1 2 3 4 5 6 7
| dd if=/dev/zero of=/tmp/testfile bs=1M count=100
• if=/dev/zero • of=/tmp/testfile • bs=1M • count=100
|
| 设备文件 |
输出内容 |
典型用途 |
| /dev/zero |
无限连续的0x00字节 |
创建空白文件,初始化存储空间 |
| /dev/null |
黑洞设备(丢弃所有写入) |
屏蔽程序输出 |
2、查看文件
1 2 3 4 5 6 7 8 9 10
| file 文件名 [root@farrah 桌面]# file test test: ASCII text
[root@xnha tmp]# hexdump testfile 0000000 0000 0000 0000 0000 0000 0000 0000 0000 * 6400000
|
二、压缩和解压缩
1、压缩命令gzip
gzip是Linux中用于文件压缩的常用工具,压缩以后会生成.gz后缀文件,原文件默认删除。
[!NOTE]
注意:gzip仅仅用来压缩文件 要想压缩目录需要使用tar命令打包成一个文件后再对文件进行压缩
命令格式
选项说明
| 参数 |
说明 |
| -k |
保留原文件 |
| -d |
解压文件 |
| -1 ~ -9 |
压缩级别:1最快压缩率最低,-9最慢压缩率最高(默认-6) |
| -v |
显示压缩率等详细信息 |
| -c |
输出到标准输出(不修改文件) |
| -t |
测试压缩文件的完整性 |
| -r |
递归处理目录 |
| -l |
查看压缩文件信息(压缩前后大小,压缩率) |
使用方法
删除原文件的压缩
1 2 3 4 5
| [root@farrah 桌面]# cd /tmp [root@farrah tmp]# touch testfile [root@farrah tmp]# gzip testfile [root@farrah tmp]# ll -rw-r--r--. 1 root root 29 8月 4 10:14 testfile.gz
|
不删除原文件的压缩
1 2 3 4
| [root@farrah tmp]# gzip -k testfile [root@farrah tmp]# ll /tmp |grep testfile 筛选出testfile查看文件 -rw-r--r--. 1 root root 0 8月 4 10:17 testfile -rw-r--r--. 1 root root 29 8月 4 10:17 testfile.gz
|
2.解压命令gunzip
命令格式
1
| gunzip [选项] 文件名.gz #与gzip -d 可同等替换
|
#选项说明与压缩命令gzip基本一致
使用方法
不保留原压缩文件的解压
1 2 3
| [root@farrah tmp]# gunzip testfile.gz [root@farrah tmp]# ll /tmp | grep testfile -rw-r--r--. 1 root root 0 8月 4 10:17 testfile
|
保留原压缩文件的解压
1 2 3 4 5 6 7 8 9 10
| [root@farrah tmp]# gunzip -k testfile.gz [root@farrah tmp]# ll /tmp | grep testfile -rw-r--r--. 1 root root 0 8月 4 10:17 testfile -rw-r--r--. 1 root root 29 8月 4 10:17 testfile.gz
[root@farrah tmp]# gzip -d -k testfile.gz [root@farrah tmp]# ll /tmp | grep testfile -rw-r--r--. 1 root root 0 8月 4 10:17 testfile -rw-r--r--. 1 root root 29 8月 4 10:17 testfile.gz
|
3.gzip查看压缩内容
1 2
| zcat file.gz gzip -l backup.gz
|
4.压缩命令bzip2
bzip2 是 Linux 中基于 Burrows-Wheeler 变换 的高效压缩工具,相比 gzip 压缩率更高,但速度较
慢。压缩后生成 .bz2 后缀文件,默认删除原始文件。
命令格式
选项说明
| 参数 |
说明 |
| -k |
保留原文件 |
| -d |
解压文件 |
| -1 ~ -9 |
压缩级别:1最快压缩率最低,-9最慢压缩率最高(默认-6) |
| -v |
显示压缩率等详细信息 |
| -c |
输出到标准输出(不修改文件) |
| -t |
测试压缩文件的完整性 |
| -f |
强制覆盖已存在的输出文件 |
| -s |
降低内存占用(牺牲速度) |
| -z |
显式指定压缩模式(默认行为) |
使用方法
1 2 3 4
| [root@farrah tmp]# bzip2 testfile [root@farrah tmp]# ll /tmp |grep testfile -rw-r--r--. 1 root root 14 8月 4 10:17 testfile.bz2
|
1 2 3 4 5
| [root@farrah tmp]# bzip2 -k testfile [root@farrah tmp]# ll /tmp |grep testfile -rw-r--r--. 1 root root 0 8月 4 10:17 testfile -rw-r--r--. 1 root root 14 8月 4 10:17 testfile.bz2
|
5.解压命令bunzip2
命令格式
#选项说明与压缩命令gzip基本一致
使用方法
不保留原文件进行解压
1 2 3
| [root@farrah tmp]# bunzip2 testfile.bz2 [root@farrah tmp]# ll /tmp |grep testfile -rw-r--r--. 1 root root 0 8月 4 10:17 testfile
|
保留原文件进行解压
1 2 3 4 5 6 7 8 9 10 11
| [root@farrah tmp]# bunzip2 -k testfile.bz2 [root@farrah tmp]# ll /tmp | grep testfile -rw-r--r--. 1 root root 0 8月 4 10:17 testfile -rw-r--r--. 1 root root 14 8月 4 10:17 testfile.bz2
[root@farrah tmp]# bzip2 -kd testfile.bz2 [root@farrah tmp]# ll /tmp | grep testfile -rw-r--r--. 1 root root 0 8月 4 10:17 testfile -rw-r--r--. 1 root root 14 8月 4 10:17 testfile.bz2
|
6.bzip2查看压缩内容
1 2 3 4 5
| bzcat file.bz2 bzip2 -l archive.bz2
|
三、打包文件
1、打包命令tar
tar (Tape Archive)是 Linux 系统中用于 打包文件目录 的归档工具,支持保留文件权限、时间戳和
目录结构(不对文件/目录本身造成任何影响,产生新文件)。常与压缩工具( gzip 、 bzip2 、 xz )
结合使用,形成 .tar.gz 、 .tar.bz2 、 .tar.xz 等压缩包
命令格式
选项说明
| 参数 |
说明 |
| -c(create) |
创建新归档文件 |
| -x (extracct) |
解压归档文件 |
| -t (list) |
查看归档内容列表 |
| -r |
追加文件到已有归档(要先解压在重新打包) |
| 参数 |
说明 |
| -f |
指定归档名(必须放在最后) |
| -v |
显示操作详情 |
| -z |
使用gzip压缩(生成.tar.gz) |
| -j |
使用bzip2压缩(生成tar.bz2) |
| -J |
使用xz压缩(生成.tar.xz) |
| -C |
解压到指定目录 |
| –exclude |
排除指定文件/目录 |
| –wildcards |
使用通配符文件 |
使用方法
打包压缩
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| [root@farrah tmp]# tar -cvf test1.tar test test/ test/dir1/ test/dir1/file1.txt test/dir1/file2.txt test/dir1/newfile.txt test/dir2/ test/dir2/subdir/ test/dir2/subdir/dir1/ test/dir2/subdir/dir1/file1.txt test/dir2/subdir/dir1/file2.txt [root@farrah tmp]# gzip test1.tar
[root@farrah tmp]# tar -czvf test1.tar.gz test test/ test/dir1/ test/dir1/file1.txt test/dir1/file2.txt test/dir1/newfile.txt test/dir2/ test/dir2/subdir/ test/dir2/subdir/dir1/ test/dir2/subdir/dir1/file1.txt test/dir2/subdir/dir1/file2.txt
|
解压
1.解压.tar文件
1 2 3 4 5
| [root@farrah tmp]# tar -xf testfile.tar [root@farrah tmp]# ll /tmp | grep testfile -rw-r--r--. 1 root root 0 8月 4 10:17 testfile -rw-r--r--. 1 root root 14 8月 4 10:17 testfile.bz2 -rw-r--r--. 1 root root 10240 8月 4 12:26 testfile.tar
|
2.解压.tar.gz到指定目录
1 2 3 4 5 6 7 8
| [root@farrah tmp]# tar -cvzf testfile.tar.gz testfile testfile/ testfile/dir1/ testfile/file [root@farrah tmp]# tar -zxf testfile.tar.gz -C /tmp/test/ [root@farrah tmp]# ls /tmp/test testfile
|
3.查看归档内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
[root@xnha ~]# gunzip test321.tar.gz [root@xnha ~]# tar -tf test321.tar
[root@farrah tmp]# tar -tf testfile.tar.gz testfile/ testfile/dir1/ testfile/file[root@farrah tmp]# tar -tf testfile.tar.gz testfile/ testfile/dir1/ testfile/file
[root@farrah tmp]# tar -ztf testfile.tar.gz | grep test testfile/ testfile/dir1/ testfile/file
|