一起来学Linux命令(三)

前言

目前正在出一个Linux命令系列教程, 篇幅会较多, 喜欢的话,给个关注❤️ ~

作为服务端开发,linux命令还是要掌握一下的,可以做做基础性的运维。好了, 废话不多说直接开整吧~

shell 复制代码
-n 展示前n行

-c 展示前n个字符

使用示例:

shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z head]# touch a.txt
[root@iZ2ze5vrnucj8nu52fq932Z head]# echo 'hello1' >> a.txt
[root@iZ2ze5vrnucj8nu52fq932Z head]# echo 'hello2' >> a.txt
[root@iZ2ze5vrnucj8nu52fq932Z head]# echo 'hello3' >> a.txt
[root@iZ2ze5vrnucj8nu52fq932Z head]# echo 'hello4' >> a.txt
[root@iZ2ze5vrnucj8nu52fq932Z head]# echo 'hello5' >> a.txt
[root@iZ2ze5vrnucj8nu52fq932Z head]# cat a.txt
hello1
hello2
hello3
hello4
hello5
[root@iZ2ze5vrnucj8nu52fq932Z head]# head -n 3 a.txt
hello1
hello2
hello3

[root@iZ2ze5vrnucj8nu52fq932Z head]# head -c 1 a.txt
h
[root@iZ2ze5vrnucj8nu52fq932Z head]# 

tail

tail命令完全和head相反,是从尾部开始展示文本

shell 复制代码
-f 循环读取

-q 不显示处理信息

-v 显示详细的处理信息

-c<数目> 显示的字节数

-n<行数> 显示行数

--pid=PID 与-f合用,表示在进程ID,PID死掉之后结束.

-q, --quiet, --silent 从不输出给出文件名的首部

-s, --sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒

tail命令非常的时候我们看服务的运行日志,有时候日志文件几十个G,如果用cat就不大适合了,全部输出出来,得把屏幕铺满,而且要花费大量时间

shell 复制代码
# 循环读取 我们查看正在运行中的日志可以使用这个命令 可以看到实时的日志打印
[root@iZ2ze5vrnucj8nu52fq932Z head]# tail -f a.txt
hello1
hello2
hello3
hello4
hello5


[root@iZ2ze5vrnucj8nu52fq932Z head]# tail -q a.txt
hello1
hello2
hello3
hello4
hello5
[root@iZ2ze5vrnucj8nu52fq932Z head]# tail -v a.txt
==> a.txt <==
hello1
hello2
hello3
hello4
hello5
[root@iZ2ze5vrnucj8nu52fq932Z head]# tail -c 10 a.txt
o4
hello5
[root@iZ2ze5vrnucj8nu52fq932Z head]# 
[root@iZ2ze5vrnucj8nu52fq932Z head]# tail -n 1 a.txt
hello5
[root@iZ2ze5vrnucj8nu52fq932Z head]# tail -pid 1 a.txt

more

shell 复制代码
+n   从笫n行开始显示

-n    定义屏幕大小为n行

+/pattern 在每个档案显示前搜寻该字串(pattern),然后从该字串前两行之后开始显示

-c    从顶部清屏,然后显示

-d    提示"Press space to continue,'q' to quit(按空格键继续,按q键退出)

-l    忽略Ctrl+l(换页)字符

-p    通过清除窗口而不是滚屏来对文件进行换页,与-c选项相似

-s    把连续的多个空行显示为一行

-u    把文件内容中的下画线去掉
shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z head]# more +3 a.txt
hello3
hello4
hello5


[root@iZ2ze5vrnucj8nu52fq932Z head]# more -3 a.txt
hello1
hello2
hello3
--More--(60%)

# 点回车会继续输出

hello4
hello5


# 在每个档案显示前搜寻该字串(pattern),然后从该字串前两行之后开始显示
[root@iZ2ze5vrnucj8nu52fq932Z head]# more +/llo5 a.txt

...skipping
hello3
hello4
hello5
[root@iZ2ze5vrnucj8nu52fq932Z head]# 



# 先清屏再显示
[root@iZ2ze5vrnucj8nu52fq932Z head]# more -c a.txt
hello1
hello2
hello3
hello4
hello5
[root@iZ2ze5vrnucj8nu52fq932Z head]# 


# 提示"Press space to continue,'q' to quit(按空格键继续,按q键退出)
[root@iZ2ze5vrnucj8nu52fq932Z head]# more -1 -d a.txt
hello1
--More--(20%)[Press space to continue, 'q' to quit.]

# 按空格会继续输出
hello2
hello3
hello4
hello5
[root@iZ2ze5vrnucj8nu52fq932Z head]#  

lessmore 类似,但使用 less 可以随意浏览文件,而 more 仅能向前移动,却不能向后移动,而且 less 在查看之前不会加载整个文件, 这个大家可以自己试下

shell 复制代码
# 这里给大家讲下如何翻页 和 跳转

上下箭头:向上、向下滚动。

空格:向下翻页。

b:向上翻页。

g:跳到文件开头。

G:跳到文件结尾。

q:退出 less。

wc

shell 复制代码
-c 统计字节数。

-l 统计行数。

-m 统计字符数。这个标志不能与 -c 标志一起使用。

-w 统计字数。一个字被定义为由空白、跳格或换行字符分隔的字符串。

-L 打印最长行的长度。

使用示例:

shell 复制代码
[root@iZ2ze5vrnucj8nu52fq932Z head]# wc -c a.txt
35 a.txt
[root@iZ2ze5vrnucj8nu52fq932Z head]# wc -l a.txt
5 a.txt
[root@iZ2ze5vrnucj8nu52fq932Z head]# wc -m a.txt
35 a.txt
[root@iZ2ze5vrnucj8nu52fq932Z head]# wc -w a.txt
5 a.txt
[root@iZ2ze5vrnucj8nu52fq932Z head]# wc -L a.txt
6 a.txt

[root@iZ2ze5vrnucj8nu52fq932Z head]# 

结束语

命令很多,大家不用去背,可以放到便签之类的工具中,用到的时候翻一下就好~

本着把自己知道的都告诉大家,如果本文对您有所帮助,点赞+关注鼓励一下呗~

Linux相关文章

往期面试题相关文章

项目源码(源码已更新 欢迎star⭐️)

往期设计模式相关文章

设计模式项目源码(源码已更新 欢迎star⭐️)

Kafka 专题学习

项目源码(源码已更新 欢迎star⭐️)

ElasticSearch 专题学习

项目源码(源码已更新 欢迎star⭐️)

往期并发编程内容推荐

推荐 SpringBoot & SpringCloud (源码已更新 欢迎star⭐️)

博客(阅读体验较佳)

相关推荐
BestOrNothing_20156 小时前
(2)联想拯救者安装 Ubuntu 双系统前的 BIOS 设置全过程
linux·bios·拯救者·ubuntu22.04·联想lenovo
23.6 小时前
【Linux】grep命令终极指南
linux
巨斧空间掌门6 小时前
JDK17 下载 windows Linux
linux·运维·服务器
短剑重铸之日6 小时前
《ShardingSphere解读》07 读写分离:如何集成分库分表+数据库主从架构?
java·数据库·后端·架构·shardingsphere·分库分表
AI+程序员在路上6 小时前
CANopen 协议:介绍、调试命令与应用
linux·c语言·开发语言·网络
wefly20177 小时前
m3u8live.cn 在线M3U8播放器,免安装高效验流排错
前端·后端·python·音视频·前端开发工具
learndiary7 小时前
2026.03.12~2026.03.19制作的共7个视频及简介
linux·视频·学习日记小店
JiMoKuangXiangQu7 小时前
Linux:ARM64 中断处理简析
linux·arm64 中断
小生不才yz7 小时前
【Makefile 专家之路 | 函数篇】11. 终极奥义:eval 函数——动态生成规则的“核武器”
linux
皮卡蛋炒饭.8 小时前
进程得控制
linux·运维·服务器