文章目录
一、cat
1.读取文本文件里的内容
cat - concatenate files and print on the standard output
concatenate 连接
[root@localhost ~]cat sc.txt
2. nl
nl = cat -n 输出文件的时候,显示行号
-n, --number
number all output lines
[root@localhost ~]cat -n sc.txt
[root@localhost ~]# nl sc.txt
1 hunan
2 changsha
3 linux
4 sanchuang
3.多个文件的输出
[root@hz shell]# cat 文件1 文件2 文件…可以多个将文件的内容连接起来一起输出到屏幕上
[root@hz shell]# cat 文件1 文件2 文件… >文件3 将多个文件里的内容保存到另一个文件里
4.创建文件并写入内容
[root@hz shell]# cat >zhenglu.txt <<EOF
ZHENGLU
nongfa
furong
changsha
end
EOF
[root@hz shell]# ls
zhenglu.txt
[root@hz shell]# cat zhenglu.txt
ZHENGLU
nongfa
furong
changsha
end
zhenglu.txt 帮助产生一个文件zhenglu.txt --》会清空zhenglu.txt文件的内容
EOF 是自定义的结束标记, 输入的内容遇到EOF的时候,表示要结束输入 end of file (也可以用其他合法字符替换,如 end方便记忆)
5.查看系统版本
查看Rocky系统的版本
[root@huang ~]# cat /etc/rocky-release
Rocky Linux release 9.4 (Blue Onyx)
查看Ubuntu系统的版本
root@huang:~# cat /etc/issue
Ubuntu 24.04 LTS \n \l
二、head
默认显示文件前10行
[root@hz ~]# head -3 /etc/passwd 显示前3行
[root@hz ~]# head -n 3 /etc/passwd 显示前3行
root:x :0:0:root:/root:/bin/bash
bin:x :1:1:bin:/bin:/sbin/nologin
daemon:x :2:2:daemon:/sbin:/sbin/nologin
三、tail
默认显示文件后10行
[root@hz ~]# tail /etc/passwd
pengyaqin:x :1027:1027::/home/pengyaqin:/bin/bash
caojie:x :1028:1028::/home/caojie:/bin/bash
luobiao:x :1029:1029::/home/luobiao:/bin/bash
zouqiang:x :1030:1030::/home/zouqiang:/bin/bash
panmingqian:x :1031:1031::/home/panmingqian:/bin/bash
xiaofeng:x :1032:1032::/home/xiaofeng:/bin/bash
xiaozhang:x :1033:1033::/home/xiaozhang:/bin/bash
feng:x :1034:1034::/home/feng:/bin/bash
rose:x :1035:1035::/home/rose:/bin/bash
jack:x :1036:1036::/home/jack:/bin/bash
tail -n 2 取最后两行
tail - n -2
tail -2
高级用法
tail -n +2 从第二行开始到末尾
tail -f sc.txt 动态监控文件末尾的变化,一旦有新的内容,
立马在屏幕上输出--》监控某个文件的变化,日志文件
日志文件:程序在运行的过程中,会记录发生的事情的文件 log
tailf = tail -f
可 ctrl+c 强行终止
四、sed
打印特定行 print
[root@hz shell]# sed -n ‘3p;5p;7,9p’ /etc/passwd
daemon:x :2:2:daemon:/sbin:/sbin/nologin
lp:x :4:7:lp:/var/spool/lpd:/sbin/nologin
shutdown:x :6:0:shutdown:/sbin:/sbin/shutdown
halt:x :7:0:halt:/sbin:/sbin/halt
mail:x :8:12:mail:/var/spool/mail:/sbin/nologin
[root@hz shell]# nl /etc/passwd|sed -n ‘3p;5p;7,9p’
3 daemon:x :2:2:daemon:/sbin:/sbin/nologin
5 lp:x :4:7:lp:/var/spool/lpd:/sbin/nologin
7 shutdown:x :6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x :7:0:halt:/sbin:/sbin/halt
9 mail:x :8:12:mail:/var/spool/mail:/sbin/nologin
五、more/less
分页显示
1.more
回车 一行一行往下显示
空格 下一页 -->pgdn
b 上一页 back --》pgup
q 退出 quit
(显示百分比,看完会自动退出)
2.less
回车 一行一行往下显示
空格 下一页 -->pgdn
b 上一页 back --》pgup
q 退出 quit
(不显示百分比,需要手动退出)
六、 |
管道 pipe:是实现进程和进程之间通信的一种方式。把前面一个命令的输出送给后面一个命令作为输出
[root@localhost ~]# ps -aux | wc -l 查看所有的进程并统计行数
172
ps 是查看进程的命令
七、wc
用于统计文件字数、行数、字节数等信息
[root@localhost ~]# wc sc.txt
14 13 103 sc.txt
[root@localhost ~]# wc -l sc.txt
14 sc.txt
八、grep
文本过滤的命令,有匹配的内容,会显示整行
[root@hz /]# ps aux|grep bash 查看所有的进程,然后过滤出bush程序
root 1342 0.0 0.1 7788 4224 pts/0 Ss 15:39 0:00 -bas
root 2079 0.0 0.0 6632 2560 pts/0 S+ 19:03 0:00 grep --color=auto bash
九、du
查看文件或者文件夹的大小
[root@fengdeyong ~]# du -sh hello.c
4.0K hello.c
[root@fengdeyong ~]# du -sh /boot
118M /boot
十、stat
查看文件的状态
huang@huang:~$ stat sanchuang.txt
File: sanchuang.txt
Size: 32 Blocks: 8 IO Block: 4096 regular file
Device: 252,0 Inode: 524306 Links: 1
Access: (0664/-rw-rw-r–) Uid: ( 1000/ huang) Gid: ( 1000/ huang)
Access: 2025-05-13 11:51:18.612042333 +0000
Modify: 2025-05-13 11:51:08.636958209 +0000
Change: 2025-05-13 11:51:08.636958209 +0000
atime 访问时间 access
mtime 修改时间 modify
ctime 修改过文件属性的时间 change
例如:大小,用户,组等属性
十一.file
查看文件的类型
[root@fengdeyong china]# file xiang
xiang: directory
总结
以下是对文档中 Linux 常用文件查看命令的简短总结,涵盖命令功能、典型用法及核心要点:
一、文件内容查看与处理
1.cat
2.head/tail
3.sed
二、分页与过滤
1.more/less
2.grep
三、管道
|
四、文件大小与属性
1.du
2…wc
3.stat
4.file