一起来学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⭐️)

博客(阅读体验较佳)

相关推荐
小旭952717 分钟前
Spring Security 实现权限控制(认证 + 授权全流程)
java·后端·spring
weixin_4080996718 分钟前
【完整教程】天诺脚本如何调用 OCR 文字识别 API?自动识别屏幕文字实战(附代码)
前端·人工智能·后端·ocr·api·天诺脚本·自动识别文字脚本
金銀銅鐵23 分钟前
[Java] 如何通过 cglib 的 FastClass 调用一个类中的“任意”方法?
java·后端
程序猿编码1 小时前
一个授予普通进程ROOT权限的Linux内核级后门:原理与实现深度解析
linux·运维·服务器·内核·root权限
小夏子_riotous1 小时前
openstack的使用——9. 密钥管理服务Barbican
linux·运维·服务器·系统架构·centos·云计算·openstack
宠友信息1 小时前
一套基于uniapp+springboot完整社区系统是如何实现的?友猫社区源码级功能解析
java·spring boot·后端·微服务·微信·uni-app
六点的晨曦2 小时前
VMware安装Ubuntu的记录
linux·ubuntu
小信丶2 小时前
Spring Cloud Stream EnableBinding注解详解:定义、应用场景与示例代码
java·spring boot·后端·spring
w6100104662 小时前
CKA-2026-Service
linux·服务器·网络·service·cka
ffqws_2 小时前
Spring Boot入门:通过简单的注册功能串联Controller,Service,Mapper。(含有数据库建立,连接,及一些关键注解的讲解)
数据库·spring boot·后端