一起来学Linux命令(三)

449 阅读9分钟

前言

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

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

head

-n 展示前n行

-c 展示前n个字符

使用示例:

[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相反,是从尾部开始展示文本


-f 循环读取

-q 不显示处理信息

-v 显示详细的处理信息

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

-n<行数> 显示行数

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

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

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

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


# 循环读取 我们查看正在运行中的日志可以使用这个命令 可以看到实时的日志打印
[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

+n   从笫n行开始显示

-n    定义屏幕大小为n行

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

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

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

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

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

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

-u    把文件内容中的下画线去掉
[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 在查看之前不会加载整个文件, 这个大家可以自己试下


# 这里给大家讲下如何翻页 和 跳转

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

空格:向下翻页。

b:向上翻页。

g:跳到文件开头。

G:跳到文件结尾。

q:退出 less。

wc

-c 统计字节数。

-l 统计行数。

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

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

-L 打印最长行的长度。

使用示例:


[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⭐️)

博客(阅读体验较佳)