Linux查看文件内容和压缩、解压缩

1,272 阅读7分钟

1.查看文件内容

1.1 七大常见的文件类型

第一个字符文件类型
-普通文件,包括纯文本文件、二进制文件、各种压缩文件等。
d目录,类似 Windows 系统中的文件夹。
b块设备文件,就是保存大块数据的设备,比如最常见的硬盘。
c字符设备文件,例如键盘、鼠标等。
s套接字文件,通常用在网络数据连接,可以启动一个程序开监听用户的要求,用户可以通过套接字进行数据通信。
p管道文件,其主要作用是解决多个程序同时存取一个文件所造成的错误。
l链接文件,类似 Windows 系统中的快捷方式。

1.2显示命令

1.2.1 cat

选项效果
-n显示行号包括空行
-b跳过空白行编号
-s将所有的连续的多个空行替换为一个空行(压缩成一个空行)
-A显示隐藏字符

例子

[root@localhost ~]# cat /etc/centos-release
#查看你系统内核版本
CentOS Linux release 7.4.1708 (Core) 

[root@localhost ~]# cat /proc/version
Linux version 3.10.0-693.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Aug 22 21:09:27 UTC 2017
[root@localhost ~]# mkdir data
[root@localhost ~]# cd ./data
[root@localhost data]# touch 1.txt
[root@localhost data]# vim 1.txt
[root@localhost data]# cat -b 1.txt
#跳过空白行编号
     1	111


     2	123


     3	234    133

[root@localhost data]# cat -s 1.txt
#将连续的空行压缩成一个空行
111

123

234    133



[root@localhost data]# cat 1.txt
#查看文件
111


123


234    133

1.2.2 more(分页查看文件内容)

more [选项] 文件名

基本操作

指令功能
空格下一行
b上一行
enter滚动一行
q退出

例子

[root@localhost ~]# ls -lh /etc/*.conf | more
#命令显示过多也可以使用管道 加more查看

-rw-r--r--. 1 root root   55 3月   1 2017 /etc/asound.conf
-rw-r--r--. 1 root root  15K 8月   4 2017 /etc/autofs.conf
-rw-------. 1 root root  232 8月   4 2017 /etc/autofs_ldap_auth.conf
-rw-r--r--. 1 root root  22K 8月   7 2017 /etc/brltty.conf
-rw-r--r--. 1 root root  676 8月   3 2017 /etc/cgconfig.conf
-rw-r--r--. 1 root root  265 8月  31 19:02 /etc/cgrules.conf
-rw-r--r--. 1 root root  131 8月   3 2017 /etc/cgsnapshot_blacklist.conf
-rw-r--r--. 1 root root 1.1K 1月  31 2017 /etc/chrony.conf
-rw-r--r--. 1 root root 1.2K 8月   3 2017 /etc/dleyna-server-service.conf
-rw-r--r--. 1 root root  27K 8月   3 2017 /etc/dnsmasq.conf
-rw-r--r--. 1 root root 1.3K 8月   5 2017 /etc/dracut.conf
-rw-r--r--. 1 root root  112 3月  16 2017 /etc/e2fsck.conf
-rw-r--r--. 1 root root   20 6月  24 2014 /etc/fprintd.conf
-rw-r--r--. 1 root root   38 5月   2 2017 /etc/fuse.conf
-rw-r--r--. 1 root root  842 11月  6 2016 /etc/GeoIP.conf
-rw-r--r--. 1 root root    9 6月   7 2013 /etc/host.conf
-rw-r--r--. 1 root root 4.8K 8月   2 2017 /etc/idmapd.conf
-rw-r--r--. 1 root root 2.1K 8月   5 2017 /etc/ipsec.conf
-rw-r--r--. 1 root root 7.1K 8月  31 19:01 /etc/kdump.conf
-rw-r--r--. 1 root root  590 4月  29 2017 /etc/krb5.conf
-rw-r--r--. 1 root root  478 6月  13 2017 /etc/ksmtuned.conf
-rw-r--r--. 1 root root   28 2月  28 2013 /etc/ld.so.conf
-rw-r-----. 1 root root  191 4月  19 2017 /etc/libaudit.conf
-rw-r--r--. 1 root root 2.4K 10月 13 2013 /etc/libuser.conf
-rw-r--r--. 1 root root   19 8月  31 19:04 /etc/locale.conf
-rw-r--r--. 1 root root  662 7月  31 2013 /etc/logrotate.conf
-rw-r--r--. 1 root root 5.1K 6月  10 2014 /etc/man_db.conf
-rw-r--r--. 1 root root  936 8月   3 2017 /etc/mke2fs.conf
--More--
#不可以向上翻页

1.2.3 less

指令功能
空格下一行
b上一行
enter滚动一行
q退出
n向下查找
N向上查找
Page Up向上翻页
Page Down向下翻页

例子

[root@localhost ~]# ls -lh /etc/*.conf | less

-rw-r--r--. 1 root root   55 3月   1 2017 /etc/asound.conf
-rw-r--r--. 1 root root  15K 8月   4 2017 /etc/autofs.conf
-rw-------. 1 root root  232 8月   4 2017 /etc/autofs_ldap_auth.conf
-rw-r--r--. 1 root root  22K 8月   7 2017 /etc/brltty.conf
-rw-r--r--. 1 root root  676 8月   3 2017 /etc/cgconfig.conf
-rw-r--r--. 1 root root  265 8月  31 19:02 /etc/cgrules.conf
-rw-r--r--. 1 root root  131 8月   3 2017 /etc/cgsnapshot_blacklist.conf
-rw-r--r--. 1 root root 1.1K 1月  31 2017 /etc/chrony.conf
-rw-r--r--. 1 root root 1.2K 8月   3 2017 /etc/dleyna-server-service.conf
-rw-r--r--. 1 root root  27K 8月   3 2017 /etc/dnsmasq.conf
-rw-r--r--. 1 root root 1.3K 8月   5 2017 /etc/dracut.conf
-rw-r--r--. 1 root root  112 3月  16 2017 /etc/e2fsck.conf
-rw-r--r--. 1 root root   20 6月  24 2014 /etc/fprintd.conf
-rw-r--r--. 1 root root   38 5月   2 2017 /etc/fuse.conf

1.2.4 head/tail

head -n 具体数字 文件名 (不加具体的数字,默认开头十行)

tail -n 具体数字 文件名 (不加具体的数字,默认结尾十行)

tail -f 具体数字 文件名 实时跟踪最后十行

例子

[root@localhost test]# head -n -3 5.txt
#不显示最后3行
1
2
3
4
5
6
[root@localhost test]# tail -n -3 5.txt
#只显示最后三行
7
8
9
[root@localhost test]# head -n +3 5.txt
#只显示前三行
1
2
3
[root@localhost test]# tail -n +3 5.txt 
#从第三行开始
3
4
5
6
7
8
9

1.2.5 tr(转换)

tr [选项]... SET1 [SET2]

选项作用
-d删除
-s压缩
-c用字符串1中字符集的补集替换此字符集,要求字符集为ASCII

例子

[root@localhost opt]# tr 123 abc
#将出现的123 转换为abc
12345qwe
abc45qwe


[root@localhost opt]# tr 18475 abc
#最后一个一直用
13452
a3cc2

[root@localhost opt]# tr -d abc
#删除
257acbd
257d

[root@localhost opt]# tr -s " "
#压缩
1    2    3    4
1 2 3 4

[root@localhost opt]# tr -s "1"
11111
1

1.2.6 cut

wc [选项]... 目标文件..

选项作用
-l统计行数
-w统计单词个数
-c统计字节数

例子

[root@localhost test]# cat 2.txt
abc  abcd
[root@localhost test]# wc 2.txt
 1  2 10 2.txt

[root@localhost test]# cat 2.txt
abc     abcd
[root@localhost test]# wc 2.txt
 1  2 13 2.txt
 
[root@localhost test]# wc -l 2.txt   //统计行数
1 2.txt
[root@localhost test]# wc -w 2.txt   //统计单词数
2 2.txt
[root@localhost test]# wc -c 2.txt   //统计字词数
13 2.txt

1.2.8 grep (查找、过滤文件)

grep [选项]… 查找条件 目标文件

选项作用
-i查找时忽略大小写
-w只匹配完整单词
-v反向查找,输出与查找条件不相符的行
-o只显示匹配项
-f对比两个文件的相同行
-c匹配的行数

基本格式

  • 要查找的字符串以双引号括起来单引号也可以
  • “^……”表示以……开头,“……$”表示以……结尾
  • “^$”表示空行
[root@localhost opt]# cat 123.txt |grep -v '^$' >test.txt  //将非空行写入test.txt文件
[root@localhost opt]# grep "^b" 123.txt    //过滤以b开头
[root@localhost opt]#grep '/$'  123.txt    //过滤以/结尾
[root@localhost opt]# grep -v "^$" 123.txt //过滤非空行3 备份与恢复

2.压缩,解压缩

2.1 gzip bzip2

压缩

gzip [-9] 文件名…

bzip2 [-9] 文件名...

解压缩

gzip -d .gz格式的压缩文件

bzip2 -d .bz2格式的压缩文件

  • 9代表压缩比率,9最大1最小

  • gzip的压缩率 要高于bzip2

例子

[root@localhost test]# ls
1.txt  2.txt  5.txt  test.txt

[root@localhost test]# gzip *.txt  //压缩
[root@localhost test]# ls
1.txt.gz  2.txt.gz  5.txt.gz  test.txt.gz

[root@localhost test]# bzip2 -k *.txt   //压缩时保留原文件
[root@localhost test]# ls
1.txt  1.txt.bz2  2.txt  2.txt.bz2  5.txt  5.txt.bz2  test.txt  test.txt.bz2


[root@localhost test]# gzip -d *.txt.gz   //解压缩
[root@localhost test]# ls
1.txt  2.txt  5.txt  test.txt

2.2 tar(归档)

tar [选项] ... 归档文件名 源文件或目录

tar [选项] ... 归档文件名 [-C 目标目录]

选项作用
-c创建归档文件
-x解开归档文件
-C解压时指定目录
-f使用归档文件
-p打包时保留文件及目录的权限
-P打包时保留文件及目录的绝对径
-t查看包内的文件
-v输出详细信息
-j调用 bzip2 程序进行压缩或解压
-z调用 gzip 程序进行压缩或解压
[root@localhost data]# tar -zcvf vm.tar.gz 1.txt 2.txt 3.txt
#//将三个文件归档后调用gzip程序压缩成vm.tar.gz
1.txt
2.txt
3.txt
[root@localhost data]# tar -jcvf vm.tar.gz 1.txt 2.txt 3.txt
#//将三个文件归档后调用bzip2程序压缩成vm.tar.bz2
1.txt
2.txt
3.txt


[root@localhost data]# tar -zxvf vm.tar.gz -C /opt
#将vm.tar.gz文件解压缩到/opt目录下
1.txt
2.txt
3.txt