Linux系统-基本指令(6)
文章目录
- find 指令
- grep 指令
- zip 和 unzip 指令(4-0.00.00)
- Windows 和 Linux 进行压缩包文件互传
- tar 指令(打包压缩目录)
- Linux 和 Linux 如何互传文件
- bc 指令(了解即可)
- uname-r 指令(了解即可)
- 几个好用的热键
这几天学习指令下来,我觉得指令这东西其实也不算是很重要的东西,每天练下指令,自然而然的熟练了,我也并非是将一个指令的全部选项都列出来,反正我学了哪些指令+选项我就总结下来,没接触到的那就以后再说
Linux基本指令-文章索引 | 指令知识点 |
---|---|
Linux系统-基本指令(3) | pwd,whoami,cd,ls, |
Linux系统-基本指令(4) | mkdir,touch,rm,man,cp |
Linux系统-基本指令(5) | mv,cat,more和less,head和tail,cal |
Linux系统-基本指令(6) | find,grep,zip和unzip,tar,bc |
find 指令
1. find +指定路径 -name *.c(普通文件或目录名称)
,这里的*
是通配符,在该路径下查找以.c
作为后缀的目标文件,再将目标文件的路径给打印出来
//查找普通文件
[root@hcss-ecs-28ce ~]# find /root/lesson -name *.c
/root/lesson/code.c
/root/lesson/time.c// 查找目录
[root@hcss-ecs-28ce lesson]# ls
a.out dir1 dir2 temp.txt time.c
[root@hcss-ecs-28ce lesson]# tree dir1
dir1
├── code.c
├── file.txt
└── log.txt0 directories, 3 files
[root@hcss-ecs-28ce lesson]# pwd
/root/lesson[root@hcss-ecs-28ce lesson]# find /root -name dir1
/root/lesson/dir1
2. find
查找就类似于在Linux下做深度优先遍历,从你当前指定的目录往下找,匹配结点是否是目标文件,是就把目标文件的路径打出来,不是就接着匹配下一个结点(所以它查找是比较慢的,它是一个一个去找)
3. find ~ -name '*.txt'
,带上选项~
是让find
从当前路径下开始查找,*.txt
可以带单引号,也可以不带
[root@hcss-ecs-28ce lesson]# pwd
/root/lesson
[root@hcss-ecs-28ce lesson]# ls
a.out dir1 dir2 temp.txt time.c[root@hcss-ecs-28ce lesson]# find ~ -name '*.txt'
/root/lesson/dir1/file.txt
/root/lesson/dir1/log.txt
/root/lesson/temp.txt
4. find
与whicn
的区别
find
可以在系统目录树直接查找,可以查找任意文件which
只用来查找系统自带的可执行程序which ls
就相当于命令find /usr/bin/ -name ls
[root@hcss-ecs-28ce lesson]# find /usr/bin/ -name ls
/usr/bin/ls
[root@hcss-ecs-28ce lesson]# which ls
alias ls='ls --color=auto'/usr/bin/ls
5. 如果man实在查不出对应的指令,就可以试试whereis 指令
(不用指定路径,可以在系统级别的目录下查找),看这个指令是否有匹配的对应手册压缩包。比如下面的ls.l.gz
文件,其实这就说明指令ls
手册被预装到Linux
系统中,它就是一个文件,所以man
能显示出1号手册中的ls
指令的内容
[root@hcss-ecs-28ce lesson]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz
grep 指令
1. 指令grep
它就是一个行文本过滤工具。grep +'关键信息' +普通文件
,将该普通文件中包行关键信息的文本行全部打印出来,单引号和双引号用在这种:"hello world"
[root@hcss-ecs-28ce dir1]# grep '999' log.txt
hello bit,hello world999
hello bit,hello world1999
hello bit,hello world2999
...
2. grep
指令有什么用呢?假如你今天负责检查一个很大的项目,有20万行代码,那你首先就得先找到mian函数在哪些地方,才能入手函数,main函数都找不到,那咋看这个20万行代码呢?
- 带上选项
-n
可以显示包含main
该文本所在的行数
[root@hcss-ecs-28ce dir1]# grep "main" *.c
code.c:int main()
file.c:int main()[root@hcss-ecs-28ce dir1]# grep -n "main" *.c
code.c:2:int main()
file.c:2:int main()
- 带上选项
-v
可以把不匹配的行打印出来,匹配的行不要打印出来
[root@hcss-ecs-28ce dir1]# cat code.c
#include<stdio.h>
int main()
{printf("hello world/n");return 0;
}[root@hcss-ecs-28ce dir1]# grep -v "main" code.c
#include<stdio.h>
{printf("hello world/n");return 0;
}
grep
是对大小敏感的,带上选项-i
可以忽视大小写
[root@hcss-ecs-28ce dir1]# cat file.txt
int Main()
int MAIN()
int main()[root@hcss-ecs-28ce dir1]# grep "main" file.txt
int main()[root@hcss-ecs-28ce dir1]# grep -i "main" file.txt
int Main()
int MAIN()
int main()
- 上面三个选项是可以组合使用的,
grep -niv "main" code.c
:显示行号,反向匹配,忽略大小写。其实在后面还会介绍grep
指令去查找进程
zip 和 unzip 指令(4-0.00.00)
1. 两个常识。打包压缩是什么?为什么要打包压缩?
是什么
:打包压缩是使用特定的算法,将文件进行合并或者压缩,减少体积为什么
:首先通过压缩文件,文件体积减少,节省磁盘空间的资源,减少网络传输的成本。其次多个文件转一个文件,发送文件并不存在缺失文件的情况,可以提高文件传输的容错性- 打包和压缩是两个动作,比如今天你要将衣服收进书包里,这个动作是打包。而你要想让这个书包放更多的东西,就将衣服往里压一压,这个动作就是压缩。
但目前咱将打包和压缩作为一个整体(不去分开讨论)
2. 如果发现zip
和unzip
指令用不了,直接用命令yum install -y zip unzip
进行安装即可
3. 我想要将test
这个目录里面的内容全部打包压缩
[root@hcss-ecs-28ce dir1]# ls
code.c file.c file.txt log.txt test
[root@hcss-ecs-28ce dir1]# tree test
test
├── dir1
│ └── dir2
│ └── dir3
│ └── log.txt
├── f1.txt
├── f2.txt
├── f3.txt
├── f4.txt
└── f5.txt3 directories, 6 files
[root@hcss-ecs-28ce dir1]# cat test/dir1/dir2/dir3/log.txt
hello world
- 这里直接用命令
zip test.zip test
是不会成功的,因为你打包压缩的是一个目录,而不是普通文件。解压用命令:unzip test.zip
[root@hcss-ecs-28ce dir1]# ls
test
[root@hcss-ecs-28ce dir1]# zip test.zip testadding: test/ (stored 0%)
[root@hcss-ecs-28ce dir1]# ls
test test.zip[root@hcss-ecs-28ce dir1]# mkdir other
[root@hcss-ecs-28ce dir1]# ls
other test test.zip[root@hcss-ecs-28ce dir1]# mv test.zip other
[root@hcss-ecs-28ce dir1]# cd other
[root@hcss-ecs-28ce other]# ls
test.zip[root@hcss-ecs-28ce other]# unzip test.zip
Archive: test.zipcreating: test/
[root@hcss-ecs-28ce other]# ls
test test.zip
[root@hcss-ecs-28ce other]# tree test
test0 directories, 0 files
- 这里将该路径下的所有文件都删除,可以用到通配符
*
[root@hcss-ecs-28ce other]# ls
test test.zip
[root@hcss-ecs-28ce other]# rm -rf *
[root@hcss-ecs-28ce other]# ls
- 想成功打包一个目录里的全部内容,得带上选项
-r
,递归处理,将指定目录下的所有文件和子目录一并处理,zip -r(目录)dst.zip src
[root@hcss-ecs-28ce dir1]# ls
other test
[root@hcss-ecs-28ce dir1]# zip -r test.zip testadding: test/ (stored 0%)adding: test/dir1/ (stored 0%)
...
[root@hcss-ecs-28ce dir1]# ls
other test test.zip
[root@hcss-ecs-28ce dir1]# mv test.zip other
[root@hcss-ecs-28ce dir1]# cd other
[root@hcss-ecs-28ce other]# ls
test.zip[root@hcss-ecs-28ce other]# unzip test.zip
Archive: test.zipcreating: test/creating: test/dir1/
...[root@hcss-ecs-28ce other]# ls
test test.zip
[root@hcss-ecs-28ce other]# tree test
test
├── dir1
│ └── dir2
│ └── dir3
│ └── log.txt
├── f1.txt
├── f2.txt
├── f3.txt
├── f4.txt
└── f5.txt3 directories, 6 files
[root@hcss-ecs-28ce other]# cat test/dir1/dir2/dir3/log.txt
hello world
- 命令
unzip XX.zip
是解压缩到当前路径,而unzip XX.zip -d 指定路径
,可以解压缩到指定路径中
[root@hcss-ecs-28ce dir1]# ls
other test
[root@hcss-ecs-28ce dir1]# ls other
[root@hcss-ecs-28ce dir1]# zip -r test.zip testadding: test/ (stored 0%)
...
[root@hcss-ecs-28ce dir1]# ls
other test test.zip[root@hcss-ecs-28ce dir1]# unzip test.zip -d other/
Archive: test.zipcreating: other/test/
...
[root@hcss-ecs-28ce dir1]# ls
other test test.zip
[root@hcss-ecs-28ce dir1]# ls other
test[root@hcss-ecs-28ce dir1]# tree other/test
other/test
├── dir1
│ └── dir2
│ └── dir3
│ └── log.txt
├── f1.txt
├── f2.txt
├── f3.txt
├── f4.txt
└── f5.txt3 directories, 6 files
Windows 和 Linux 进行压缩包文件互传
1. 用不了指令rz
和sz
,直接进行指令的安装:yum install -y lrzsz
2. 将Linux
中的文件传入到Windows
中需要用到命令sz + 压缩包文件
[root@hcss-ecs-28ce dir1]# ls
other test test.zip
[root@hcss-ecs-28ce dir1]# sz test.zip
3. 将Windows
中的压缩包文件传入到Linux
中要用到指令rz
。如果你装上了xshell
的话是可以直接将压缩包文件拖拽到xshell
界面,目前处于哪个路径下,该压缩包就在哪个路径下
[root@hcss-ecs-28ce dir1]# ls
other test
[root@hcss-ecs-28ce dir1]# rz[root@hcss-ecs-28ce dir1]# ls
other test test.zip
tar 指令(打包压缩目录)
1. 命令tar -czf test.tgz test
将目录test
里面的全部内容打包压缩成文件test.tgz
。c
:创建,z
:压缩,f
:后面紧跟着的打包压缩名是什么。文件test.tgz
其实是test.tar.gz
(将打包和压缩看成了一个整体,简写成此格式)
[root@hcss-ecs-28ce dir1]# ls
other test
[root@hcss-ecs-28ce dir1]# tar -czf test.tgz test
[root@hcss-ecs-28ce dir1]# ls
other test test.tgz
2. 解压缩包将上面选项c
替换成x
即可。tar -xzf XXX.tgz -C 指定路径
,不带选项-C
就解压缩到当前路径
// 解压缩当当前路径
[root@hcss-ecs-28ce dir1]# ls
other test test.tgz
[root@hcss-ecs-28ce dir1]# mv test.tgz other
[root@hcss-ecs-28ce dir1]# ls
other test
[root@hcss-ecs-28ce dir1]# ls other
test.tgz
[root@hcss-ecs-28ce dir1]# cd other
[root@hcss-ecs-28ce other]# tar -xzf test.tgz
[root@hcss-ecs-28ce other]# ls
test test.tgz// 解压缩到指定路径
[root@hcss-ecs-28ce dir1]# ls
other
[root@hcss-ecs-28ce dir1]# cd other
[root@hcss-ecs-28ce other]# pwd
/root/lesson/dir1/other
[root@hcss-ecs-28ce other]# tar -xzf test.tgz -C ..
[root@hcss-ecs-28ce other]# ls ..
other test
3. 带上选项-v
可以将打包压缩或解压缩包的过程给显示出来
[root@hcss-ecs-28ce other]# ls ..
other
[root@hcss-ecs-28ce other]# tar -xvzf test.tgz -C ..
test/
test/dir1/
...
[root@hcss-ecs-28ce other]# ls ..
other test
Linux 和 Linux 如何互传文件
bc 指令(了解即可)
该指令可以看成是一个命令行计算器,它其实可以与管道配合(
echo 1 + 2 + 3 + 4 + 5 | bc
),按quit
或ctrl + c
退出
[root@hcss-ecs-28ce other]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1 + 1
2
3*987
2961
quit[root@hcss-ecs-28ce other]# echo 1 + 2 + 3 + 4 + 5
1 + 2 + 3 + 4 + 5
[root@hcss-ecs-28ce other]# echo 1 + 2 + 3 + 4 + 5 | bc
15
uname-r 指令(了解即可)
uname
用来获取电脑和操作系统的相关信息。uname
可显示linux
主机所用的操作系统的版本、硬件的名称等基本信息。带上选项a
可以详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类型,硬件平台类型,操作系统名称
[root@hcss-ecs-28ce other]# uname -r
3.10.0-1160.119.1.el7.x86_64
[root@hcss-ecs-28ce other]# uname -a
Linux hcss-ecs-28ce 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
几个好用的热键
1. Tap
键可以做路径或指令的补全,Tap
一下不行,就Tap
两下
[root@hcss-ecs-28ce other]# whi(按下Tap键,可能要按两次)
which while whiptail [root@hcss-ecs-28ce other]# cat test/
dir1/ f1.txt f2.txt f3.txt f4.txt f5.txt
[root@hcss-ecs-28ce other]# cat test/dir1/dir2/dir3/log.txt
2. 上下方向键可以翻译历史命令
3. ctrl + c
可以停止异常操作
4. ctrl + r
可以搜索历史命令(按下ctrl + r
,再输入历史命令中的一部分)
// 这里我只按下ctrl + r,输入while就能查到此命令
[root@hcss-ecs-28ce other]# cnt=1;while [ $cnt -le 10000 ];do echo "hello bit,hello world$cnt";let cnt++; done > log.txt
[root@hcss-ecs-28ce other]# ls
log.txt test test.tgz
5. history
指令可以查看历史命令
[root@hcss-ecs-28ce other]# history1 2025-04-09 01:37:43 root vim /etc/sudoers +502 2025-04-09 01:03:00 root ls3 2025-04-09 01:03:03 root pwd...