前言:
上文讲解了部分指令,本文我们来讲解剩下的指令【Linux】初见,基础指令-CSDN博客
cat指令
语法:cat 选项 文件
功能:打印文件中的内容
选项:
-b 对非空行输出进行编号
-n 对输出的说有行进行编号
-s 不输出多行空行
root@hcss-ecs-4ce7:~# cat temp.txt
hello
hello
hello
hello
hello
hello
#对所有行进行编号
root@hcss-ecs-4ce7:~# cat -n temp.txt
1 hello
2 hello
3 hello
4
5 hello
6
7
8
9 hello
10 hello
#仅对非空行进行编号
root@hcss-ecs-4ce7:~# cat -b temp.txt
1 hello
2 hello
3 hello
4 hello
5 hello
6 hello
#对多个非空行进行压缩,仅打印一行非空行
root@hcss-ecs-4ce7:~# cat -s temp.txt
hello
hello
hello
hello
hello
hello
补充:
cat为顺序打印,而tac为逆序打印
root@hcss-ecs-4ce7:~# cat temp.txt
1
2
3
4
5
6
7
#逆序打印
root@hcss-ecs-4ce7:~# tac temp.txt
7
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
2025
root@hcss-ecs-4ce7:~# date +%X
08:08:54 PM
root@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:~# cal
May 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
└── tmp
4 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.gz
root@hcss-ecs-4ce7:~# whereis printf
printf: /usr/bin/printf /usr/share/man/man3/printf.3.gz /usr/share/man/man1/printf.1.gz
root@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
└── tmp
4 directories, 0 files
#压缩
root@hcss-ecs-4ce7:~# zip -r new.zip new
adding: 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
└── tmp
4 directories, 0 files
#解压
root@hcss-ecs-4ce7:~# unzip new.zip -d hyc
Archive: new.zip
creating: 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
└── tmp
5 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
└── tmp
4 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
└── tmp
5 directories, 0 files
补充:
scp可以实现Linux中文件的互传
bc指令
功能:方便进行除法运算
uname指令
功能:用来获取电脑和操作系统的相关信息
选项:
-r 显示操作系统的内核版本
-a 显示所有可用的系统信息(如内核名称、节点名称、内核版本、操作系统类型、硬件平台、处理器类型等)
root@hcss-ecs-4ce7:~# uname
Linux
root@hcss-ecs-4ce7:~# uname -r
5.15.0-113-generic
root@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:退出当前账户