RHCSA Linux 系统 文件的查看、复制、移动、重命名、编辑文件
一.查看
1. cat 命令
① 用途:它能一次显示整个文件内容,适合查看小文件。
② 命令格式:cat [选项] 文件名
③ eg:
☛cat text.txt
此命令会直接在终端显示 text.txt 文件的全部内容。
☛cat -n text.txt
给 text.txt 文件的每一行加上行号后显示。
2. more 命令
① 用途:用于查看大文件,它会分页显示文件内容,用户可通过按空格键翻页,按 Enter 键逐行向下查看。
② 命令格式:more [选项] 文件名
③ eg: more largefile.txt 逐页查看 largefile.txt 文件内容。
3. less 命令
① 用途:同样用于查看大文件,相比 more 功能更强大,支持向前和向后翻页,还能进行搜索等操作。
② 命令格式:less [选项] 文件名
③ eg:
less bigfile.log:查看 bigfile.log 文件内容。
在 less 查看模式下,输入 / 关键词可以搜索文件中包含该关键词的行。
4. head 命令
① 用途:显示文件的开头部分内容,默认前 10 行。
② 语法:head [选项] 文件名
③ eg:
☛head example.txt:显示 example.txt 文件的前 10 行内容。
☛head -n 5 example.txt:显示 example.txt 文件的前 5 行内容。
5. tail 命令
① 用途:显示文件的结尾部分内容,默认显示后 10 行,常用于查看日志文件的最新记录。
② 语法:tail [选项] 文件名
③ eg:
☛tail log.txt:显示 log.txt 文件的后 10 行内容。
☛tail -n 3 log.txt:显示 log.tx
t 文件的后 3 行内容。
☛tail -f log.txt:实时跟踪更新,新写入的内容会立即显示在终端。
二. 复制
命令格式:cp 源文件 (对谁复制) 目标文件 (复制到哪里)
eg:
☛ [root@localhost ~]# cp /root/aaa/am
把 /root 下的 aaa 复制到 am 目录去。
☛ [root@localhost ~]# cp /root/1 /root/2 /root/3 /mnt
把 /root 下的 1、2、3 文件复制到 /mnt 目录下。
三. 移动
命令格式:mv 源文件 目标文件
eg:
☛ [root@localhost ~]# mv /file/root
把 file 移动到 /root 目录下。
四. 重命名
命令格式: mv 源文件 目标文件
eg:
☛ [root@localhost ~]# mv /file0 /file - hardlink
把 /file0 名字改为 /file - hardlink。
☛ [root@localhost ~]# mv /file1 /root/file2
把 /file1 移动到 /root 目录下,并且名字改成 file2。
五.编辑文件
1. 创建文件并写入内容
[root@localhost ~]# echo dhfsojdo > /root/file1
将字符串dhfsojdo写入到/root/file1文件中。
如果文件不存在则创建。
2. 查看文件内容
[root@localhost ~]# cat /file1
1234
3. 覆盖写入文件内容
[root@localhost ~]# echo qwe > file1
将字符串qwe写入file1,会覆盖原文件内容。
[root@localhost ~]# cat /file1
qwe
4. 追加写入文件内容
[root@localhost ~]# echo qwe >> /file1
将字符串qwe追加到/file1文件末尾。
[root@localhost ~]# cat /file1
qwe
qwe