cat [-AbeEnstTuv] [--help] [--version] fileName
参数说明:
实例:
把 textfile1 的文档内容加上行号后输入 textfile2 这个文档里:
cat -n textfile1 > textfile2
把 textfile1 和 textfile2 的文档内容加上行号(空白行不加)之后将内容附加到 textfile3 文档里:
cat -b textfile1 textfile2 >> textfile3
清空 /etc/test.txt 文档内容:
cat /dev/null > /etc/test.txt
tac 参数和cat不共用,感觉tac用处不大,完全是cat倒着显示。
比如cat显示为123,tac显示为321.
tac textfile1
nl [选项]... [文件]...
参数说明:
实例:
默认情况:
[root@localhost test]# nl 4
1 111
2 222
3 333
无论空行否都添加行号:
[root@localhost test]# nl -b a 4
1 111
2 222
3
4
5 333
行号在自己栏位的最右方显示,且加 0:
[root@localhost test]# nl -n rz 4
000001 111
000002 222
000003 333
行号在萤幕的最左方显示:
[root@localhost test]# nl -n ln 4
1 111
2 222
3 333
more [-dlfpcsu] [-num] [+/pattern] [+linenum] [fileNames..]
参数说明:
实例:
从第 20 行开始显示 testfile 之文档内容。
more +20 testfile
逐页显示 testfile 文档内容,如有连续两行以上空白行则以一行空白行显示。
more -s testfile
常用操作命令
less 与 more 类似,less 可以随意浏览文件,支持翻页和搜索,支持向上翻页和向下翻页。
less [参数] 文件
参数说明:
更多详情可参考:
https://www.runoob.com/linux/linux-comm-less.html
head 命令可用于查看文件的开头部分的内容,有一个常用的参数 -n 用于显示行数,默认为 10,即显示 10 行的内容。
head [参数] [文件]
参数说明:
实例
显示 notes.log 文件的开头 5 行,请输入以下命令:
head -n 5 runoob_notes.log
显示文件前 20 个字节:
head -c 20 runoob_notes.log
tail 命令可用于查看文件的内容,有一个常用的参数 -f 常用于查阅正在改变的日志文件。
tail -f filename
会把 filename 文件里的最尾部的内容显示在屏幕上,并且不断刷新,只要 filename 更新就可以看到最新的文件内容。
tail [参数] [文件]
参数:
实例
要跟踪名为 notes.log 的文件的增长情况,请输入以下命令:
tail -f notes.log
此命令显示 notes.log 文件的最后 10 行。当将某些行添加至 notes.log 文件时,tail 命令会继续显示这些行。 显示一直继续,直到您按下(Ctrl-C)组合键停止显示。
显示文件 notes.log 的内容,从第 20 行至文件末尾:
tail -n +20 notes.log
Linux的链接分为两种:硬链接 和 软链接
硬链接: A 生成了一个硬链接 B,A和B指向同一个文件,当把A删除后,可以通过B访问到这个文件。允许一个文件拥有多个路径,用户可以通过这种机制建立硬链接到一些重要的文件上,防止误删!
软链接: 类似windows下的快捷方式,删除源文件后,快捷方式也就无法访问了。
ln(英文全拼:link files)命令是一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接。
ln [参数][源文件或目录][目标文件或目录]
其中参数的格式为
[-bdfinsvF] [-S backup-suffix] [-V {numbered,existing,simple}]
[--help] [--version] [--]
软链接:
1.软链接,以路径的形式存在。类似于Windows操作系统中的快捷方式
2.软链接可以 跨文件系统 ,硬链接不可以
3.软链接可以对一个不存在的文件名进行链接
4.软链接可以对目录进行链接
硬链接:
1.硬链接,以文件副本的形式存在。但不占用实际空间。
2.不允许给目录创建硬链接
3.硬链接只有在同一个文件系统中才能创建
必要参数说明:
-b 删除,覆盖以前建立的链接
-d 允许超级用户制作目录的硬链接
-f 强制执行
-i 交互模式,文件存在则提示用户是否覆盖
-n 把符号链接视为一般目录
-s 软链接(符号链接)
-v 显示详细的处理过程
选择参数说明:
-S "-S<字尾备份字符串> "或 “–suffix=<字尾备份字符串>”
-V “-V<备份方式>“或”–version-control=<备份方式>”
–help 显示帮助信息
–version 显示版本信息
实例:
创建 f1文件,把f1硬链接到f2,把f1软链接到f3,并修改f1的内容:
[root@localhost test]# touch f1 # 创建 一个 f1文件
[root@localhost test]# ln f1 f2 # 创建 一个 硬链接f2文件
[root@localhost test]# ln -s f1 f3 # 创建 一个 软链接(符号链接)f3文件
[root@localhost test]# ls -l
total 0
-rw-r--r--. 2 root root 0 Feb 21 13:47 f1
-rw-r--r--. 2 root root 0 Feb 21 13:47 f2
lrwxrwxrwx. 1 root root 2 Feb 21 13:48 f3 -> f1
[root@localhost test]# echo "hello world" > f1 # 给 f1 文件中写入一些东西
[root@localhost test]# ll
total 8
-rw-r--r--. 2 root root 12 Feb 21 13:52 f1
-rw-r--r--. 2 root root 12 Feb 21 13:52 f2
lrwxrwxrwx. 1 root root 2 Feb 21 13:48 f3 -> f1
[root@localhost test]# cat f1 # 查看 f1
hello world
[root@localhost test]# cat f2 # 查看 f2
hello world
[root@localhost test]# cat f3 # 查看 f3
hello world
touch命令:创建文件
echo命令: 输出字符串,也可以输出到文件中
>>和>都属于输出重定向:
>会覆盖目标的原有内容。当文件存在时会先删除原文件,再重新创建文件,然后把内容写入该文件;否则直接创建文件。
>>会在目标原有内容后追加内容。当文件存在时直接在文件末尾进行内容追加,不会删除原文件;否则直接创建文件。
删除f1后,查看f2和f3的区别:
[root@localhost test]# rm f1
rm: remove regular file ‘f1’? y
[root@localhost test]# ll
total 4
-rw-r--r--. 1 root root 12 Feb 21 14:03 f2
lrwxrwxrwx. 1 root root 2 Feb 21 13:48 f3 -> f1
[root@localhost test]# cat f2 # f2 硬链接还在
hello world
[root@localhost test]# cat f3 # f3 (软链接、符号链接)快捷方式失效了
cat: f3: No such file or directory
[root@localhost test]#
https://www.bilibili.com/video/BV187411y7hF?p=8
https://www.bilibili.com/video/BV187411y7hF?p=9