shell的基本命令
####diff###
diff命令是用来比较两个命令或目录的不同
diff [ options] file1 file2
diff [ options] directory1 directory2
diff在比较文件过程中结果取读方式
[num1, num2] [a|c|d] [ num3,num4]
num1,num2表示在第一个文件的行数
a [add] ##表示添加
c[change] ##表示更改
d[delete] ##表示删除
<表示第一个文件的内容,
>表示第二个文件的第二行和内容,
> ---分隔线num3,num4 表示在第二个文件的行数
2.4c.4 表示改变第一个文件中的第二行和第四行才能匹配第二个文件中的第二行和第四行
diff 在比较目录过程中结果的取读
only in directory/:filename 表示只在directory目录中存在filename文件
diff中常用的参数
-b或–ignore-space-change ##不检查空格字符
[root@localhost mnt]# cat westos1
hello 1234 ##之间一个空格
[root@localhost mnt]# cat westos
hello 1234 ##之间3个空格
[root@localhost mnt]# diff westos westos1
1c1 ##改变第一个文件的第一行才能匹配到第二个文件的第一行
< hello 1234 ##第一个文件的内容
--- ##分隔线
> hello 1234 ##第二个文件的内容
[root@localhost mnt]# diff -b westos westos1 ##参数-b 不检查空格字符,两个文件就没有不同了
root@localhost mnt]#
-B或–ignore-blank -lines ##不检查空白行
[root@localhost mnt]# cat westos
hello,1234 ##没空白行
[root@localhost mnt ]# cat westos1
hello,1234 ##有两个空白行
[root@localhost mnt]# diff westos westos1
1a2,3 ##第一个文件增加第二和第三行才能匹配第二个文件
> ##第二个文件第二行的内容
> ##第二个文件第三行的内容
[root@localhost mnt]# diff -B westos westos1 ##参数-B,不检查空白行,两个文件就没有不同
[root@localhost mnt]#
-c ##显示全部内文,并标出不同之处
[root@localhost mnt]# cat westos1
hello,1234 westos
[root@localhost mnt]# cat westos
hello,1234
[root@localhost mnt]# diff -c westos westos1
*** westos 2019-05-21 22:25:40.355400825 -0400 ##***表示第一个文件
--- westos1 2019-05-21 22:26:00.180400825 -0400 ##---表示第二个文件
***************
*** 1,2 **** ## !表示存在的不同的地方
! hello,1234
!
--- 1 ----
! hello,1234 westos
-i或–ignore-case ##不检查大小写的不同之处
[root@localhost mnt]# cat westos1
hello,1234
[root@localhost mnt]# cat westos
HELLO,1234
[root@localhost mnt]# diff westos westos1
1c1 ##改变第一个文件的第一行,才和第二个文件匹配
< HELLO,1234
---
> hello,1234
[root@localhost mnt]# diff -i westos westos1 ##参数i,不检查大小写的不同之处
[root@localhost mnt]#
-p: 若比较的文件为C语言的程序码文件时,显示差异所在的函数名称;
[root@localhost mnt]# cat westos.c
#include<stdio.h>
main()
{
print ("hello world\n");
}
[root@localhost mnt]# cat westos1.c
#include<stdio.h>
main()
{
print ("Hello world\n");
}
[root@localhost mnt]# diff westos.c westos1.c ##参数-p,显示差异所在的函数名称
4c4 ##改变第一个文件的第四行后才可以匹配第二个人文件
< print ("hello world\n");
---
> print ("Hello world\n");
[root@localhost mnt]# diff -p westos.c westos1.c
*** westos.c 2019-05-21 23:11:04.034400825 -0400
--- westos1.c 2019-05-21 23:11:43.196400825 -0400
***************
*** 1,5 ****
#include<stdio.h>
main()
{
! print ("hello world\n"); ##不同所在函数的名称
}
--- 1,5 ----
#include<stdio.h>
main()
{
! print ("Hello world\n"); ##不同所在函数的名称
}
-q 或–brief : 仅显示有无差异,不显示详细信息
[root@localhost mnt]# cat westos1
hello,1234
[root@localhost mnt]# cat westos
HELLO,1234
[root@localhost mnt]# diff westos westos1
1c1
< HELLO,1234
---
> hello,1234
[root@localhost mnt]# diff -q westos westos1 ##-q仅显示有无差异,不显示详细信息
Files westos and westos1 differ
-r或–recursive :比较子目录中的文件
[root@localhost mnt]# ls -R dir1 ##查看目录dir1
dir1:
test ##子目录test
dir1/test:
top1 ##子目录下的文件top1
[root@localhost mnt]# ls -R dir2 ##查看目录dir2
dir2:
test ##子目录test
dir2/test:
top2##子目录下的文件top2
[root@localhost mnt]# diff dir1 dir2
Common subdirectories: dir1/test and dir2/test
[root@localhost mnt]# diff -r dir1 dir2
Only in dir1/test: top1
Only in dir2/test: top2
[root@localhost mnt]#
-u 以合并的方式显示文件内容的不同
[root@localhost mnt]# cat westos1
hello,1234
[root@localhost mnt]# cat westos
HELLO,1234
[root@localhost mnt]# diff -u westos westos1##-u 以合并的方式显示文件内容的不同
--- westos 2019-05-21 22:39:09.641400825 -0400 ###---表示第一个文件
+++ westos1 2019-05-21 22:38:46.706400825 -0400 ##+++表示第二个文件
@@ -1 +1 @@ ##第一个文件的第一行,第二个文件的第一行
-HELLO,1234 ##第一个文件第一行的内容
+hello,1234 ##第二个文件第一行的内容
##打补丁的方式对文件进行修补
patch 命令
yum install -y patch ##安装插件
patch常用来打补丁修改文件中的不同之处,使两个文件一致
diff -u test1 test2 > test.path ##生成补丁文件,这是test1和test2文件相比较产生的补丁,对test1生效。
patch test1 test.path ##使用生成的补丁文件对test1文件修补,这种修改方式会覆盖源文件,无备份;
[root@localhost mnt]# patch test1 test.path ##对test1 修补
patching file test1
[root@localhost mnt]# cat test1
hellow,123
[root@localhost mnt]# ls
test1 test2 test.path
[root@localhost mnt]# cat test2
hellow,123
[root@localhost mnt]# patch test2 test.path ##对test1 修补,会询问是否需要覆盖源文件
patching file test2
Reversed (or previously applied) patch detected! Assume -R? [n]
patch -b test1 test.path ##使用生成的补丁文件对test1文件修补,这种修改方式会修改test1,也会自动备份源内容到.orig文件;
###基本命令cut###
cut 命令多用于字符截取
cut -d ##指定分隔符
cut -f 1,7|1-7 ##指定截取的列
cut -c 1,1-4 ##指定截取的字符的位置
注:如果不小心删除了/etc/passwd源文件,
没关机之前cp /etc/passwd- /etc/passwd ##从备份的文件/etc/passwd-中还原
cut -d : -f 1 passwd ##以 :为分隔符,截取文件的第一列
cut -d : -f 1,7 passwd ##以 :为分隔符,截取文件的第一列和第七列
cut -d : -f 1-4 passwd ##以 :为分隔符,截取文件的第一到第四列
cut -c 2 passwd ##截取文件第第二个字符
cut -c 2,5 passwd##截取文件第第二个和第五个字符
cut -c 1-5 passwd##截取文件第第一个到第五个字符
测试:
用cut命令获取ip地址
[root@localhost mnt]# ifconfig eth0 | head -n 2 | tail -n 1 | cut -c 14-27
172.25.254.140 ##筛选文件前两行中最后一行,截取第十四到第二十七个字符
[root@localhost mnt]# ifconfig eth0 | head -n 2 | tail -n 1 | cut -d " " -f 10
172.25.254.140 ##筛选文件前两行中最后一行,以空格符为分隔符截取第十列
[root@localhost mnt]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.140 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::5054:ff:fe46:2899 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:46:28:99 txqueuelen 1000 (Ethernet)
RX packets 11180 bytes 5228356 (4.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5726 bytes 780464 (762.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
##sort命令###
sort 多用于字符排序
sort -n ##纯数字排序
sort -r ##倒序
sort -u ##去掉重复数字
sort -o ##输出到指定文件中
sort -t ##指定分隔符
sort -k ##指定要排序的列
sort lee ##对文件中的数字进行排序(按第一位数字的大小排)
sort -r lee ##对文件中的数字进行倒序排序(按第一位数字的大小排)
sort -n lee ##对文件中的内容按纯数字进行排序
sort -rn lee ##对文件中的内容按纯数字进行倒序排序
sort -rnu lee ##对文件中的内容按纯数字进行倒序排序并去掉重复的,
sort -rnu lee -o file##对文件中的内容按纯数字进行倒序排序并去掉重复的,并将结果输出到指定文件file
sort -t ##指定分隔符 sort -k ##指定排序的列
sort -t : -k 2 -n lee ##以:为分隔符对第二列进行排序
sort -t : -k 1 -n lee ##以:为分隔符对第一列进行排序
测试:
对/mnt 中的文件按容量大小进行排序
[root@localhost mnt]# touch file{1..5}
[root@localhost mnt]# ll
total 16
-rw-r--r--. 1 root root 47 May 22 02:00 file
-rw-r--r--. 1 root root 0 May 22 02:51 file1
-rw-r--r--. 1 root root 0 May 22 02:51 file2
-rw-r--r--. 1 root root 0 May 22 02:51 file3
-rw-r--r--. 1 root root 0 May 22 02:51 file4
-rw-r--r--. 1 root root 0 May 22 02:51 file5
-rw-r--r--. 1 root root 105 May 22 02:39 lee
-rw-r--r--. 1 root root 612 May 22 01:19 passwd
-rw-r--r--. 1 root root 9 May 22 02:28 test
[root@localhost mnt]# ll | sort -t " " -k 5
total 16
-rw-r--r--. 1 root root 0 May 22 02:51 file1
-rw-r--r--. 1 root root 0 May 22 02:51 file2
-rw-r--r--. 1 root root 0 May 22 02:51 file3
-rw-r--r--. 1 root root 0 May 22 02:51 file4
-rw-r--r--. 1 root root 0 May 22 02:51 file5
-rw-r--r--. 1 root root 105 May 22 02:39 lee
-rw-r--r--. 1 root root 47 May 22 02:00 file
-rw-r--r--. 1 root root 612 May 22 01:19 passwd
-rw-r--r--. 1 root root 9 May 22 02:28 test
[root@localhost mnt]# ll | sort -t " " -k 5 -n
-rw-r--r--. 1 root root 0 May 22 02:51 file1
-rw-r--r--. 1 root root 0 May 22 02:51 file2
-rw-r--r--. 1 root root 0 May 22 02:51 file3
-rw-r--r--. 1 root root 0 May 22 02:51 file4
-rw-r--r--. 1 root root 0 May 22 02:51 file5
total 16
-rw-r--r--. 1 root root 9 May 22 02:28 test
-rw-r--r--. 1 root root 47 May 22 02:00 file
-rw-r--r--. 1 root root 105 May 22 02:39 lee
-rw-r--r--. 1 root root 612 May 22 01:19 passwd
[root@localhost mnt]# ll | grep total -v ##grep total -v 除了total 之外的排序
-rw-r--r--. 1 root root 47 May 22 02:00 file
-rw-r--r--. 1 root root 0 May 22 02:51 file1
-rw-r--r--. 1 root root 0 May 22 02:51 file2
-rw-r--r--. 1 root root 0 May 22 02:51 file3
-rw-r--r--. 1 root root 0 May 22 02:51 file4
-rw-r--r--. 1 root root 0 May 22 02:51 file5
-rw-r--r--. 1 root root 105 May 22 02:39 lee
-rw-r--r--. 1 root root 612 May 22 01:19 passwd
-rw-r--r--. 1 root root 9 May 22 02:28 test
[root@localhost mnt]# ll | sort -t " " -k 5 -rn | grep "total" -v ##以空格符为分隔符,指定第五列的大小 除了total 之外的排序倒序排列
-rw-r--r--. 1 root root 612 May 22 01:19 passwd
-rw-r--r--. 1 root root 105 May 22 02:39 lee
-rw-r--r--. 1 root root 47 May 22 02:00 file
-rw-r--r--. 1 root root 9 May 22 02:28 test
-rw-r--r--. 1 root root 0 May 22 02:51 file5
-rw-r--r--. 1 root root 0 May 22 02:51 file4
-rw-r--r--. 1 root root 0 May 22 02:51 file3
-rw-r--r--. 1 root root 0 May 22 02:51 file2
-rw-r--r--. 1 root root 0 May 22 02:51 file1
##uniq命令###
uniq 对重复的字符作出相应处理
uniq -u ##显示唯一行
uniq -d ##显示重复行
uniq -c ##每行显示一次并统计重复次数
sort -n -t : -k 1 lee | uniq -d ##以分隔符为间隔符,第一列排序显示重复行
sort -n -t : -k 1 lee | uniq -u##以分隔符为间隔符,第一列排序显示唯一行
sort -n -t : -k 1 lee | uniq -c##以分隔符为间隔符,第一列排序显示一次行并统计重复的行数次数
注意:
uniq 一般配合sort命令的使用
[root@localhost mnt]# sort -n lee
1:3
1:3
1:324
1:4
1:5
1:5
1:67
1:67
1:67
3:1
3:13
3:3
3:32
3:454
3:46
6:432
6:45
6:454
6:545
6:654
6:87
[root@localhost mnt]# sort -n lee | uniq -c
1
2 1:3
1 1:324
1 1:4
2 1:5
3 1:67
1 3:1
1 3:13
1 3:3
1 3:32
1 3:454
1 3:46
1 6:432
1 6:45
1 6:454
1 6:545
1 6:654
1 6:87
[root@localhost mnt]# sort -n lee | uniq -d
1:3
1:5
1:67
[root@localhost mnt]# sort -n lee | uniq -u
1:324
1:4
3:1
3:13
3:3
3:32
3:454
3:46
6:432
6:45
6:454
6:545
6:654
6:87
###tr命令##
tr进行字符大小写的转换
tr ‘a-z’ ‘A-Z’ ##将小写转换成大写
tr ‘A-Z’ ‘a-z’ ##将大写转换成小写
一般磁盘加密时必须输入大写YES,否则无法加密成功
编写一个脚本,给磁盘加密时输入yes不区分大小写进行加密
[root@localhost mnt]# cryptsetup luksFormat /dev/vdb3 ##给/dev/vdb3加密
WARNING!
========
This will overwrite data on /dev/vdb3 irrevocably.
Are you sure? (Type uppercase yes): YES ##必须输入大写字母
Enter passphrase:
Verify passphrase:
[root@localhost mnt]# sh crypt_disk.sh /dev/vdb3 ##使用脚本给磁盘加密
WARNING!
===========
This will overwrite date on /dev/vdb1 irrevocably.
Are you sure? (Type uppercase yes): yes ##不区分大小写
Enter passphrase:westos123
Verify passphrase:westos123
[root@localhost mnt]# blkid ##查看可用磁盘
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/vdb1: UUID="SIaPf4-OdHu-OzAW-NlQG-vZ3D-X8ZO-1FK3Ih" TYPE="LVM2_member"
/dev/vdb2: UUID="00fc0b59-7241-4ea5-955a-190a4060f5b3" TYPE="crypto_LUKS"
/dev/mapper/vg0-vo: UUID="12294be2-bdad-4817-b162-038e22313d9f" TYPE="ext4"
/dev/vdb3: UUID="38989828-f36a-4bcc-886d-7ee76379ae00" TYPE="crypto_LUKS"
##磁盘加密成功