一、diff
用法:diff [options] files|directorys
输出信息:[num1,num2][a|c|d][num3,num4]
num1,num2 ##第一个文件中的行
a ##添加
c ##更改
d ##删除
< ##第一个文件中的内容
> ##第二个文件中的内容
num3,num4 ##第二个文件中的行
常用参数:
| -b | 忽略空格 |
| -B | 忽略空行 |
| -i | 忽略大小写 |
| -c | 显示文件所有内容并标示不同 |
| -r | 对比目录 |
| -u | 合并输出 |

二、patch
patch 原文件 补丁文件
-b ##备份文件


三、cut
| -d : | 指定:为分隔符 |
| -f | 指定显示的列 5第五列| 3,5 3和5列|3-5 3到5列|5- 第五列以后|-5 到第五列 |
| -c | 指定截取的字符(数字用法同-f) |

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


五、uniq
| -c | 合并重复并统计重复个数 |
| -d | 显示重复的行 |
| -u | 显示唯一的行 |

练习:
1.ifconfig 网卡 可以显示此网卡的信息
显示信息中包含此网卡使用的ip地址
请用命令过滤此ip并在输出时只显示ip其他信息不显示

2.找出能登陆系统用户中UID最大的用户,并显示其名称

六、tr
| tr 'a-z' 'A-Z' | 小写转大写 |
| tr 'A-Z' 'a-z' | 大写转小写 |

七、test
test = [ ] ##[ ] 就相当于test命令
"test $a = $b" = [ "$a" = "$b" ]
test数字对比
=
=!
| -eq | 等于 |
| -ne | 不等于 |
| -le | 小于等于 |
| -lt | 小于 |
| -ge | 大于等于 |
| -gt | 大于 |
[root@westoslinux mnt]# a=1
[root@westoslinux mnt]# b=1
[root@westoslinux mnt]# test "$a" = "$b" && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" = "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" != "$b" ] && echo yes || echo no
no
[root@westoslinux mnt]# a=2
[root@westoslinux mnt]# [ "$a" != "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# b=3
[root@westoslinux mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" -le "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" -eq "$b" ] && echo yes || echo no
no
test的条件关系
| -a | 并且 |
| -o | 或者 |
[root@westoslinux mnt]# c=5
[root@westoslinux mnt]# a=1
[root@westoslinux mnt]# b=9
[root@westoslinux mnt]# [ "$c" -ge "$a" -a "$c" -le "$b" ] && echo yes
yes || echo no ##判断c值是不是在a和b之间
[root@westoslinux mnt]# [ "$c" -le "$a" -o "$c" -ge "$b" ] && echo yes || echo no ##判断c值是不是小于a或大于b
no
[root@westoslinux mnt]# c=20
[root@westoslinux mnt]# [ "$c" -le "$a" -o "$c" -ge "$b" ] && echo yes || echo no
yes
test对空的判定
| -n | nozero 判定内容不为空 |
| -z | zero 判定内容为空 |
[root@westoslinux mnt]# [ -z "$d" ] && echo no || echo yes
no
[root@westoslinux mnt]# [ -n "$d" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ ! -n "$d" ] && echo yes || echo no
yes
test对于文件的判定
| -ef | 文件节点号是否一致(硬链) |
| -nt | 文件1是不是比文件2新 |
| -ot | 文件1是不是比文件2老 |
| -d | 目录 |
| -S | 套结字 |
| -L | 软链接 |
| -e | 存在 |
| -f | 普通文件 |
| -b | 快设备 |
| -c | 字符设备 |
[root@westoslinux mnt]# touch westos
[root@westoslinux mnt]# touch westos1
[root@westoslinux mnt]# ls -i
17676842 test.sh 17251759 westos 17676846 westos1
[root@westoslinux mnt]# [ "westos" -ef "westos1" ] && echo yes || echo no
no
[root@westoslinux mnt]# rm -fr westos1
[root@westoslinux mnt]# ln /mnt/westos /mnt/westos1
[root@westoslinux mnt]# ls -i
17676842 test.sh 17251759 westos 17251759 westos1
[root@westoslinux mnt]# [ "westos" -ef "westos1" ] && echo yes || echo no
yes
[root@westoslinux mnt]# rm -fr westos1
[root@westoslinux mnt]# touch westos1
[root@westoslinux mnt]# [ "westos" -nt "westos1" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ "westos" -ot "westos1" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "westos" -ot "westos1" ] && echo yes || echo no
no
[root@westoslinux mnt]# rm -fr westos1
[root@westoslinux mnt]# touch westos1
[root@westoslinux mnt]# [ "westos" -nt "westos1" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ "westos" -ot "westos1" ] && echo yes || echo no
yes
[root@westoslinux mnt]#
[root@westoslinux mnt]# [ -f "westos" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ -e "westos" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ -d "westos" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ -d "/mnt" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ -L "westos" ] && echo yes || echo no
no
[root@westoslinux mnt]# ln -s /mnt/westos /mnt/test1
[root@westoslinux mnt]# [ -L "test1" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ -b "test1" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ -b "/dev/vda" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ -c "/dev/vda" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ -c "/dev/pts/0" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ -S "/dev/pts/0" ] && echo yes || echo no
no
练习:
file_check.sh 在执行时
如果脚本后未指定检测文件报错“未指定检测文件,请指定”
如果脚本后指定文件不存在报错“此文件不存在”
当文件存在时请检测文件类型并显示到输出中
[root@westoslinux mnt]# vim file_check.sh
[ -z "$*" ] && {
echo 未指定检测文件,请指定
exit
}
[ ! -e "$*" ] && {
echo "$* is not exist"
exit
}
[ -d "$*" ] && {
echo "$* exists and is a directory"
exit
}
[ -L "$*" ] && {
echo "$* exists and is a symbolic link "
exit
}
[ -f "$*" ] && {
echo "$* exists and is a regular file"
exit
}
[ -b "$*" ] && {
echo "$* exists and is block special"
exit
}
[ -c "$*" ] && {
echo "$* exists and is character special"
exit
}
[ -S "$*" ] && {
echo "$* exists and is a socket"
exit
}
八、&& ||
| && | 符合条件做动作 |
| || | 不符合条件做动作 |
本文详细介绍了Linux中常用的几个命令行工具的使用方法,包括diff进行文件比较,patch应用补丁,cut进行字段裁剪,sort进行排序,uniq处理重复行,以及test进行条件检查。此外,还演示了如何利用这些工具进行实际操作,并提供了练习题以加深理解。通过这些工具,可以高效地管理和操作Linux系统。

被折叠的 条评论
为什么被折叠?



