当前位置: 首页 > news >正文

Linux系统-基本指令(5)

文章目录

  • mv 指令
  • cat 指令(查看小文件)
  • 知识点(简单阐述日志)
  • more 和 less 指令(查看大文件)
  • head 和 tail 指令(跟查看文件有关)
  • 知识点(管道)
  • 时间相关的指令(3-2.36.00)
  • 知识点(跟时间相关)
  • cal 指令(了解即可)

mv 指令

1. 普通文件和目录剪切是不需要带选项的。mv + 普通文件/目录 + 指定路径

// 剪切普通文件
[root@hcss-ecs-28ce dir]# ls
file.txt[root@hcss-ecs-28ce dir]# mv file.txt ..
[root@hcss-ecs-28ce dir]# ls[root@hcss-ecs-28ce dir]# ls ..
dir  file.c  file.txt// 剪切目录
[root@hcss-ecs-28ce dir]# ls
dir1
[root@hcss-ecs-28ce dir]# mv dir1 ..
[root@hcss-ecs-28ce dir]# ls[root@hcss-ecs-28ce dir]# ls ..
dir  dir1  file.c  file.txt

2. 修改普通文件和目录的文件名称(修改名称的那个文件得存在

[root@hcss-ecs-28ce lesson]# ls
dir  dir1  file.c  file.txt[root@hcss-ecs-28ce lesson]# mv file.c code.c
[root@hcss-ecs-28ce lesson]# ls
code.c  dir  dir1  file.txt[root@hcss-ecs-28ce lesson]# mv dir dir2
[root@hcss-ecs-28ce lesson]# ls
code.c  dir1  dir2  file.txt

cat 指令(查看小文件)

1. cat正向打印文件中的内容,tac逆向打印文件中的内容

// cat 指令
[root@hcss-ecs-28ce lesson]# vim code.c
[root@hcss-ecs-28ce lesson]# cat code.c
#include<stdion.h>
int main()
{printf("hello world/n");return 0;
}// tac 指令
[root@hcss-ecs-28ce lesson]# tac code.c}return 0;printf("hello world/n");
{
int main()
#include<stdion.h>

2. cat它的作用就是用来查看一些小文件。因为当想查看文件的时候,如果直接用vimnano打开小文件,就很容易进行误操作

  • 选项 -n对输出的所以行进行编号
  • 选项 -b对非空输出行进行编号
  • 选项 -s不输出多行空行(一行或几行空行还是会输出的)
[root@hcss-ecs-28ce lesson]# cat -n code.c1	#include<stdion.h>2	int main()3	{4	   printf("hello world/n");5	   return 0;6	}7	[root@hcss-ecs-28ce lesson]# cat -b code.c1	#include<stdion.h>2	int main()3	{4	   printf("hello world/n");5	   return 0;6	}

知识点(简单阐述日志)

  • 如果要写一个上点规模的代码,日志是必须要有的
  • 什么是日志呢?在你的程序正常工作之前,你自己首先要打开一个日志文件,比如log.txt
  • 在你程序运行期间,代码里所有的printf要打印的内容全部写入到该日志文件中,不要往显示器中写入,这样日志文件就记录了该程序的执行痕迹
  • 这个文件就叫日志文件,这个行为就是写日志(它会有固定模式的)。通过这种方式,如果程序出问题了,能方便对问题进行排查

more 和 less 指令(查看大文件)

// 该命令是输出重定向,直接构建一万行内容写入到log.txt文件中
// 自行创建一个大文件log.txt用来举例子
[root@hcss-ecs-28ce lesson]# cnt=1;while [ $cnt -le 10000 ];do echo "hello bit,hello world$cnt";let cnt++; done > log.txt
[root@hcss-ecs-28ce lesson]# ll log.txt
-rw-r--r-- 1 root root 258894 Jun  3 18:00 log.txt

1. 之前用cat是来查看小文件,如果用cat去查看大文件的话就会出现刷屏的情况(ctrl + c强制停止)。可以用Linux系统自带的日志(大文件)举例/var/log/messages

[root@hcss-ecs-28ce lesson]# cat /var/log/messages
...
Jun  3 14:01:01 hcss-ecs-28ce systemd: Started Session 1385 of user root.
Jun  3 15:01:01 hcss-ecs-28ce systemd: Started Session 1386 of user root.
Jun  3 16:01:01 hcss-ecs-28ce systemd: Started Session 1387 of user root.
Jun  3 17:01:01 hcss-ecs-28ce systemd: Started Session 1388 of user root.
Jun  3 17:14:44 hcss-ecs-28ce systemd: Started Session 1389 of user root.
Jun  3 17:14:44 hcss-ecs-28ce systemd-logind: New session 1389 of user root.

2. 此时可以用到more指令,只不过它比较鸡肋,只能回车键往下翻(不能往上翻),more + 要查看的大文件(按q可以退出)

[root@hcss-ecs-28ce lesson]# more log.txt
...
hello bit,hello world28
hello bit,hello world29
hello bit,hello world30
hello bit,hello world31
hello bit,hello world32
hello bit,hello world33
...

3. 这里更推荐使用less指令

  • 它不仅可以上下翻(上下方向键
  • 还可以高亮的去查找特定的信息(/后面跟要搜索的信息
  • q退出查看。在搜索的过程中按n可以查看下一个匹配项
[root@hcss-ecs-28ce lesson]# less log.txt

在这里插入图片描述

head 和 tail 指令(跟查看文件有关)

1. head + 普通文件,默认查看该普通文件的前十行内容。tail + 普通文件,默认查看该普通文件的后上行内容

[root@hcss-ecs-28ce lesson]# head log.txt
hello bit,hello world1
hello bit,hello world2
hello bit,hello world3
hello bit,hello world4
hello bit,hello world5
hello bit,hello world6
hello bit,hello world7
hello bit,hello world8
hello bit,hello world9
hello bit,hello world10[root@hcss-ecs-28ce lesson]# tail log.txt
hello bit,hello world9991
...

2. head/tail -n 指定的文件,显示该文件指定行数n的内容。head从头开始,tail从尾部开始

[root@hcss-ecs-28ce lesson]# head -3 log.txt
hello bit,hello world1
hello bit,hello world2
hello bit,hello world3[root@hcss-ecs-28ce lesson]# tail -3 log.txt
hello bit,hello world9998
hello bit,hello world9999
hello bit,hello world10000

3. 那如果我想去查看大文件任意区间的文本内容呢?比如区间[2000, 2020],这里总共有两种方法

  • 第一种方法:head -2020 log.txt > temp.txt,先将该文件的前2020行写入到temp.txt中,再取temp.txt文件的后21行,tail -21 temp.txt
[root@hcss-ecs-28ce lesson]# head -2020 log.txt > temp.txt[root@hcss-ecs-28ce lesson]# ll temp.txt
-rw-r--r-- 1 root root 51413 Jun  3 18:23 temp.txt[root@hcss-ecs-28ce lesson]# tail -21 temp.txt
hello bit,hello world2000
hello bit,hello world2001
...

知识点(管道)

  • 第二种方法:这里只需要先知道管道是怎么用的即可。head -2020 log.txt | tail -21|就是管道,它就跟流水线加工一样,先提取到前2020行,再把这2020行通过管道喂给后面的指令,再提取末尾21行。这样经过二次加工,完成了对数据的两次操作
[root@hcss-ecs-28ce lesson]# head -2020 log.txt | tail -21
hello bit,hello world2000
hello bit,hello world2001
hello bit,hello world2002
...
  • 管道可以有多个(多个|),这样就可以使用多组命令来批量化,流水线化的对文本内容进行编辑和加工
// tac 逆向打印文本中的内容
[root@hcss-ecs-28ce lesson]# head -2020 log.txt | tail -21 | tac
hello bit,hello world2020
hello bit,hello world2019
hello bit,hello world2018// wc -l 输出多少行
[root@hcss-ecs-28ce lesson]# head -2020 log.txt | tail -21 | tac | wc -l
21

时间相关的指令(3-2.36.00)

1. date:后面啥也不跟,是老外看时间的方式。咱们看时间一般用到这个命令:date +%Y-%m-%d_%H:%M:%S+是选项,%是固定搭配,%Y与%m之间是分隔符(可自主选择),从做到右依次为:年,月,日,时,分,秒

[root@hcss-ecs-28ce lesson]# date
Tue Jun  3 18:43:55 CST 2025[root@hcss-ecs-28ce lesson]# date +%Y-%m-%d_%H:%M:%S
2025-06-03_18:44:38

2. 如果想输出2025-6-30 7:30:00,但它这里是不支持空格符作为分隔符的(%d %H,不支持),想带空格的话,就得用到转义字符\,使它成为字面上的空格

[root@hcss-ecs-28ce lesson]# date +%Y-%m-%d\ %H:%M:%S
2025-06-03 19:56:46
[root@hcss-ecs-28ce lesson]# date +%Y-%m-%d %H:%M:%S
date: extra operand ‘%H:%M:%S’
Try 'date --help' for more information

3. 这里date命令并不重要,但时间是很重要的。就比如之前说的日志,时间就是其中一个很重要的部分,我们不仅需要知道该程序出现什么问题,更要知道该问题出现在什么时候,才可以评估损失大小

4. 通过命令date +%s去获取时间戳。时间戳:1970-01-01 12:00(凌晨12点)到现在累计的秒数

[root@hcss-ecs-28ce lesson]# date +%s
1748952158
[root@hcss-ecs-28ce lesson]# date +%s
1748952161
[root@hcss-ecs-28ce lesson]# date +%s
1748952163
[root@hcss-ecs-28ce lesson]# date +%s
1748952168

知识点(跟时间相关)

1. 先讨论一下时差指的是什么,这里是按照我自己的理解粗略的阐述一下

  • 假如我想到国外的以一家购物网站上买一辆自行车,我是在中午12点买的,可能在美国记录的时间是晚上12点。
  • 因为全国有些地方就存在时间差的情况,可能在中国12点的时候,美国那边就晚上12点。
  • 结果过了一周了,我的自行车还没到,我就询问他们,怎么弄的啊!我上个星期中午12点的货咋还没到呢?
  • 然后这个客服就按照他们他们当地的时间去找中午12点的货却怎么也找不到(因为系统记录的是晚上12点的货),所以就需要规定一个时差,用自己这个地方的时间加上或减去这个时差就相当于另外一个地方的时间

2. 在计算机中只需要存储时间戳即可,命令date +%Y-%m输出的日期是通过对应的算法将此刻的时间戳转换而来的

3. 现在来讨论一下时间戳这个东西有什么用,就拿咱们之前的日志来说,时间戳对于日志是一个非常重要的组成部分

  • 举个例子,有一天我发现我的服务器在早上八点钟到晚上八点钟这段时间是非常的卡,我就想要去写一个程序去排查出问题
  • 我就先记录这一天的早上八点和晚上八点的时间戳,连着记录一个星期,再分别统计每天早上八点时间戳与晚上八点时间戳这个范围内服务器对应日志(日志中就包含了该时间戳下用户访问的IP信息),看一下它到底做了什么
  • 这才发现是这段时间的访问量从2万增长到了10万。所以时间戳在记录上它是一个线性增长的数值,在业务和应用上,它可以对内容做批量化的分段查找和定位

4. 之前咱是通过命令date +%s去打印时间戳,其实还可以写一个可执行程序,通过C语言函数time去打印时间戳

[root@hcss-ecs-28ce lesson]# ls
a.out  code.c  dir1  dir2  file.txt  log.txt  temp.txt  time.c[root@hcss-ecs-28ce lesson]# cat time.c
#include<stdio.h>
#include<time.h>int main()
{printf("%lu\n", time(NULL));return 0;
}[root@hcss-ecs-28ce lesson]# ./a.out
1748954029
[root@hcss-ecs-28ce lesson]# ./a.out
1748954032
[root@hcss-ecs-28ce lesson]# ./a.out
1748954036

5. 命令date +%Y-%m-%d_%H:%M:%S -d @1748954036,可将该时间戳转换成指定的日期格式

[root@hcss-ecs-28ce lesson]# date +%Y-%m-%d_%H:%M:%S -d @1748954036
2025-06-03_20:33:56
[root@hcss-ecs-28ce lesson]# date +%Y-%m-%d_%H:%M:%S -d @1748954
1970-01-21_13:49:14
[root@hcss-ecs-28ce lesson]# date +%Y-%m-%d_%H:%M:%S -d @17489
1970-01-01_12:51:29
[root@hcss-ecs-28ce lesson]# date +%Y-%m-%d_%H:%M:%S -d @0
1970-01-01_08:00:00
  • 当时间戳为0的时候,为啥会是早上八点呢,不是凌晨12点开始计算的吗?
  • 这是因为北京和他们开始统计时间戳的地方(也是就是格林威治)时差是八个小时,当时他们是12:00:00,北京是08:00:00,而咱们用的机器都在国内,所以服务器就自动的帮我们加上了时差,那你所获取的时间就是咱们本地的时间

cal 指令(了解即可)

  1. cal 打印当月的日历
    在这里插入图片描述
  2. cal 2025 打印2025年的日历
    在这里插入图片描述
  3. cal -3 打印前一个月,这个月,后一个月的日历。想了解的更多可以man cal
    在这里插入图片描述

相关文章:

  • STM32入门教程——按键控制LED光敏传感器控制蜂鸣器
  • 05 APP 自动化- Appium 单点触控 多点触控
  • 接口自动化测试之pytest 运行方式及前置后置封装
  • 不连网也能跑大模型?
  • YAML文件
  • NLP学习路线图(二十):FastText
  • Python Pytest
  • Read View在MVCC里如何工作
  • 第二章 2.TCP IP Protocol Suite(CCNA)
  • 使用cmd命令行创建数据库和表-简单步骤记录
  • 【Zephyr 系列 6】使用 Zephyr + BLE 打造蓝牙广播与连接系统(STEVAL-IDB011V1 实战)
  • 北京通用人工智能研究院-通才智能体 LEO
  • 【Pandas】pandas DataFrame rename_axis
  • 记录被mybatis一级缓存坑的问题
  • electron-vite_18桌面共享
  • Web3如何重塑数据隐私的未来
  • LeetCode[404]左叶子之和
  • 机器学习——主成分分析(PCA)
  • Axure设计案例:滑动拼图解锁
  • 项目计划未与实际情况同步更新,如何保持计划的实时性?
  • 网站动图是怎么做的/青岛官网seo
  • 人和动物做的网站/saas建站平台
  • 专业的徐州网站开发/百度广告代理商加盟
  • 做网站的得多少钱/微信朋友圈广告30元 1000次
  • 苏州网站建设服务公司/黄山网络推广公司
  • 罗定市城乡规划建设局网站/网站运营维护的基本工作