本篇博文主要包括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_时间戳](http://img.558idc.com/uploadfile/allimg/centos/831a97d524dfb4f38a0748737280d6033635ad.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_02](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d99516f840558.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_03](http://img.558idc.com/uploadfile/allimg/centos/899731829c8d9836ec2460ae6c6b9088f76d4e.gif)
head 命令 默认只查看前10行
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_04](http://img.558idc.com/uploadfile/allimg/centos/03183400_63132d98dec1d3870.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_05](http://img.558idc.com/uploadfile/allimg/centos/8966ecb23e2d7cd62b20842971429f74628aa1.gif)
tail命令 默认只查看后10行
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_06](http://img.558idc.com/uploadfile/allimg/centos/03183400_63132d98e8ff865953.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_07](http://img.558idc.com/uploadfile/allimg/centos/39ea24e41984b854887360782695ba4dd15a65.gif)
带上-n选上我们可以查看指定行数 加入我们要查看前20行
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_08](http://img.558idc.com/uploadfile/allimg/centos/03183400_63132d98f3b1670270.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_09](http://img.558idc.com/uploadfile/allimg/centos/45d4d8182b3a8bd31ea5596a8037c38b75c519.gif)
那加入我们要拿到[30000,30020] 时,应该怎么做呢?
head -30020 hello.txt | tail -21![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_10](http://img.558idc.com/uploadfile/allimg/centos/28413d1285445c7c80f610d2586079d844247e.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_11](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d990df9597367.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_12](http://img.558idc.com/uploadfile/allimg/centos/223895532aea13257d83897ad81cb5840e80a5.gif)
这里我们使用了管道,那么什么是管道呢?
在上述这行命令中,我们会发现有一个 | 符号,这个符号其实就是管道。管道的作用是传输数据资源,放入数据的在 | 的左侧,管道的右侧做二次加工。具体实现原理我们在后续展开。
管道存在的意义:
级联多个命令,来完成流水线式的工作
2. 时间相关的指令
date显示
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_13](http://img.558idc.com/uploadfile/allimg/centos/03183400_63132d98f317e1536.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_14](http://img.558idc.com/uploadfile/allimg/centos/49403fa5641370ac874893780981c153ef921b.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_linux_15](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d9902d857520.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_16](http://img.558idc.com/uploadfile/allimg/centos/54df3be5116d713aa9031014ea2c20f21eb00b.gif)
2.在设定时间方面
date +%s
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_17](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d99018bc87689.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_18](http://img.558idc.com/uploadfile/allimg/centos/17e4ea33296e2679bb9886569630c3657e87cc.gif)
其中这个 1662192265 这串数字是什么呢?
在计算机中,我们把这个时间叫做时间戳.
时间与时间戳之间的相互转换:
- 时间->时间戳:date +%s
- 时间戳->时间:date -d @ 这串数字
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_19](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d990322359093.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_20](http://img.558idc.com/uploadfile/allimg/centos/33299dc961fd09ed788668bf907adb20dabf2d.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_linux_21](http://img.558idc.com/uploadfile/allimg/centos/03183400_63132d98f094c40663.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_22](http://img.558idc.com/uploadfile/allimg/centos/0257266205411698a35180fb2b4220d41340f8.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_23](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d990b2fe18348.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_24](http://img.558idc.com/uploadfile/allimg/centos/45d4b5446da01ea38df6838b33dada84cc8fd3.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_25](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d991e74829500.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_26](http://img.558idc.com/uploadfile/allimg/centos/22ec7ee1213d3cf9a4909168d8484588f553b3.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_27](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d990ed7b15277.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_28](http://img.558idc.com/uploadfile/allimg/centos/c3b8c1f46ff07e20d90926b23724df72acf832.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_linux_29](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d9902b6d1920.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_30](http://img.558idc.com/uploadfile/allimg/centos/2151d25947a1af2037c63948b60d13d616a04a.gif)
5. grep指令
grep命令详解
行文本过滤工具
语法: grep [选项] 搜寻字符串 文件
功能: 在文件中搜索字符串,将找到的行打印出来
常用选项:
- -i :忽略大小写的不同,所以大小写视为相同
- -n :顺便输出行号
- -v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_31](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d9916b1b80914.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_32](http://img.558idc.com/uploadfile/allimg/centos/54a7b5374748767d01e503bb1e6fcea7a8e4c8.gif)
通过下面这个例子我们明确知道grep 是明确大小写的
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_33](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d990f9c887034.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_34](http://img.558idc.com/uploadfile/allimg/centos/a778fc2776073fc5dca59573fe743fe9787bae.gif)
若要忽略大小写,带上 -i 选项即可
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_35](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d990513f59556.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_36](http://img.558idc.com/uploadfile/allimg/centos/f4037c905eaad07b1336497954c90309d705fa.gif)
反向匹配 -v :凡是具有关键字的文本行去掉。
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_37](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d99c4d7d8469.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_38](http://img.558idc.com/uploadfile/allimg/centos/14fb18154546c5410d9566368dcd475a3074ca.gif)
-n 带上行号
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_39](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d990faf750226.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_40](http://img.558idc.com/uploadfile/allimg/centos/333006e2213006e005a68587fd9ce3788f04d6.gif)
6. zip/unzip指令
语法: zip 压缩文件.zip 目录或文件
功能: 将目录或文件压缩成zip格式
常用选项: -r 递归处理,将指定目录下的所有文件和子目录一并处理
-d 打包压缩至指定目录
举例:将file.txt打包 解包
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_41](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d9914d4772375.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_42](http://img.558idc.com/uploadfile/allimg/centos/b1be9e900c3d36c17042823aaa88ef6bb9cfe1.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_43](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d9917d3114344.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_44](http://img.558idc.com/uploadfile/allimg/centos/671d84c0171f0cc77e27050e3e6fbd047b8269.gif)
解压到指定目录: -d 指定目录
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_45](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d991ec9849365.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_46](http://img.558idc.com/uploadfile/allimg/centos/d49bf10781930caca2f1818ab4634ecfef495a.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_linux_47](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d991b3f447453.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_48](http://img.558idc.com/uploadfile/allimg/centos/d9132fd1433bc7c83c6920d80116147dff640e.gif)
-C (大C): 解压到指定目录
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_49](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d9912d4344594.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_50](http://img.558idc.com/uploadfile/allimg/centos/64b644f2221d5720ae9823c29d4fc4a14ac850.gif)
8. bc指令
bc命令是Linux的计算器可以很方便的进行浮点运算
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_51](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d990a58a80137.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_52](http://img.558idc.com/uploadfile/allimg/centos/52c6e66170261e21c6f181c2ce462141c22d79.gif)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_53](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d991858531590.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_基本指令_54](http://img.558idc.com/uploadfile/allimg/centos/b4972b4829c8c15dd60738497a56671eb4de7c.gif)
9.unname -r
语法:uname [选项]
功能: uname用来获取电脑和操作系统的相关信息。
补充说明:uname可显示linux主机所用的操作系统的版本、硬件的名称等基本信息。
常用选项:
-a或–all 详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类型,硬件平台类型,操作系统名称
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_linux_55](http://img.558idc.com/uploadfile/allimg/centos/03183401_63132d992089174454.png)
![[ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname_时间戳_56](http://img.558idc.com/uploadfile/allimg/centos/81ec8380914ad65bdd7974dc6919bdcd3c21dd.gif)
(本篇完)
