本篇博文主要包括10个常用的Linux基本指令,其他常用指令大家可访问此篇博文[ Linux 长征路第一篇] 基本指令(1)
文章内容包括10个常用的Linux基本指令:head、tail、 date显示、cal、find、grep、zip/unzip、tar、bc、uname。
1. head/tail指令
head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾
head:
功能:head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行。
选项: -n <行数> 显示的行数
指令演示:
为了方便演示,我们伪造一个10w行的长文本
cnt=1; while [ $cnt -le 100000 ];do echo "hello world $cnt"; let cnt++; done > hello.txt
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_02](https://file.cfanz.cn/uploads/png/2022/09/03/10/BKI6VMc08I.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_03](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
head 命令 默认只查看前10行
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_04](https://file.cfanz.cn/uploads/png/2022/09/03/10/D02DbMS823.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_05](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
tail命令 默认只查看后10行
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_06](https://file.cfanz.cn/uploads/png/2022/09/03/10/8ffQU2JNZW.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_07](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
带上-n选上我们可以查看指定行数 加入我们要查看前20行
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_08](https://file.cfanz.cn/uploads/png/2022/09/03/10/L1VEN42669.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_09](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
那加入我们要拿到[30000,30020] 时,应该怎么做呢?
head -30020 hello.txt | tail -21
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_10](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_11](https://file.cfanz.cn/uploads/png/2022/09/03/10/8Q7SWGTMSM.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_12](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
这里我们使用了管道,那么什么是管道呢?
在上述这行命令中,我们会发现有一个 | 符号,这个符号其实就是管道。管道的作用是传输数据资源,放入数据的在 | 的左侧,管道的右侧做二次加工。具体实现原理我们在后续展开。
管道存在的意义:
级联多个命令,来完成流水线式的工作
2. 时间相关的指令
date显示
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_13](https://file.cfanz.cn/uploads/png/2022/09/03/10/fHU120DKG6.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_14](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
默认的date指令显示的特别不适合我们来阅读,我们可以加上一些选项进行查看
date 指定格式显示时间: date +%Y-%m-%d_%H:%M:%S
- %H : 小时(00..23)
- %M : 分钟(00..59)
- %S : 秒(00..61)
- %X : 相当于 %H:%M:%S
- %d : 日 (01..31)
- %m : 月份 (01..12)
- %Y : 完整年份 (0000..9999)
- %F : 相当于 %Y-%m-%d
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_15](https://file.cfanz.cn/uploads/png/2022/09/03/10/92O52M0J34.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_16](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
2.在设定时间方面
date +%s
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_17](https://file.cfanz.cn/uploads/png/2022/09/03/10/Y62cW4774T.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_18](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
其中这个 1662192265 这串数字是什么呢?
在计算机中,我们把这个时间叫做时间戳.
时间与时间戳之间的相互转换:
- 时间->时间戳:date +%s
- 时间戳->时间:date -d @ 这串数字
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_19](https://file.cfanz.cn/uploads/png/2022/09/03/10/K202ZL6N0c.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_20](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
Unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix timestamp)是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。
3.cal指令
cal指令可以用来显示日历
命令格式:cal [参数][月份][月份]
功能:用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份
常用选项:
- -3 显示系统前一个月,当前月,下一个月的月历
- -j 显示在当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数)
- -y 显示当前年份的日历
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_21](https://file.cfanz.cn/uploads/png/2022/09/03/10/N7e3V3EB0Z.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_22](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_23](https://file.cfanz.cn/uploads/png/2022/09/03/10/Pe2592A31X.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_24](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_25](https://file.cfanz.cn/uploads/png/2022/09/03/10/7KKH2R5JDF.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_26](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_27](https://file.cfanz.cn/uploads/png/2022/09/03/10/0CdC6Gd1S9.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_28](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
4. find指令
- Linux下fifind命令在目录结构中搜索文件,并执行指定的操作。
- Linux下fifind命令提供了相当多的查找条件,功能很强大。由于fifind具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。
- 即使系统中含有网络文件系统( NFS),fifind命令在该文件系统中同样有效,只你具有相应的权限。
- 在运行一个非常消耗资源的fifind命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)
语法: find pathname -options
功能:用于在文件树中查找文件,并做出相应的处理(可能访问磁盘)
常用选项:-name 按照文件名查找文件
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_29](https://file.cfanz.cn/uploads/png/2022/09/03/10/6Y27772b2b.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_30](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
5. grep指令
grep命令详解
行文本过滤工具
语法: grep [选项] 搜寻字符串 文件
功能: 在文件中搜索字符串,将找到的行打印出来
常用选项:
- -i :忽略大小写的不同,所以大小写视为相同
- -n :顺便输出行号
- -v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_31](https://file.cfanz.cn/uploads/png/2022/09/03/10/16R62SW1QY.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_32](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
通过下面这个例子我们明确知道grep 是明确大小写的
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_33](https://file.cfanz.cn/uploads/png/2022/09/03/10/1218202f1I.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_34](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
若要忽略大小写,带上 -i 选项即可
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_35](https://file.cfanz.cn/uploads/png/2022/09/03/10/HC12N62DI3.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_36](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
反向匹配 -v :凡是具有关键字的文本行去掉。
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_37](https://file.cfanz.cn/uploads/png/2022/09/03/10/6e9SSe60ca.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_38](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
-n 带上行号
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_39](https://file.cfanz.cn/uploads/png/2022/09/03/10/AI6EbGB2A0.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_40](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
6. zip/unzip指令
语法: zip 压缩文件.zip 目录或文件
功能: 将目录或文件压缩成zip格式
常用选项: -r 递归处理,将指定目录下的所有文件和子目录一并处理
-d 打包压缩至指定目录
举例:将file.txt打包 解包
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_41](https://file.cfanz.cn/uploads/png/2022/09/03/10/2dLD32J9aU.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_42](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_43](https://file.cfanz.cn/uploads/png/2022/09/03/10/5cU2E3DVZI.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_44](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
解压到指定目录: -d 指定目录
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_45](https://file.cfanz.cn/uploads/png/2022/09/03/10/bDQ1eA7OS3.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_46](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
7. tar指令
tar [-cxtzjvf] 文件与目录 ....
参数:
- -c :建立一个压缩文件的参数指令(create 的意思);
- -x :解开一个压缩文件的参数指令!
- -t :查看 tarfifile 里面的文件!
- -z :是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩?
- -j :是否同时具有 bzip2 的属性?亦即是否需要用 bzip2 压缩?
- -v :压缩的过程中显示文件!这个常用,但不建议用在背景执行过程!
- -f :使用档名,请留意,在 f 之后要立即接档名喔!不要再加参数!
- -C (大C): 解压到指定目录
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_47](https://file.cfanz.cn/uploads/png/2022/09/03/10/7e6D621095.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_48](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
-C (大C): 解压到指定目录
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_49](https://file.cfanz.cn/uploads/png/2022/09/03/10/07fTM4MeH6.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_50](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
8. bc指令
bc命令是Linux的计算器可以很方便的进行浮点运算
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_51](https://file.cfanz.cn/uploads/png/2022/09/03/10/DA5L7561eD.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_52](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_53](https://file.cfanz.cn/uploads/png/2022/09/03/10/dG8SDT8BEe.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_54](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
9.unname -r
语法:uname [选项]
功能: uname用来获取电脑和操作系统的相关信息。
补充说明:uname可显示linux主机所用的操作系统的版本、硬件的名称等基本信息。
常用选项:
-a或–all 详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类型,硬件平台类型,操作系统名称
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_55](https://file.cfanz.cn/uploads/png/2022/09/03/10/BVPe2681a8.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_56](https://file.cfanz.cn/uploads/gif/2021/09/18/17/58Y87YaF16.gif)
(本篇完)
