Linux学习笔记—shell常用指令(一)

1.diff命令

diff命令用于比较两个文件或目录的不同。比如,游戏、软件更新,只更新补丁文件。

diff [options] target1 target2
diff file1 file2
diff directory1 directory2 

diff命令常用参数

-a 所有的文件都视为文本文件来逐行比较,即使不是文本文件. 
-b 忽略空格引起的变化.
-B 忽略插入删除空行引起的变化.
-c 显示全部内文,并标出不同之处.
-i 不检查大小写的不同.
-p 若比较的文件为C语言的程序代码文件时,显示差异所在的函数名称.
-q 仅报告文件是否相异,不报告详细的差异. 
-r 当比较目录时,递归比较任何找到的子目录. 
-u 以合并的方式来显示文件内容的不同.

[num1,num2][a|c|d][num3,num4]
num1,num2表示在第一个文件中的行数
a表示添加——add
c表示更改——change
d表示删除——delete
< 表示第一个文件中的内容,>表示第二个文件中的内容,---分割线
num3,num4表示在第二个文件中的行数

示例一:

[root@localhost mnt]# cat westos
123
[root@localhost mnt]# cat westos1
123
456
[root@localhost mnt]# diff westos westos1  
1a2     ##第1个文件添加第2个文件的第2行的456后等于第2个文件
> 456

示例二:

[root@desktop5 mnt]# cat westos
1234
[root@desktop5 mnt]# cat westos1
123
[root@desktop5 mnt]# diff westos westos1  ##两个文件内容对比
1c1
< 1234
---
> 123

示例三:

[root@desktop5 mnt]# cat westos
123
456
[root@desktop5 mnt]# cat westos1
123
[root@desktop5 mnt]# diff westos westos1  ##
2d1
< 456

示例四:

[root@localhost mnt]# mkdir linux
[root@localhost mnt]# mkdir unix
[root@localhost mnt]# touch linux/westos

测试:
这里写图片描述

2.patch命令

patch 源文件 补丁文件
[root@desktop5 mnt]# diff -u westos westos1        ##补丁
[root@desktop5 mnt]# diff -u westos westos1 > file.path   ##补丁导入补丁文件
[root@desktop5 mnt]# yum install patch -y       ##安装软件
[root@desktop5 mnt]# patch -b westos file.path  ##-b:备份
patching file westos
[root@desktop5 mnt]# ls
file.path  westos  westos1  westos.orig    ##westos.orig为原备份文件,即原westos文件

测试:
这里写图片描述
这里写图片描述

3.cut命令

cut命令多用于字符截取

cut -d        ##指定分隔符
cut -f 1,7 | 1-7 ##指定截取的列
cut -c 1,4 | 1-4 ##指定截取的字符位置

示例:

[root@localhost mnt]# cp /etc/passwd .    ##可删除一部分内容

这里写图片描述
示例1:

[root@localhost mnt]# cut -d : -f 1 passwd    ##截取passwd文件以:间隔的第一列

这里写图片描述
示例2:

[root@localhost mnt]# cut -d : -f 1-3 passwd  ##截取passwd文件以:间隔的第一列到第三列

这里写图片描述
示例3:

[root@localhost mnt]# cut -d : -f 1,3 passwd  ##截取passwd文件以:间隔的第一列和第三列

这里写图片描述
示例4:

[root@localhost mnt]# cut -d : -f 3- passwd   ##截取passwd文件以:间隔的第三列及其以后

这里写图片描述
示例5:

[root@localhost mnt]# cut -c 1,3 passwd       ##截取passwd文件第一个和第三个字符

这里写图片描述

4.sort命令

sort -n 纯数字排序
sort -r 倒序
sort -u 去掉重复数字
sort -o 输出到指定文件中
sort -t 指定分隔符
sort -k 指定要排序的列

实验环境:
这里写图片描述

示例1:

sort westos                 ##默认按第一列数字进行排序

这里写图片描述
示例2:

sort -n westos              ##默认纯数字进行排序

这里写图片描述
示例3:

[root@localhost mnt]# sort -urn westos   ##纯数字倒序,并去掉重复的数字

这里写图片描述
示例4:

[root@localhost mnt]# sort -urn westos -o file   ##结果输出到file文件中

这里写图片描述


实验环境:
这里写图片描述
示例5:

[root@localhost mnt]# sort -t : -k 2 -n westos ##以:间隔的第二列纯数字排序

这里写图片描述

5.uniq命令

uniq -u   ##显示唯一的行
uniq -d   ##显示重复的行
uniq -c   ##每行显示一次并统计重复次数

实验环境:
这里写图片描述
示例1:

这里写图片描述

示例2:
这里写图片描述
示例3:
这里写图片描述

示例4:

sort -n westos1 | uniq -d    ##组合使用,数字排序后,显示重复的数字

这里写图片描述

示例5:

 sort -n westos1 | uniq -c    ##组合使用,数字排序后,统计重复的次数

这里写图片描述
示例6:

sort -n westos1 | uniq -u    ##组合使用,数字排序后,显示不重复的数字(即唯一的行)

这里写图片描述

6.&&、|| 命令

&& 用来执行条件成立后执行的命令
|| 用来执行条件不成立后执行的命令


实验一:ping命令成功 yes 不成功no
铺垫:

[root@localhost mnt]# ping -c1 -w1 172.25.254.5 &> /dev/null  ##结果放入垃圾箱  
[root@localhost mnt]# ping -c1 -w1 172.25.254.5 &> /dev/null && echo yes || echo no                          

实验:

[root@localhost mnt]# vim check_ip.sh
#!/bin/bash
ping -c1 -w1 $1 &>/dev/null && echo $1 is up || echo $1 is down

测试:
这里写图片描述
实验二:找出目录下最大的文件check_file.sh
铺垫:

[root@localhost mnt]# ls -Sl /mnt/ | grep -v total
[root@localhost mnt]# ls -Sl /mnt/ | grep -v total | awk -F " " 'NR==1{print $9}'

这里写图片描述
这里写图片描述
实验:

[root@localhost mnt]# vim check_file.sh
#!/bin/bash
ls -Sl $1 | grep -v total | awk -F " " 'NR==1{print $9}'

测试:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/hannah_zh/article/details/80691309
今日推荐