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

【Linux】初见,基础指令(续)

前言:

        上文讲解了部分指令,本文我们来讲解剩下的指令【Linux】初见,基础指令-CSDN博客

cat指令

语法:cat  选项  文件

功能:打印文件中的内容

选项:

-b  对非空行输出进行编号

-n  对输出的说有行进行编号

-s  不输出多行空行

root@hcss-ecs-4ce7:~# cat temp.txt
hello
hello
hellohellohello
hello#对所有行进行编号
root@hcss-ecs-4ce7:~# cat -n temp.txt1	hello2	hello3	hello4	5	hello6	7	8	9	hello10	hello#仅对非空行进行编号
root@hcss-ecs-4ce7:~# cat -b temp.txt1	hello2	hello3	hello4	hello5	hello6	hello#对多个非空行进行压缩,仅打印一行非空行
root@hcss-ecs-4ce7:~# cat -s temp.txt
hello
hello
hellohellohello
hello

 补充:

cat为顺序打印,而tac为逆序打印

root@hcss-ecs-4ce7:~# cat temp.txt
1
2
3
4
5
6
7#逆序打印
root@hcss-ecs-4ce7:~# tac temp.txt7
6
5
4
3
2
1

more指令

语法:more  选项

功能:类似于cat,都是打印文件内容

选项:

-n  指定行数

q  退出more 

区别:cat是一口气把所有东西全部打印出来,而more我们可以指定一次性打印多少行。当文件内容过多时,more有利于我们更好的读

#指定一次性只打印3行,此时我们按住enter,会自动继续打印
root@hcss-ecs-4ce7:~# more -3  temp.txt
1
2
3
--More--(40%)#点击q退出more
root@hcss-ecs-4ce7:~# more -3  temp.txt
1
2
3
4
root@hcss-ecs-4ce7:~# 

less指令

语法:less  参数  文件

 功能:less与more相识,但是less的功能更加强大。less一次性会打印一整个屏幕,可以用上下键进行查看内容

选项:

-i  搜索时忽略大小写

-N 显示每行的行号

/  从光标位置开始向下搜索

?从光标位置开始向上搜索

n  根据之前要搜索的内容,继续搜索(搜索方向和之前一致)

N  根据之前要搜索的内容,继续搜索(搜索方向和之前相反)

q  退出less界面

 -i 和 -N是less指令的选项,后面的按键是进入less界面后使用的

less -N temp.txt

输出指令后进入less界面

head指令 

语法:head  参数  文件

功能:打印文件是头部内容,默认打印前10行内容

选项:

-行数   打印指定行数内容 

#默认打印10行
root@hcss-ecs-4ce7:~# head temp.txt
1
2
3
4
5
6
7
8
9
10#指定打印前3行
root@hcss-ecs-4ce7:~# head -3 temp.txt
1
2
3

补充:关于Linux中有那些文件、目录分类 。首先我们知道Linux中是依靠文件详细信息里的第一个字母来分类的

d  目录

-   普通文件

c  字符文件

b  块设备文件

l  链接文件

p  管道文件

tail指令

语法: tail  参数  文件

功能: 显示文件尾部的内容

选项:

-f  循环读数

-行数  指定显示行数(默认显示10行)

#默认显示10行
root@hcss-ecs-4ce7:~# tail temp.txt
3
4
5
6
7
8
9
10
11
12#指定显示3行
root@hcss-ecs-4ce7:~# tail -3 temp.txt
10
11
12

 拓展:如果我们只想看中间的内容呢?比如 3~9行,怎么办?

root@hcss-ecs-4ce7:~# head -9 temp.txt | tail -7
3
4
5
6
7
8
9

date指令

语法:date  + 格式

功能:显示时间

使用者可以自己设定显示的格式

%H  小时

%M  分钟

%S  秒

%X 相当于%H:%M:%S

%m  月

%Y  年

%F  相当于%Y-%m-%d

时间戳

所谓时间戳,就是所有计算机内部统一的时间。是格林威治从1970年1月1 日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。

其目的是让全世界的计算机同一时间,避免时差的影响。

时间-> 时间戳:date  +%s

时间戳->时间:date  -d@时间戳

#常规时间
root@hcss-ecs-4ce7:~# date
Tue May 20 08:08:37 PM CST 2025#用户自己设定显示样式
root@hcss-ecs-4ce7:~# date +%Y
2025root@hcss-ecs-4ce7:~# date +%X
08:08:54 PMroot@hcss-ecs-4ce7:~# date +%F
2025-05-20#得到时间戳
root@hcss-ecs-4ce7:~# date +%s
1747742967#时间戳得到时间
root@hcss-ecs-4ce7:~# date -d@1747742967
Tue May 20 08:09:27 PM CST 2025

cal指令 

 语法:cal

功能:查看日历 

root@hcss-ecs-4ce7:~# calMay 2025        
Su Mo Tu We Th Fr Sa  1  2  3  4  5  6  7  8  9 10  
11 12 13 14 15 16 17  
18 19 20 21 22 23 24  
25 26 27 28 29 30 31  

find指令

语法:find  路径  选项

功能:查找命令

选项:

-name  按照名字查找命令

#指定路径,按照名字查找
root@hcss-ecs-4ce7:~# ls 
hyc  new  new.txt  temp.txt  you.txt
root@hcss-ecs-4ce7:~# tree new
new
├── path
├── path1
│   └── path2
└── tmp4 directories, 0 files
root@hcss-ecs-4ce7:~# find ./new -name tmp
./new/tmp

whereis指令

语法:whereis  指令名

功能:用于查找指令的位置

root@hcss-ecs-4ce7:~# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gzroot@hcss-ecs-4ce7:~# whereis printf
printf: /usr/bin/printf /usr/share/man/man3/printf.3.gz /usr/share/man/man1/printf.1.gzroot@hcss-ecs-4ce7:~# whereis touch
touch: /usr/bin/touch /usr/share/man/man1/touch.1.gz

alias指令

功能:给指令取别名

root@hcss-ecs-4ce7:~# alias abc= 'ls'
alias ls='ls --color=auto'root@hcss-ecs-4ce7:~# abc
hyc  new  new.txt  temp.txt  you.txt

grep指令

语法:grep  选项  搜寻字符串  文件

功能:在文件中搜寻字符串,并打印出来。

选项:

-i  忽略大小写

-n  输出行号

-v  根据搜索条件,反向过滤

root@hcss-ecs-4ce7:~# cat temp.txt
a
A
B
b
ab
Ab
aB
AB#存在a的字符串
root@hcss-ecs-4ce7:~# grep 'a' temp.txt
a
ab
aB#忽略大小写
root@hcss-ecs-4ce7:~# grep -i 'a' temp.txt
a
A
ab
Ab
aB
AB#显示行号
root@hcss-ecs-4ce7:~# grep -ni 'a' temp.txt
1:a
2:A
5:ab
6:Ab
7:aB
8:AB#反向过滤,没有a的字符串
root@hcss-ecs-4ce7:~# grep -nv 'a' temp.txt
2:A
3:B
4:b
6:Ab
8:AB

zip&unzip指令

语法:zip  压缩文件.zip    目录或文件

功能:压缩目录或文件

选项:

-r  递归处理目录及其包含的内容

root@hcss-ecs-4ce7:~# ls
hyc  new  new.txt  temp.txt  you.txt
root@hcss-ecs-4ce7:~# tree new
new
├── path
├── path1
│   └── path2
└── tmp4 directories, 0 files#压缩
root@hcss-ecs-4ce7:~# zip -r new.zip newadding: new/ (stored 0%)adding: new/path1/ (stored 0%)adding: new/path1/path2/ (stored 0%)adding: new/tmp/ (stored 0%)adding: new/path/ (stored 0%)
root@hcss-ecs-4ce7:~# ls
hyc  new  new.txt  new.zip  temp.txt  you.txt

语法:unzip  压缩文件.zip  目录

 功能:将压缩文件解压

选项:

-d  指定解压后所放的位置

root@hcss-ecs-4ce7:~# tree new
new
├── path
├── path1
│   └── path2
└── tmp4 directories, 0 files#解压
root@hcss-ecs-4ce7:~# unzip new.zip -d hyc
Archive:  new.zipcreating: hyc/new/creating: hyc/new/path1/creating: hyc/new/path1/path2/creating: hyc/new/tmp/creating: hyc/new/path/root@hcss-ecs-4ce7:~# tree hyc
hyc
└── new├── path├── path1│   └── path2└── tmp5 directories, 0 files

rzsz指令

这是一个用于Linux和windows之间基于Xshell互传文件的工具。sz为发送,rz为接收。

演示sz

为了方便观察我们选择桌面,点击确定。我们就可以在桌面看见我们从Linux传输过来的压缩包了

解压并打开文件,发现确实是我们的内容。

演示rz

我们先将之前的压缩包改名方便观察

 执行rz,跳出窗口,我们选择NEW

执行成功,我们发现NEW成功的传送到了Linux

tar指令(重要)

语法:tar  选项  文件.tgz   目标文件

功能:打包与解包

选项:

-c:建立一个打包文件(create的意思)

-x:解包

-z:是否同时具有gzip的属性?亦即是否需要用gzip压缩?

-v:打包的过程中显示文件,这个常用

-f:使用档名,请留意,在f之后要立即接档名,不要再加参数

-C:解压到指定目录

(打包常用:-czf         解包常用:-xcf )

root@hcss-ecs-4ce7:~# ls
hyc  new  new.txt  temp.txt  you.txt
root@hcss-ecs-4ce7:~# tree new
new
├── path
├── path1
│   └── path2
└── tmp4 directories, 0 files#打包new
root@hcss-ecs-4ce7:~# tar czf new.tgz new 
root@hcss-ecs-4ce7:~# ls
hyc  new  new.tgz  new.txt  temp.txt  you.txt#指定路径解包
root@hcss-ecs-4ce7:~# tar xzf new.tgz -C hyc
root@hcss-ecs-4ce7:~# tree hyc
hyc
└── new├── path├── path1│   └── path2└── tmp5 directories, 0 files

补充

scp可以实现Linux中文件的互传

bc指令

功能:方便进行除法运算

 

uname指令

功能:用来获取电脑和操作系统的相关信息

选项:

-r  显示操作系统的内核版本

-a  显示所有可用的系统信息(如内核名称、节点名称、内核版本、操作系统类型、硬件平台、处理器类型等)

root@hcss-ecs-4ce7:~# uname
Linuxroot@hcss-ecs-4ce7:~# uname -r
5.15.0-113-genericroot@hcss-ecs-4ce7:~# uname -a
Linux hcss-ecs-4ce7 5.15.0-113-generic #123-Ubuntu SMP Mon Jun 10 08:16:17 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Linux中常用热键

ctrl+c:中止异常任务

tab:搜索或匹配对应指令(点击两下)

ctrl+r:搜索历史命令

方向上下键:查看之前使用过的命令

ctrl+d:退出当前账户

http://www.xdnf.cn/news/549379.html

相关文章:

  • Linux第十一讲:信号
  • 构建自动收集并总结互联网热门话题的网站
  • 进程间通信(IPC)常用方式对比
  • 当PLC遇上电焊机器人:EtherCAT转CANopen上演工业级“语言翻译官”
  • DP2 跳台阶【牛客网】
  • [面试精选] 0001. 两数之和
  • 人工智能的“歧视”:“她数据”在算法运行中隐形
  • C46-二维数组与指针的总结
  • VUE3 中的 ResizeObserver 警告彻底解决方案
  • C#:多线程Task使用
  • c++使用protocol buffers
  • JS实现古诗竖排从右至左
  • 谈谈jvm的调优思路
  • c++学习方向选择说明
  • [软件工程]第二章题目汇总
  • MySQL 8.0窗口函数详解
  • 48、c# 中 IList 接⼝与List的区别是什么?
  • Gin--Blog项目-flags文件解析
  • RK3576 Android 14.0 SDK开发指南(第一集)
  • 丝杆升降机在锂电行业的自动化应用有什么?
  • Unity-编辑器扩展
  • 2025年护网行动蓝队防御全解析:构建智能动态防御体系
  • Raft算法学习(1)博士论文大纲
  • Go学习教程(附电子书资料)
  • 桥梁凝冰在线监测装置:科技守护道路安全的新防线
  • Python入门手册:Python简介,什么是Python
  • C++之fmt库介绍和使用(2)
  • GPS模块_亿佰特E108-GN04D_u-center2调试
  • Linux:面试题
  • CAU数据库class3 关系型数据库基础