# 第六节课 文件内容操作命令

# 一、常用文件操作命令

# cat 命令 (concatenate)

cat ( concatenate ) 命令本来用于连接多个文件的内容,但在实际使用中更多地用于査看文本文件内容。cat 是应用较为广泛的文本文件内容査看命令。使用该命令时,只需要指定文件名作为参数。

cat 命令的常用选项如下。 “ -n ”选项,显示行号,包括空白行。

[root@bogon ~]# cat -n /etc/passwd
     1	root:x:0:0:root:/root:/bin/bash
     2	bin:x:1:1:bin:/bin:/sbin/nologin
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     ...
1
2
3
4
5

# more,less 命令

使用more 命令和 less 命令可以进入阅读模式,采用全屏的方式分页显示文件内容,当内容满屏时便会暂停,按空格键继续显示下一页面或按 Q 键退出,因此更适合用来阅读长文件。

more 命令示例

[root@bogon ~]# more /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup

...

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
--More--(73%)
1
2
3
4
5
6
7
8
9
10
11

less 命令示例

[root@bogon ~]# less /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup

...

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
/etc/profile
1
2
3
4
5
6
7
8
9
10
11

# head 命令和 tail命令

head 命令和 tail命令用于显示文件的局部内容,默认情况下,head 命令显示前 10行内容,tail命令显示后 10行内容

head 示例

[root@bogon ~]# head /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
1
2
3
4
5
6
7
8
9
10

tail 示例

[root@bogon ~]# tail /etc/profile
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge
1
2
3
4
5
6
7
8
9
10
11

-f ” 是 tail 命令的一个常用选项。在生产环境中,tail 命令更多地被用于査看系统日志文件,以便观察相关的网络访问、服务调试等信息。配合 “-f” 选项可以用于跟踪日志文件末尾的内容变化,实时显示更新的日志内容。

[root@bogon ~]# tail -f /var/log/messages
Oct 26 15:02:05 bogon dhclient[2970]: DHCPREQUEST on enp0s3 to 192.168.1.1 port 67 (xid=0x23e09772)
Oct 26 15:02:24 bogon dhclient[2970]: DHCPREQUEST on enp0s3 to 192.168.1.1 port 67 (xid=0x23e09772)
Oct 26 15:02:36 bogon dhclient[2970]: DHCPREQUEST on enp0s3 to 192.168.1.1 port 67 (xid=0x23e09772)
Oct 26 15:02:49 bogon dhclient[2970]: DHCPREQUEST on enp0s3 to 192.168.1.1 port 67 (xid=0x23e09772)
Oct 26 15:03:01 bogon dhclient[2970]: DHCPREQUEST on enp0s3 to 192.168.1.1 port 67 (xid=0x23e09772)
Oct 26 15:03:11 bogon dhclient[2970]: DHCPREQUEST on enp0s3 to 192.168.1.1 port 67 (xid=0x23e09772)
Oct 26 15:03:18 bogon dhclient[2970]: DHCPREQUEST on enp0s3 to 192.168.1.1 port 67 (xid=0x23e09772)
Oct 26 15:03:30 bogon dhclient[2970]: DHCPREQUEST on enp0s3 to 192.168.1.1 port 67 (xid=0x23e09772)
Oct 26 15:03:38 bogon dhclient[2970]: DHCPREQUEST on enp0s3 to 192.168.1.1 port 67 (xid=0x23e09772)
Oct 26 15:03:51 bogon dhclient[2970]: DHCPREQUEST on enp0s3 to 192.168.1.1 port 67 (xid=0x23e09772)
1
2
3
4
5
6
7
8
9
10
11

# wc 命令 (word count)

wc ( word count ) 命令用于统计指定文件中的行数、单词数和字节数。

wc命令的常用选项包括 1 (统计行数 )、-W (统计单词数)和-C ( 统计字节数 )。其中比较常用的是“ 1”选项。

[root@bogon ~]# wc /etc/passwd
  23   31 1013 /etc/passwd
  #23代表23行,31个单词,1013个字节
[root@bogon ~]# wc -l /etc/passwd
23 /etc/passwd
1
2
3
4
5

# grep 命令 (global regular expression print)

grep 命令用于在文本文件中查找并显示包含指定字符串的所有行。通过该命令,我们 可以从众多杂乱的信息中找到所需要的部分。

查询包含 root 字符的行,示例

[root@bogon ~]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
1
2
3

以 root 字符开头,示例

[root@bogon ~]# grep "^root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
1
2

以 bash 字符结尾,示例

[root@bogon ~]# grep "bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
1
2

# 管道符 “ |

通过管道符 | ,可以把多个简单的命令连接起来以实现更加复杂的功能。

管道符 T 用于连接左右两个命令,将 T 左边命令的执行结果作为 T ’右边命令的 输人,这样就像一根管道一样连接着左右两条命令,并在管道中实现数据从左至右的 传输

[root@bogon ~]# ps -ef #查看进程
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 10月26 ?      00:00:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root         2     0  0 10月26 ?      00:00:00 [kthreadd]
root         3     2  0 10月26 ?      00:00:02 [ksoftirqd/0]
...

[root@bogon ~]# ps -ef|grep nginx
root       947   510  0 10:41 pts/0    00:00:00 grep --color=auto nginx
root      3264     1  0 10月26 ?      00:00:00 nginx: master process /www/server/nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf
www       3271  3264  0 10月26 ?      00:00:00 nginx: worker process
www       3272  3264  0 10月26 ?      00:00:00 nginx: cache manager process

1
2
3
4
5
6
7
8
9
10
11
12
13

# >、>> 标准输出重定向

输出重定向使用“ > ”和“>>”操作符,分别用于覆盖、追加文件内容。

[root@bogon ~]# touch testfile
[root@bogon ~]# ls
testfile
[root@bogon ~]# cat /etc/passwd > testfile #标准输出重定向内容
[root@bogon ~]# cat testfile 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

...

www:x:1000:1000::/home/www:/sbin/nologin
mysql:x:1001:1001::/home/mysql:/sbin/nologin
redis:x:1002:1002::/home/redis:/sbin/nologin
[root@bogon ~]# echo "helloworld" >> testfile #追加内容
[root@bogon ~]# cat testfile 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

...

www:x:1000:1000::/home/www:/sbin/nologin
mysql:x:1001:1001::/home/mysql:/sbin/nologin
redis:x:1002:1002::/home/redis:/sbin/nologin
helloworld
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

# diff 命令 (different)

diff命令用于比较多个文本文件之间的差异,这在系统安全防范中非常重要。

diff命令的常用选项是“ -q ”,通过该选项可以确认两个文件是否不同。

示例

[root@bogon ~]# cp .bashrc .bashrc.bak
[root@bogon ~]# diff .bashrc .bashrc.bak 
[root@bogon ~]# echo "aaaaa" >> .bashrc.bak 
[root@bogon ~]# diff .bashrc .bashrc.bak 
12a13
> aaaaa

[root@bogon ~]# diff -q  .bashrc .bashrc.bak 
文件 .bashrc 和 .bashrc.bak 不同
1
2
3
4
5
6
7
8
9

# 二、vi 编辑器的使用 (Visual interface)

Vi 是 Linux 系统中使用极为广泛的文本编辑器。它可以在任何 Shell、字符终端或基于字符的网络连接中使用,能够髙效地在文件中进行编辑、删除、替换和移动等操作。

由于 Vi是一个工作在字符界面下的编辑器,因此它的大部分功能都是通过命令或快捷键来实现的,操作相对于那些图形界面下的编辑工具要复杂一些。当用户熟悉了 Vi 的常用操作之后,将会发现 Vi的使用也是非常灵活和便捷的。在 Vi编辑界面中,有3 种不同的工作模式:命令模式、插人模式和末行模式。

不同的工作模式所具备的功能不同。

• 命令模式。启动 Vi 编辑器后默认进人命令模式,主要完成光标移动、宇符串查找、删除、复制和粘贴等操作。不论用户处于何种模式,只要按 Esc 键,即可进人命令模式。

• 插人模式。在命令模式下,输人 i 或按 Insert 键就可以切换到插入模式。该模式中的主要操作就是输入文件内容,可以对文件正文进行修改,或者添加新的内容。处于插入模式时,Vi 编辑器的最后一行会出现插人 ”的状态提亦信息。

• 末行模式。在命令模式下,输人“:”即可进人末行模式,可以保存文件、退出编辑器,以及对文件内容进行査找、替换等操作。当处于末行模式时,Vi 编辑器的最后一行会出现 : 提示符

# 命令模式的基本操作

# 光标移动

操作类型 操作键 功能
光标方向移动 上、下、左、右
翻页 Page Up Page Down 上一页、下一页
行内快速跳转 Home End 跳转至行首、行尾
行间快速跳转 双击g 单击G 跳转文件行首、行尾
行号显示 :set nu(set number) :set nonu (set no number) 编辑器显示行号,取消显示行号

# 复制、黏贴和删除

操作类型 操作键 功能
删除 Delete dd 删除光标所在处单个字符,删除光标所在处整行
复制 yy #yy (#号代表数字,例如3yy) 复制单行或多行
黏贴 p 粘贴内容

# 内容查找

操作键 功能
?word 自上而下在文件中查找字符串“word”
n 定位下一个被匹配的找字符串
N 定位上一个被匹配的找字符串

# 撤销编辑

操作键 功能
u 取消最后一次的操作
U 取消所有操作

# 末行模式的基本操作

功能 命令
保存内容 :w
退出 vi 编辑器 :q
放弃修改并且退出 vi 编辑器 :q!
保存文件并且退出 :wq

# 可视化操作

在命令模式下输入 v 便可以进入可视模式,此时左右移动方向键,就会将光标所经过区域的字符选中,然后可以输人 y 进行复制,输人 d 进行删除,或是输人 p 进行粘贴。不论执行何种操作,在操作结束之后,都会自动退出可视模式。

另外,也可以输人 V 进人可视模式,此时就会按行选定光标所经过的区域,而之前输人 V 进人可视模式则是按字符选定内容,这是两者的不同之处。