Linux查看文件内容和压缩文件命令

210 阅读3分钟

Linux查看文件内容和压缩文件命令

查看文件内容

一些重要的目录:

  • 根(/)相当于一个入口,文件的总入口,也是唯一入口
  • bin (sbin) 二进制文件,可执行文件,也可能存在别处,默认使用较多所有用户软连接文件 -------命令
  • sbin 管理员可使用的二进制文件-------------命令ls cd useradd
  • boot 系统内核、启动文件
  • dev 硬件文件(黄色)
  • etc 配置文件等同于windows 注册表,一般都是文本文件
  • usr 应用程序和操作系统相关的文件 bin sbin
  • var/log 日志文件,数据变化的
  • proc 主要保存系统的内核、进程、外部设备状态和网络状态等
  • mnt 默认挂载点
  • opt 第三方应用程序安装所在目录程序

Cat(查看文件内容)

格式:

cat [选项] 文件名

选项:

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

示例:

Cat 查看文件内容

[root@localhost jlx]#cat passwd   //查看passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

cat加选项

-n
[root@localhost jlx]#cat -n 1.txt  //显示行号包括空行 
     1	111
     2	
     3	
     4	222
     5	
     6	
     7	333

-b
[root@localhost jlx]#cat -b 1.txt  //跳过空白行编号 
     1	111


     2	222


     3	333
     
-s     
[root@localhost jlx]#cat -s 1.txt  //将所有的连续的多个空行替换为一个空行
111

222

333

-A
[root@localhost jlx]#cat -A 1.txt  //显示隐藏字符
111$
$
$
222$
$
$
333$

more(分页查看文件内容)

格式:

more [选项] 文件名

操作说明:

指令功能
空格向下 一页
回车向下一行
b向上
q退出

示例

[root@localhost jlx]#more /etc/*.conf
::::::::::::::
/etc/asound.conf
::::::::::::::
#
# Place your global alsa-lib configuration here...
#
::::::::::::::
/etc/autofs.conf
::::::::::::::
#
# Define default options for autofs.
#
[ autofs ]
#
# master_map_name - default map name for the master map.
#
#master_map_name = auto.master
#
# timeout - set the default mount timeout in secons. The internal
#	    program default is 10 minutes, but the default installed
#	    configuration overrides this and sets the timeout to 5
#	    minutes to be consistent with earlier autofs releases.
#
timeout = 300
#
# master_wait - set the default maximum time to wait for the
# 		master map to become available if it cannot
# 		be read at program start (default 10, wait
# 		for 10 seconds then continue).
--More--(4%)

less(分页查看文件内容)

格式:

less [选项] 文件名

操作说明:

指令功能
空格向下 一页
回车向下一行
b向上
q退出
/查找内容
n向下查找
N向上查找

示例:

[root@localhost jlx]#less /etc/*.conf
::::::::::::::
/etc/asound.conf
::::::::::::::
#
# Place your global alsa-lib configuration here...
#
::::::::::::::
/etc/autofs.conf
::::::::::::::
#
# Define default options for autofs.
#
[ autofs ]
#
# master_map_name - default map name for the master map.
#
#master_map_name = auto.master
#
# timeout - set the default mount timeout in secons. The internal
#	    program default is 10 minutes, but the default installed
#	    configuration overrides this and sets the timeout to 5
#	    minutes to be consistent with earlier autofs releases.
#
timeout = 300
#
# master_wait - set the default maximum time to wait for the
# 		master map to become available if it cannot
# 		be read at program start (default 10, wait
# 		for 10 seconds then continue).

head/tail(查看头部和尾部内容)

格式:

head -n <行数> 文件名 (不加行数默认开头十行)

tail -n<行数> 文件名 (不加行数默认结尾十行)

tail -f 文件名:实时跟踪最后十行

示例:

[root@localhost jlx]#head -n +3 2.txt  //+3 只显示前三行
1
2
3
[root@localhost jlx]#tail -n +3 2.txt  //+3 从第三行开始
3
4
5
6
7
8
9
10
11
12
13
              //此处为空格
[root@localhost jlx]#head -n -3 2.txt  //-3 不显示最后3行
1
2
3
4
5
6
7
8
9
10
11
[root@localhost jlx]#tail -n -3 2.txt  //-3 只显示最后三行
12
13

“ | ” 管道符 将前面命令的结果 交给后面的命令执行 tips:不是所有的命令都支持管道符

拓展:

通过组合完成要求: 我只要第二行

[root@localhost jlx]#cat 2.txt|head -n 2|tail -n 1
2

wc(统计文件中的信息)

格式:

wc [选项] 目标文件

不加选项,则默认统计行数、单词个数、字节数三者

  • -l:统计行数
  • -w:统计单词个数
  • -c:统计字节数

示例:

[root@localhost jlx]#wc fstab     //默认都统计
 12  60 541 fstab
[root@localhost jlx]#wc -l fstab  //统计行数,注意空行也会被统计
12 fstab
[root@localhost jlx]#wc -w fstab  //统计单词数
60 fstab
[root@localhost jlx]#wc -c fstab  //统计字节数,注意换行符也会被统计
541 fstab

grep(66检索、过滤文件内容)

格式:

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

选项:

选项作用
-i查找时忽略大小写
-v反向查找,输出与查找条件不相符的行
-o只显示匹配项
-r在目录内递归查找
-n显示匹配行及行号

示例:

[root@localhost jlx]#grep -i root passwd  //查找时忽略大小写
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost jlx]#grep -v root passwd  //反向查找,输出与查找条件不相符的行
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[root@localhost jlx]#grep -o root passwd  //只显示匹配项
root
root
root
root

[root@localhost jlx]#grep -r root passwd  //在目录内递归查找
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost jlx]#grep -n root passwd  //显示匹配行及行号
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

正则表达式:

  • ^ 以什么开头 放最前
  • $ 以什么什么结尾 放最后
  • 空白行怎么表示?------^$
  • grep -n 'bash$' passwd 过滤出以bash结尾的行
  • grep -n '^$' fstab 过滤出找出空白行
  • grep -nv '^$' fstab 找出非空白行

压缩文件

gzip和bzip2

区别:

Bzip2 -k 保留源文件,压缩比gzip 要好一点

常用选项:

标题
-9压缩
-d解压缩

格式:

  • 压缩

gzip [-9] 文件名

bzip2 [-9] 文件名

  • 解压缩

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

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

示例:

[root@localhost jlx]#ls
1.txt  2.txt  fstab  passwd
[root@localhost jlx]#bzip2 1.txt
[root@localhost jlx]#ls
1.txt.bz2  2.txt  fstab  passwd
[root@localhost jlx]#bzip2 -k 2.txt  //压缩后保留源文件
[root@localhost jlx]#ls
1.txt.bz2  2.txt  2.txt.bz2  fstab  passwd
[root@localhost jlx]#bzip2 -d 1.txt.bz2  //解压缩
[root@localhost jlx]#ls
1.txt  2.txt  2.txt.bz2  fstab  passwd

tar(归档)

格式:

tar [选项] 归档文件名(压缩包名字)

选项:

标题
-f代表使用归档
-c建立归档文件
-x解开归档文件
-p保留源文件权限
-v显示归档过程 , 解归档过程
-C指定解压目录
-z代表使用 gzip 压缩
-j代表使用 bzip2 压缩
-t不解开归档文件 只查看归档文件中的内容

选项有顺序要求:
如果你加了-    f 一定要在最后

示例

[root@localhost data]# tar -zcvf vm.tar.gz f1 f2 f3   //将三个文件归档后调用gzip程序压缩成vm.tar.gz
f1
f2
f3
[root@localhost data]# tar -jcvf vm.tar.bz f1 f2 f3   //将三个文件归档后调用bzip2程序压缩成vm.tar.bz2
f1
f2
f3

[root@localhost data]# tar -zxvf vm.tar.gz -C /opt    //将vm.tar.gz文件解压缩到/opt目录下
f1
f2
f3

[root@localhost data]# tar -tf ff.tar.gzip    //列出归档内容
file01
file02

[root@localhost data]# tar -tvf /tmp/ceshi/ff.tar   //详细列举归档文件中的所有文件(包括属性信息)
-rwxr--r-- root/root        87 2022-01-21 17:37 file01
-rw-r--r-- user01/hr         0 2022-01-19 17:01 file02

总结:

需要熟练掌握各个Linux命令的选项的用法以及用意
学会各个命令的搭配组合使用