1. Linux 查看文件
在 Linux 中,常用的查看文件的命令有 cat、more、less、head/tail
命令 | 描述 |
---|---|
cat | catenate,查看较小的文本文件、连接合并文件 |
more | 分屏查看较大的文本文件 |
less | 查看较大文件的内容 |
head | 从头部查看文件(默认显示10行) |
tail | 从尾部查看文件(默认显示10行) |
1.1 cat
1.1.1 cat 查看文件
语法格式:cat [选项] 文件
功能描述:查看较小的文件
选项参数:
选项 | 描述 |
---|---|
-A | 相当于 -vET 选项的整合,用于列出所有隐藏符号; |
-E | 列出每行结尾的回车符 $; |
-n | 对输出的所有行进行编号; |
-b | 同 -n 不同,此选项表示只对非空行进行编号。 |
-T | 把 Tab 键 ^I 显示出来; |
-V | 列出特殊字符; |
-s | 当遇到有连续 2 行以上的空白行时,就替换为 1 行的空白行。 |
案例演示:
[root@joel ~]# cat file.txt
Today I shed my old skin which hath, too long, suffered the bruises of failure and the wounds of mediority.
[root@joel ~]# cat -n file.txt
1 Today I shed my old skin which hath, too long, suffered the bruises of failure and the wounds of mediority.
2
3 Today I am born anew and my birthplace is a vineyard where there is fruit for all.
使用 cat 为正序打印,使用 tac 为逆序打印
1.1.2 cat 连接文件
语法格式:cat 文件1 文件2 > 新文件
功能描述:将两个文件中的内容合并到新的文件中
案例演示:
[root@joel ~]# cat test1.txt
Hello
[root@joel ~]# cat test2.txt
World!
[root@joel ~]# cat test1.txt test2.txt > test3.txt
# 查看结果
[root@joel ~]# cat test3.txt
Hello
World!
>
为重定向符,新文件可为不存在的文件
1.2 more
语法格式:more [选项] 文件
功能描述:分屏查看较大的文本文件
选项参数:
选项 | 描述 |
---|---|
-f | 计算行数时,以实际的行数,而不是自动换行过后的行数。 |
-p | 不以卷动的方式显示每一页,而是先清除屏幕后再显示内容。 |
-c | 跟 -p 选项相似,不同的是先显示内容再清除其他旧资料。 |
-s | 当遇到有连续两行以上的空白行时,就替换为一行的空白行。 |
-u | 不显示下引号(根据环境变量 TERM 指定的终端而有所不同)。 |
+n | 从第 n 行开始显示文件内容,n 代表数字。 |
-n | 一次显示的行数,n 代表数字。 |
交互指令:
交互指令 | 描述 |
---|---|
h 或 ? | 显示 more 命令交互命令帮助 |
q 或 Q | 退出 more |
回车键 | 向下移动一行 |
空格键 | 向下移动一页 |
d | 向下移动半页 |
b | 向上移动一页 |
… | … |
案例演示:
[root@joel ~]# more file.txt
Today I shed my old skin which hath, too long, suffered the bruises of failure and the w
ounds of mediority.
Today I am born anew and my birthplace is a vineyard where there is fruit for all.
--More--(57%)
1.3 less
语法格式:more [选项] 文件
功能描述:查看较大文件的内容
选项参数:
选项 | 描述 |
---|---|
-f | 计算行数时,以实际的行数,而不是自动换行过后的行数。 |
-p | 不以卷动的方式显示每一页,而是先清除屏幕后再显示内容。 |
-c | 跟 -p 选项相似,不同的是先显示内容再清除其他旧资料。 |
-s | 当遇到有连续两行以上的空白行时,就替换为一行的空白行。 |
-u | 不显示下引号(根据环境变量 TERM 指定的终端而有所不同)。 |
+n | 从第 n 行开始显示文件内容,n 代表数字。 |
-n | 一次显示的行数,n 代表数字。 |
交互指令:
交互指令 | 描述 |
---|---|
h 或 ? | 显示 more 命令交互命令帮助 |
q 或 Q | 退出 more |
回车键 | 向下移动一行 |
空格键 | 向下移动一页 |
d | 向下移动半页 |
b | 向上移动一页 |
… | … |
案例演示:
[root@joel ~]# more +5 file.txt
Today I will pluck grapes of wisdom from the tallest and fullest vines in the vineya
rd,for these were planted by the wisest of my profession who have come before me,generat
ion upon generation.
Today I will savor the taste of grapes from these vines and verily I will swallow th
e seed of success buried in each and new life will sprout within me.
--More--(57%)
1.4 head
语法格式:head [选项] 参数
功能描述:从头部查看文件(默认显示10行)
选项参数:
选项 | 描述 |
---|---|
-n K | 这里的 K 表示行数,该选项用来显示文件前 K 行的内容;如果使用 “-K” 作为参数,则表示除了文件最后 K 行外,显示剩余的全部内容 |
-c K | 这里的 K 表示字节数,该选项用来显示文件前 K 个字节的内容;如果使用 “-K”,则表示除了文件最后 K 字节的内容,显示剩余全部内容 |
-v | 显示文件名 |
案例演示:
[root@joel ~]# head -v file.txt
==> file.txt <==
Today I shed my old skin which hath, too long, suffered the bruises of failure and the wounds of mediority.
Today I am born anew and my birthplace is a vineyard where there is fruit for all.
Today I will pluck grapes of wisdom from the tallest and fullest vines in the vineyard,for these were planted by the wisest of my profession who have come before me,generation upon generation.
# 显示文件开头的3行
[root@joel ~]# head -3 file.txt
1.5 tail
语法格式:tail [选项] 文件
功能描述: 从尾部查看文件(默认显示10行)
选项参数:
选项 | 描述 |
---|---|
-n K | 这里的 K 指的是行数,该选项表示输出最后 K 行,在此基础上,如果使用 -n +K,则表示从文件的第 K 行开始输出。 |
-c K | 这里的 K 指的是字节数,该选项表示输出文件最后 K 个字节的内容,在此基础上,使用 -c +K 则表示从文件第 K 个字节开始输出。 |
-f | 输出文件变化后新增加的数据。 |
案例演示:
# 显示文件后20个字节
[root@joel ~]# tail -c 20 file.txt
seemed but a dream.
2. Linux 查找文件
在 Linux 中,常用的查找文件的命令有 find、grep、wc、sort、diff
命令 | 描述 |
---|---|
find | 在指定的目录结构中根据条件搜索文件 |
grep | 在文本中搜索匹配的内容,并输出,支持正则表达式 |
wc | 显示文件行数、单词数、字节数 |
sort | 以行为单位,对文本进行排序 |
diff | 逐行比较两个文件的差异,文件相同则不显示结果 |
2.1 find
语法格式:find [查找范围] <查找条件>
功能描述:在指定的目录结构中搜索文件
查找范围: 指定要查找的目录 ,省略表示在当前目录及子目录中查找
查找条件: 指定查找文件的具体条件,如:文件名、类型、大小、修改时间等
2.1.1 根据文件名查找
语法格式:
find [ 查范围 ] -name 文件名
根据文件名查找find [ 查找范围 ] -iname 文件名
据文件名查找,忽略大小写
案例演示:
[root@joel ~]# find . -name test
./demo/test
[root@joel ~]# find . -iname test
./demo/test
./demo/Test
2.1.2 根据文件类型查找
语法格式:find [ 查找范围 ] –type
文件类型字符
案例演示:
[root@joel ~]# find . -type d
.
./.ssh
./.pip
./demo
./.cache
./.pki
[root@joel ~]# ls -la
total 92
dr-xr-x---. 7 root root 4096 Nov 11 19:33 .
dr-xr-xr-x. 18 root root 4096 Nov 6 15:15 ..
drwxr-xr-x 3 root root 4096 Nov 11 20:18 demo
drwxr-xr-x 2 root root 4096 Sep 14 2020 .pip
drwxr----- 3 root root 4096 Aug 2 16:41 .pki
drwx------ 2 root root 4096 Sep 14 2020 .ssh
2.1.2 根据时间属性查找
语法格式:find [ 查找范围 ] <时间指令> n
查找过去n天被修改过的文件
时间指令:
时间指令 | 描述 |
---|---|
-mmin n | 在过去 n 分钟内被修改过 |
-atime n | 在过去n天内被读取/root/tmp执行过的文件 |
-amin n | 在过去 n 分钟内被读取/root/tmp执行过的文件 |
-ctime n | 在过去n 天内属性被修改过的文件 |
-cmin n | 在过去n 分钟内属性被修改过的文件 |
-n 表示 n 内,+n 表示 n 前,n 表示第 n
2.1.3 find 的复合条件
-a
:and,表示“与”
`-o :or,表示“或”
-!
:not,表示“取反”
2.1.4 find 其他条件
条件 | 描述 |
---|---|
-size -n c/k/m/g | 根据文件大小(n/nk/nm/ng字节)查找 |
-perm 文件权限数字 | 根据文件的权限进行查找 |
-user ‘用户名’ | 根据用户名查找 |
-uid 用户id | 根据用户id查找 |
-group ‘用户组名‘ | 根据用户组名查找 |
-gid 用户id | 根据用户组id查找 |
2.2 grep
语法格式:grep [选项] 待查找的字符串 文件
功能描述:在文本中搜索匹配的内容,并输出,支持正则表达式
选项参数:
选项 | 描述 |
---|---|
-c | 仅列出文件中包含模式的行数 |
-i | 忽略模式中的字母大小写 |
-l | 列出带有匹配行的文件名 |
-n | 在每一行的最前面列出行号 |
-v | 列出没有匹配模式的行 |
-w | 把表达式当做一个完整的单字符来搜寻,忽略那些部分匹配的行 |
案例演示:
# 查看文件中含有 the 的行数
[root@joel ~]# grep -c the file.txt
6
2.3 wc
语法格式:wc [选项] 文件
功能描述:显示文件行数、单词数、字节数
选项参数:
选项 | 描述 |
---|---|
-c | bytes 或 chars,只显示bytes数 |
-l | lines,只显示行数 |
-w | words,只显示单词数 |
案例演示:
[root@joel ~]# wc -lwc file.txt
11 174 971 file.txt
2.4 sort
语法格式:sort [选项] 文件
功能描述:以行为单位,对文本进行排序
去重复行:-u
案例演示:
[root@joel ~]# cat test.txt
a123
c123
b123
a123
[root@joel ~]# sort test.txt
a123
a123
b123
c123
[root@joel ~]# sort -u test.txt
a123
b123
c123
2.5 diff
语法格式:diff [选项] 文件1 文件2
功能描述:逐行比较两个文件的差异,文件相同则不显示结果
显示相同:-c
案例演示:
[root@joel ~]# cat test.txt
a123
c123
b123
a123
[root@joel ~]# cat test1.txt
a123
c123
c123
a123
[root@joel ~]# diff test.txt test1.txt
3c3
< b123
---
> c123
[root@joel ~]# diff -c test.txt test1.txt
*** test.txt 2021-11-11 22:30:57.495476892 +0800
--- test1.txt 2021-11-11 22:35:45.385206355 +0800
***************
*** 1,4 ****
a123
c123
! b123
a123
--- 1,4 ----
a123
c123
! c123
a123