•diff 命令
•patch 命令
•grep 命令
•Cut 命令
•sort 命令
•uniq 命令
•tr 命令
•sed 命令
diff 命令
• diff 命令用于比较两个文件的内容 , 以了解其区别。它还可用于创建补丁文件。补丁文件用于在企业环境的多台计算机之间对相似文件进行更改.
-c ##显示上下文周围的行
-u ##使用统一输出格式(对于生成补丁文件很有用)
-r ##从指定的目录开始文件执行递归式比较
修补命令 patch
•
patch 采用补丁文件 patchfile ( 包含由 diff 生成的差异列表 ) 并将这些差异应用于生成补丁版的一个或多个原始文件。通常 , 补丁版替换原始文件 , 但当指定 -b 选项时 , 可以制作备份。将用 .orig 文件名后缀重命名原始文件
• patch 可用于将简单的补丁文件应用于使用以下语法的单个文件
– [root@host etc]# patch issue patchfile
Patching file issue
•
以下命令显示如何使用通过 diff -Naur 创建的补丁文件。用户更改为与从中创建补丁文件的原始目录相似的可比较目录后 , 将执行 patch
– [user@host orig-dir]$ patch -b < /tmp/patchfile
Patching file hosts
Patching file network
117 vim file
118 vim file1
119 cat file
120 cat file1
121 diff file file1 ##比较不同
122 diff -c file file1 ##显示上下文周围的行
123 diff -u file file1 ##使用统一输出格式(对于生成补丁文件很有用)
diff -r ##从指定的目录开始文件执行递归式比较
124 diff -u file file1 > file.path
125 cat file.path
126 yum install patch -y
127 patch file file.path
128 cat file
129 ls
130 vim file
131 cat file
133 patch -b file file.path ##这一步生成file.orig文件,是法ile之前的内容
134 ls
135 cat file.orig
136 cat file
137 history
[root@localhost ~]# cat file
hello
[root@localhost ~]# cat file1
hello
123
[root@localhost ~]# diff file file1
1a2
> 123
[root@localhost ~]# diff -c file file1
*** file 2017-04-29 21:08:28.338550683 -0400
--- file1 2017-04-29 21:08:51.161550683 -0400
***************
*** 1 ****
--- 1,2 ----
hello
+ 123
[root@localhost ~]# diff -u file file1
--- file 2017-04-29 21:08:28.338550683 -0400
+++ file1 2017-04-29 21:08:51.161550683 -0400
@@ -1 +1,2 @@
hello
+123
[root@localhost ~]# diff -u file file1 > file.path
[root@localhost ~]# cat file.path
--- file 2017-04-29 21:08:28.338550683 -0400
+++ file1 2017-04-29 21:08:51.161550683 -0400
@@ -1 +1,2 @@
hello
+123
[root@localhost ~]# yum install patch -y
Loaded plugins: langpacks
rhel_dvd | 4.1 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package patch.x86_64 0:2.7.1-8.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
patch x86_64 2.7.1-8.el7 rhel_dvd 110 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 110 k
Installed size: 210 k
Downloading packages:
patch-2.7.1-8.el7.x86_64.rpm | 110 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : patch-2.7.1-8.el7.x86_64 1/1
Verifying : patch-2.7.1-8.el7.x86_64 1/1
Installed:
patch.x86_64 0:2.7.1-8.el7
Complete!
[root@localhost ~]# patch file file.path
patching file file
[root@localhost ~]# cat file
hello
123
[root@localhost ~]# ls
anaconda-ks.cfg diskpass Downloads file1 Music Public Videos
Desktop Documents file file.path Pictures Templates
[root@localhost ~]# vim file
[root@localhost ~]# cat file
hello
[root@localhost ~]# patch -b file file.patch
patch: **** Can't open patch file file.patch : No such file or directory
[root@localhost ~]# patch -b file file.path
patching file file
[root@localhost ~]# ls
anaconda-ks.cfg Documents file1 Music Templates
Desktop Downloads file.orig Pictures Videos
diskpass file file.path Public
[root@localhost ~]# cat file.orig
hello
[root@localhost ~]# cat file
hello
123
[root@localhost ~]# history
-----------------------------------------------------------------------------------------
grep 命令
• grep 将显示文件中与模式匹配的行。其也可以处理标准输入
• 模式可以包含正则表达式元字符 , 因此始终为正则表达式加引号通常被视为一种好办法。在本单元后面的部分中将介绍
-i ##执行不区分大小写搜索
-n ##前置返回行的行号
-r ##对文件执行递归式搜索,从命名目录开始
-c ##显示具有匹配模式的行的计数
-v ##返回不包含模式的行
-E ##后面可以跟正则表达式
149 vim passwd ##太多内容了,删掉一些,再加上一些,以便于搜索做实验
150 cat passwd
151 grep test passwd ##在passwd中精确匹配带有test的选项
152 grep -i test passwd ##执行不区分大小写搜索
153 grep -i test passwd -v ##反向搜索
154 grep -i -E "test|root" passwd ##-E后面可以跟正则表达式,此时匹配有test或者root的
155 grep -i -E "^test|root" passwd ##行首为test的或者行中有root的
156 grep -i -E "^test" passwd
157 grep test$ passwd ##行末为test的
158 grep "test" passwd ##等于grep test passwd
159 grep test passwd | grep -E "^test|test$" -v ##有test的行并且test不再行首或行末
160 grep test passwd -c ##显示具有匹配模式的行的计数
161 touch file
162 touch file1
163 echo westos > file1
164 cat file1
165 grep westos /mnt/
166 grep westos -r /mnt/ ##对文件执行递归式搜索,从命名目录开始
167 grep westos -r /mnt/ -n ##-n前置返回行的行号
168 history
---------------------------------------------------------------------------
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ls
[root@localhost mnt]# cp /etc/passwd /mnt
[root@localhost mnt]# ls
passwd
[root@localhost mnt]# vim passwd
[root@localhost mnt]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
root:test:root
test:root:test
test:root:root
root:test:test
TEST:ROOT:TEST
[root@localhost mnt]# grep test passwd
root:test:root
test:root:test
test:root:root
root:test:test
[root@localhost mnt]# grep -i test passwd
root:test:root
test:root:test
test:root:root
root:test:test
TEST:ROOT:TEST
[root@localhost mnt]# grep -i test passwd -v
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
[root@localhost mnt]# grep -i -E "test|root" passwd
root:x:0:0:root:/root:/bin/bash
root:test:root
test:root:test
test:root:root
root:test:test
TEST:ROOT:TEST
[root@localhost mnt]# grep -i -E "^test|root" passwd
root:x:0:0:root:/root:/bin/bash
root:test:root
test:root:test
test:root:root
root:test:test
TEST:ROOT:TEST
[root@localhost mnt]# grep -i -E "^test" passwd
test:root:test
test:root:root
TEST:ROOT:TEST
[root@localhost mnt]# grep test$ passwd
test:root:test
root:test:test
[root@localhost mnt]# grep "test" passwd
root:test:root
test:root:test
test:root:root
root:test:test
[root@localhost mnt]# grep test passwd | grep -E "^test|test$" -v
root:test:root
[root@localhost mnt]# grep test passwd -c
4
[root@localhost mnt]# touch file
[root@localhost mnt]# touch file1
[root@localhost mnt]# echo westos > file1
[root@localhost mnt]# cat file1
westos
[root@localhost mnt]# grep westos /mnt/
grep: /mnt/: Is a directory
[root@localhost mnt]# grep westos -r /mnt/
/mnt/file1:westos
[root@localhost mnt]# grep westos -r /mnt/ -n
/mnt/file1:1:westos
[root@localhost mnt]# history
-------------------------------------------------------------------------------
cut 命令
• cut 用于 “ 剪切 ” 文件中的文本字段或列并将其显示到标准输出
-d ##指定用于提取字段的分隔符(Tab时默认值)
-f ##指定要从每行中提取的字段
-c ##指定要从每行中提取的文本列
[root@localhost mnt]# history
1 cut -d : -f 1 passwd ##以:为分隔符,提取第一字段
2 cut -d : -f 1,7 passwd ##截取1和7部分
3 cut -d : -f 1-3 passwd ##截取1至3部分
4 cut -c 2-4 passwd ##截取2到4字符
5 history
应用:----------------------------------------------------------------------------
[root@localhost mnt]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.244 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::5054:ff:fe00:2c0b prefixlen 64 scopeid 0x20<link>
ether 52:54:00:00:2c:0b txqueuelen 1000 (Ethernet)
RX packets 1350 bytes 140946 (137.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3929 bytes 247429 (241.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 2195 bytes 206440 (201.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2195 bytes 206440 (201.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost mnt]# 要求:只把ip地址截出来^C
[root@localhost mnt]# ifconfig eth0 |grep netmask| cut -c 14-27
172.25.254.244
[root@localhost mnt]# 但是,这种方法没有普适性^C
[root@localhost mnt]# ifconfig eth0 | grep inet | grep inet6 -v |cut -d " " -f 10
172.25.254.244
[root@localhost mnt]# 这种方法和上面那种原理一样,没有普适性。^C
[root@localhost mnt]# ifconfig eth0 | grep inet | grep inet6 -v |awk -F " " '{print $2}'
172.25.254.244
[root@localhost mnt]#
------------------------------------------------------------------------------
[root@localhost mnt]# cut -d : -f 1 passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
test
root
test
root
TEST
[root@localhost mnt]# cut -d : -f 1,7 passwd
root:/bin/bash
bin:/sbin/nologin
daemon:/sbin/nologin
adm:/sbin/nologin
lp:/sbin/nologin
sync:/bin/sync
shutdown:/sbin/shutdown
halt:/sbin/halt
mail:/sbin/nologin
operator:/sbin/nologin
games:/sbin/nologin
test
root
test
root
TEST
[root@localhost mnt]# cut -d : -f 1-3 passwd
root:x:0
bin:x:1
daemon:x:2
adm:x:3
lp:x:4
sync:x:5
shutdown:x:6
halt:x:7
mail:x:8
operator:x:11
games:x:12
test:root:root
root:root:test
test:root:test
root:test:root
TEST:root:test
[root@localhost mnt]# cut -c 2-4 passwd
oot
in:
aem
dm:
p:x
ync
hut
alt
ail
per
ame
est
oot
est
oot
EST
----------------------------------------------------------------------------------
sort 命令
• sort 用于排序文本数据。该数据可以位于文件中或其他命令输出中。 Sort 通常与管道一起使用
-n ##按数值而非字符排序
-k ##设置排序字段
-t ##指定其他字段分隔符(默认为空格)
uniq 命令
•
uniq“ 删除 ” 文件中重复的相邻行。若要只打印文件中出现的唯一行(“ 删除 ” 所有重复行 ), 必须首先对 uniq 的输入进行排序。由于可以为uniq 指定其决策所基于的字段或列 , 因此这些字段或列是对其输入进行排序所必须的字段或列。如果未与选项一起使用 , uniq 会使用整个记录作为决策键 , 删除其输入中的重复行
-u ##仅显示唯一行
-d ##显示重复行
-c ##每行显示一次(包括出现计数)
1 vim file
2 cat file
3 sort file ##按字符排序
4 sort -n file ##按数字大小排序
5 sort -rn file ##倒序按数字排序
6 sort -rnu file ##-u 去掉重复值
7 sort -rn file |uniq -c ##倒序按数字排序,且每行只显示一次(包括出现计数)
8 sort -rn file | uniq -d ##显示出重复的行,不显示不重复的行
9 sort -rn file | uniq -u ##仅显示唯一行
10 vim file
11 cat file
12 sort file
13 sort -n file
14 sort -t : -k 3 -n file ##设置排序字段间隔符为:字段为3
15 sort -t : -k 3 -n file |uniq -c
16 history
------以%MEM排序,显示前5个进程的PID---------------------------------------------------
[root@localhost mnt]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 52840 6500 ? Ss 11:53 0:03 /usr/lib/systemd/systemd --switch
root 2 0.0 0.0 0 0 ? S 11:53 0:00 [kthreadd]
root 17 0.0 0.0 0 0 ? S< 11:53 0:00 [kintegrityd]
root 618 0.0 0.0 16748 992 ? SNs 11:53 0:00 /usr/sbin/alsactl -s -n 19 -c -E
root 620 0.0 1.1 329432 21008 ? Ssl 11:53 0:00 /usr/bin/python -Es /usr/sbin/fir
avahi 623 0.0 0.0 30028 1484 ? Ss 11:53 0:00 avahi-daemon: running [linux.loca
root 624 0.0 0.8 550008 15996 ? Ssl 11:53 0:00 /usr/bin/python -Es /usr/sbin/tun
root 629 0.0 0.2 283832 4432 ? Ssl 11:53 0:00 /usr/sbin/rsyslogd -n
root 635 0.0 0.2 209780 4368 ? Ss 11:53 0:00 /usr/bin/abrt-watch-log -F BUG: W
libstor+ 639 0.0 0.0 4336 364 ? Ss 11:53 0:00 /usr/bin/lsmd -d
root 32294 0.0 0.0 0 0 ? R 17:53 0:00 [kworker/0:3]
root 32370 0.0 0.0 107892 364 ? S 17:57 0:00 sleep 60
root 32379 0.0 0.0 123356 1320 pts/0 R+ 17:57 0:00 ps aux
[root@localhost mnt]# ps ax -o pid --sort -%mem |head -n 5
PID
2010
1947
737
2066
[root@localhost mnt]# ps ax -o pid --sort -%mem |grep PID -v |head -n 5
2010
1947
737
2066
620
---------------------------------------------------------------------------------
[root@localhost mnt]# vim file
[root@localhost mnt]# cat file
1
3
5
7
9
2
4
6
8
16
11
34
56
12
1
3
5
25
22
[root@localhost mnt]# sort file
1
1
11
12
16
2
22
25
3
3
34
4
5
5
56
6
7
8
9
[root@localhost mnt]# sort -n file
1
1
2
3
3
4
5
5
6
7
8
9
11
12
16
22
25
34
56
[root@localhost mnt]# sort -rn file
56
34
25
22
16
12
11
9
8
7
6
5
5
4
3
3
2
1
1
[root@localhost mnt]# sort -rnu file
56
34
25
22
16
12
11
9
8
7
6
5
4
3
2
1
[root@localhost mnt]# sort -rn file |uniq -c
1 56
1 34
1 25
1 22
1 16
1 12
1 11
1 9
1 8
1 7
1 6
2 5
1 4
2 3
1 2
2 1
[root@localhost mnt]# sort -rn file | uniq -d
5
3
1
[root@localhost mnt]# sort -rn file | uniq -u
56
34
25
22
16
12
11
9
8
7
6
4
2
[root@localhost mnt]# vim file
[root@localhost mnt]# cat file
44:a:1
35:a:3
2:a:5
23:a:7
5:a:9
7:a:2
5:a:4
1:a:6
9:a:8
3:a:16
3:a:11
3:a:34
3:a:56
3:a:12
23:a:1
8:a:3
6:a:5
99:a:25
8:a:22
[root@localhost mnt]# sort file
1:a:6
23:a:1
23:a:7
2:a:5
35:a:3
3:a:11
3:a:12
3:a:16
3:a:34
3:a:56
44:a:1
5:a:4
5:a:9
6:a:5
7:a:2
8:a:22
8:a:3
99:a:25
9:a:8
[root@localhost mnt]# sort -n file
1:a:6
2:a:5
3:a:11
3:a:12
3:a:16
3:a:34
3:a:56
5:a:4
5:a:9
6:a:5
7:a:2
8:a:22
8:a:3
9:a:8
23:a:1
23:a:7
35:a:3
44:a:1
99:a:25
[root@localhost mnt]# sort -t : -k 3 -n file
23:a:1
44:a:1
7:a:2
35:a:3
8:a:3
5:a:4
2:a:5
6:a:5
1:a:6
23:a:7
9:a:8
5:a:9
3:a:11
3:a:12
3:a:16
8:a:22
99:a:25
3:a:34
3:a:56
[root@localhost mnt]# sort -t : -k 3 -n file |uniq -c
1 23:a:1
1 44:a:1
1 7:a:2
1 35:a:3
1 8:a:3
1 5:a:4
1 2:a:5
1 6:a:5
1 1:a:6
1 23:a:7
1 9:a:8
1 5:a:9
1 3:a:11
1 3:a:12
1 3:a:16
1 8:a:22
1 99:a:25
1 3:a:34
1 3:a:56
--------------------------------------------------------------------------------
tr 命令
• tr 用于转字符 : 即 , 如果给定了两个字符范围 , 则只要发现某个字符位于第一个范围中 , 就会将其转换为第二个范围中对等的字符。该命令通常在 shell 脚本中使用 , 以按预期情况转换数据
• tr 'A-Z' 'a-z' <file
1 vim file
2 cat file
3 tr 'a-z' 'A-Z' < file
4 tr 'A-Z' 'a-z' < file
5 history
-------------------------------------------------------------
[root@localhost mnt]# vim file
[root@localhost mnt]# cat file
westos
WESTOS
[root@localhost mnt]# tr 'a-z' 'A-Z' < file
WESTOS
WESTOS
[root@localhost mnt]# tr 'A-Z' 'a-z' < file
westos
westos
[root@localhost mnt]# history
-------------------------------------------------------------
sed 命令
• sed 命令是流编辑器 , 用于对文本数据流执行编辑。假定要处理一个文件名 , sed 将对文件中的所有行执行搜索和替换 , 以将修改后的数据发送到标准输出 ; 即 , 其实际上并不修改现有文件。与 grep 一样 , sed 通常在管道中使用。
• 由于 sed 命令通常包含可以解释为 shell 元字符的字符 ,因此请按下面示例所示引用 sed 命令。默认情况下 , sed对文件中的所有行执行操作。在提供 sed 时 , 可带有地址。
s/old/new ##执行字符串转换,将old替换为new
d ##删除匹配的行
---------------------------------------------------------------------------
错误的添加用户方式:(这只是输出了,但是并没有添加用户)
[root@localhost mnt]# /mnt/file.sh 5
2
3
6
7
1
[root@localhost mnt]# cat file.sh
#!/bin/bash
for NUM in $(seq 1 $1)
do
sed -n ${NUM}p /mnt/userfile
done
[root@localhost mnt]# cat userfile
2
3
6
7
1
8
5
4
9
[root@localhost mnt]#
改正后:
[root@localhost mnt]# /mnt/file.sh 2
[root@localhost mnt]# id user1
uid=1001(user1) gid=1001(user1) groups=1001(user1)
[root@localhost mnt]# cat file.sh
#!/bin/bash
for NUM in $(seq 1 $1)
do
USERNAME=`sed -n ${NUM}p /mnt/userfile`
useradd $USERNAME
done
[root@localhost mnt]# id user3
id: user3: no such user
[root@localhost mnt]# cat userfile
user1
user2
user3
[root@localhost mnt]#
可以自动识别用户行数的
[root@localhost mnt]# vim file.sh
[root@localhost mnt]# mv /mnt/file.sh /bin/
[root@localhost mnt]# file.sh userfile
useradd: user 'user1' already exists
useradd: user 'user2' already exists
[root@localhost mnt]# id user3
uid=1003(user3) gid=1003(user3) groups=1003(user3)
[root@localhost mnt]# cat /bin/file.sh
#!/bin/bash
MAX_LINE=`wc -l $1 |awk -F " " '{print $1}'`
for NUM in `seq 1 $MAX_LINE`
do
USERNAME=`sed -n ${NUM}p $1`
useradd $USERNAME
done
[root@localhost mnt]#
不太完善的加密码的
[root@localhost mnt]# vim passwd
[root@localhost mnt]# vim passwd
[root@localhost mnt]# vim userfile
[root@localhost mnt]# vim /bin/file.sh
[root@localhost mnt]# file.sh userfile passwd
useradd: user 'user1' already exists
Changing password for user user1.
passwd: all authentication tokens updated successfully.
useradd: user 'user2' already exists
Changing password for user user2.
passwd: all authentication tokens updated successfully.
useradd: user 'user3' already exists
Changing password for user user3.
passwd: all authentication tokens updated successfully.
Changing password for user westos1.
passwd: all authentication tokens updated successfully.
Changing password for user westos2.
passwd: all authentication tokens updated successfully.
Changing password for user westos3.
passwd: all authentication tokens updated successfully.
[root@localhost mnt]# su - student
[student@localhost ~]$ passwd user1
passwd: Only root can specify a user name.
[student@localhost ~]$ exit
logout
[root@localhost mnt]# cat /bin/file.sh
#!/bin/bash
MAX_LINE=`wc -l $1 |awk -F " " '{print $1}'`
for NUM in `seq 1 $MAX_LINE`
do
USERNAME=`sed -n ${NUM}p $1`
PASSWD=$(sed -n ${NUM}p $2)
useradd $USERNAME >/dev/null
echo $PASSWD | passwd --stdin $USERNAME
done
[root@localhost mnt]#
-------------------------------------------------------------------------
•patch 命令
•grep 命令
•Cut 命令
•sort 命令
•uniq 命令
•tr 命令
•sed 命令
diff 命令
• diff 命令用于比较两个文件的内容 , 以了解其区别。它还可用于创建补丁文件。补丁文件用于在企业环境的多台计算机之间对相似文件进行更改.
-c ##显示上下文周围的行
-u ##使用统一输出格式(对于生成补丁文件很有用)
-r ##从指定的目录开始文件执行递归式比较
修补命令 patch
•
patch 采用补丁文件 patchfile ( 包含由 diff 生成的差异列表 ) 并将这些差异应用于生成补丁版的一个或多个原始文件。通常 , 补丁版替换原始文件 , 但当指定 -b 选项时 , 可以制作备份。将用 .orig 文件名后缀重命名原始文件
• patch 可用于将简单的补丁文件应用于使用以下语法的单个文件
– [root@host etc]# patch issue patchfile
Patching file issue
•
以下命令显示如何使用通过 diff -Naur 创建的补丁文件。用户更改为与从中创建补丁文件的原始目录相似的可比较目录后 , 将执行 patch
– [user@host orig-dir]$ patch -b < /tmp/patchfile
Patching file hosts
Patching file network
117 vim file
118 vim file1
119 cat file
120 cat file1
121 diff file file1 ##比较不同
122 diff -c file file1 ##显示上下文周围的行
123 diff -u file file1 ##使用统一输出格式(对于生成补丁文件很有用)
diff -r ##从指定的目录开始文件执行递归式比较
124 diff -u file file1 > file.path
125 cat file.path
126 yum install patch -y
127 patch file file.path
128 cat file
129 ls
130 vim file
131 cat file
133 patch -b file file.path ##这一步生成file.orig文件,是法ile之前的内容
134 ls
135 cat file.orig
136 cat file
137 history
-----------------------------------------------------------------------------------
[root@localhost ~]# vim file
[root@localhost ~]# vim file1[root@localhost ~]# cat file
hello
[root@localhost ~]# cat file1
hello
123
[root@localhost ~]# diff file file1
1a2
> 123
[root@localhost ~]# diff -c file file1
*** file 2017-04-29 21:08:28.338550683 -0400
--- file1 2017-04-29 21:08:51.161550683 -0400
***************
*** 1 ****
--- 1,2 ----
hello
+ 123
[root@localhost ~]# diff -u file file1
--- file 2017-04-29 21:08:28.338550683 -0400
+++ file1 2017-04-29 21:08:51.161550683 -0400
@@ -1 +1,2 @@
hello
+123
[root@localhost ~]# diff -u file file1 > file.path
[root@localhost ~]# cat file.path
--- file 2017-04-29 21:08:28.338550683 -0400
+++ file1 2017-04-29 21:08:51.161550683 -0400
@@ -1 +1,2 @@
hello
+123
[root@localhost ~]# yum install patch -y
Loaded plugins: langpacks
rhel_dvd | 4.1 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package patch.x86_64 0:2.7.1-8.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
patch x86_64 2.7.1-8.el7 rhel_dvd 110 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 110 k
Installed size: 210 k
Downloading packages:
patch-2.7.1-8.el7.x86_64.rpm | 110 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : patch-2.7.1-8.el7.x86_64 1/1
Verifying : patch-2.7.1-8.el7.x86_64 1/1
Installed:
patch.x86_64 0:2.7.1-8.el7
Complete!
[root@localhost ~]# patch file file.path
patching file file
[root@localhost ~]# cat file
hello
123
[root@localhost ~]# ls
anaconda-ks.cfg diskpass Downloads file1 Music Public Videos
Desktop Documents file file.path Pictures Templates
[root@localhost ~]# vim file
[root@localhost ~]# cat file
hello
[root@localhost ~]# patch -b file file.patch
patch: **** Can't open patch file file.patch : No such file or directory
[root@localhost ~]# patch -b file file.path
patching file file
[root@localhost ~]# ls
anaconda-ks.cfg Documents file1 Music Templates
Desktop Downloads file.orig Pictures Videos
diskpass file file.path Public
[root@localhost ~]# cat file.orig
hello
[root@localhost ~]# cat file
hello
123
[root@localhost ~]# history
-----------------------------------------------------------------------------------------
grep 命令
• grep 将显示文件中与模式匹配的行。其也可以处理标准输入
• 模式可以包含正则表达式元字符 , 因此始终为正则表达式加引号通常被视为一种好办法。在本单元后面的部分中将介绍
-i ##执行不区分大小写搜索
-n ##前置返回行的行号
-r ##对文件执行递归式搜索,从命名目录开始
-c ##显示具有匹配模式的行的计数
-v ##返回不包含模式的行
-E ##后面可以跟正则表达式
149 vim passwd ##太多内容了,删掉一些,再加上一些,以便于搜索做实验
150 cat passwd
151 grep test passwd ##在passwd中精确匹配带有test的选项
152 grep -i test passwd ##执行不区分大小写搜索
153 grep -i test passwd -v ##反向搜索
154 grep -i -E "test|root" passwd ##-E后面可以跟正则表达式,此时匹配有test或者root的
155 grep -i -E "^test|root" passwd ##行首为test的或者行中有root的
156 grep -i -E "^test" passwd
157 grep test$ passwd ##行末为test的
158 grep "test" passwd ##等于grep test passwd
159 grep test passwd | grep -E "^test|test$" -v ##有test的行并且test不再行首或行末
160 grep test passwd -c ##显示具有匹配模式的行的计数
161 touch file
162 touch file1
163 echo westos > file1
164 cat file1
165 grep westos /mnt/
166 grep westos -r /mnt/ ##对文件执行递归式搜索,从命名目录开始
167 grep westos -r /mnt/ -n ##-n前置返回行的行号
168 history
---------------------------------------------------------------------------
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ls
[root@localhost mnt]# cp /etc/passwd /mnt
[root@localhost mnt]# ls
passwd
[root@localhost mnt]# vim passwd
[root@localhost mnt]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
root:test:root
test:root:test
test:root:root
root:test:test
TEST:ROOT:TEST
[root@localhost mnt]# grep test passwd
root:test:root
test:root:test
test:root:root
root:test:test
[root@localhost mnt]# grep -i test passwd
root:test:root
test:root:test
test:root:root
root:test:test
TEST:ROOT:TEST
[root@localhost mnt]# grep -i test passwd -v
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
[root@localhost mnt]# grep -i -E "test|root" passwd
root:x:0:0:root:/root:/bin/bash
root:test:root
test:root:test
test:root:root
root:test:test
TEST:ROOT:TEST
[root@localhost mnt]# grep -i -E "^test|root" passwd
root:x:0:0:root:/root:/bin/bash
root:test:root
test:root:test
test:root:root
root:test:test
TEST:ROOT:TEST
[root@localhost mnt]# grep -i -E "^test" passwd
test:root:test
test:root:root
TEST:ROOT:TEST
[root@localhost mnt]# grep test$ passwd
test:root:test
root:test:test
[root@localhost mnt]# grep "test" passwd
root:test:root
test:root:test
test:root:root
root:test:test
[root@localhost mnt]# grep test passwd | grep -E "^test|test$" -v
root:test:root
[root@localhost mnt]# grep test passwd -c
4
[root@localhost mnt]# touch file
[root@localhost mnt]# touch file1
[root@localhost mnt]# echo westos > file1
[root@localhost mnt]# cat file1
westos
[root@localhost mnt]# grep westos /mnt/
grep: /mnt/: Is a directory
[root@localhost mnt]# grep westos -r /mnt/
/mnt/file1:westos
[root@localhost mnt]# grep westos -r /mnt/ -n
/mnt/file1:1:westos
[root@localhost mnt]# history
-------------------------------------------------------------------------------
cut 命令
• cut 用于 “ 剪切 ” 文件中的文本字段或列并将其显示到标准输出
-d ##指定用于提取字段的分隔符(Tab时默认值)
-f ##指定要从每行中提取的字段
-c ##指定要从每行中提取的文本列
[root@localhost mnt]# history
1 cut -d : -f 1 passwd ##以:为分隔符,提取第一字段
2 cut -d : -f 1,7 passwd ##截取1和7部分
3 cut -d : -f 1-3 passwd ##截取1至3部分
4 cut -c 2-4 passwd ##截取2到4字符
5 history
应用:----------------------------------------------------------------------------
[root@localhost mnt]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.244 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::5054:ff:fe00:2c0b prefixlen 64 scopeid 0x20<link>
ether 52:54:00:00:2c:0b txqueuelen 1000 (Ethernet)
RX packets 1350 bytes 140946 (137.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3929 bytes 247429 (241.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 2195 bytes 206440 (201.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2195 bytes 206440 (201.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost mnt]# 要求:只把ip地址截出来^C
[root@localhost mnt]# ifconfig eth0 |grep netmask| cut -c 14-27
172.25.254.244
[root@localhost mnt]# 但是,这种方法没有普适性^C
[root@localhost mnt]# ifconfig eth0 | grep inet | grep inet6 -v |cut -d " " -f 10
172.25.254.244
[root@localhost mnt]# 这种方法和上面那种原理一样,没有普适性。^C
[root@localhost mnt]# ifconfig eth0 | grep inet | grep inet6 -v |awk -F " " '{print $2}'
172.25.254.244
[root@localhost mnt]#
------------------------------------------------------------------------------
[root@localhost mnt]# cut -d : -f 1 passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
operator
games
test
root
test
root
TEST
[root@localhost mnt]# cut -d : -f 1,7 passwd
root:/bin/bash
bin:/sbin/nologin
daemon:/sbin/nologin
adm:/sbin/nologin
lp:/sbin/nologin
sync:/bin/sync
shutdown:/sbin/shutdown
halt:/sbin/halt
mail:/sbin/nologin
operator:/sbin/nologin
games:/sbin/nologin
test
root
test
root
TEST
[root@localhost mnt]# cut -d : -f 1-3 passwd
root:x:0
bin:x:1
daemon:x:2
adm:x:3
lp:x:4
sync:x:5
shutdown:x:6
halt:x:7
mail:x:8
operator:x:11
games:x:12
test:root:root
root:root:test
test:root:test
root:test:root
TEST:root:test
[root@localhost mnt]# cut -c 2-4 passwd
oot
in:
aem
dm:
p:x
ync
hut
alt
ail
per
ame
est
oot
est
oot
EST
----------------------------------------------------------------------------------
sort 命令
• sort 用于排序文本数据。该数据可以位于文件中或其他命令输出中。 Sort 通常与管道一起使用
-n ##按数值而非字符排序
-k ##设置排序字段
-t ##指定其他字段分隔符(默认为空格)
uniq 命令
•
uniq“ 删除 ” 文件中重复的相邻行。若要只打印文件中出现的唯一行(“ 删除 ” 所有重复行 ), 必须首先对 uniq 的输入进行排序。由于可以为uniq 指定其决策所基于的字段或列 , 因此这些字段或列是对其输入进行排序所必须的字段或列。如果未与选项一起使用 , uniq 会使用整个记录作为决策键 , 删除其输入中的重复行
-u ##仅显示唯一行
-d ##显示重复行
-c ##每行显示一次(包括出现计数)
1 vim file
2 cat file
3 sort file ##按字符排序
4 sort -n file ##按数字大小排序
5 sort -rn file ##倒序按数字排序
6 sort -rnu file ##-u 去掉重复值
7 sort -rn file |uniq -c ##倒序按数字排序,且每行只显示一次(包括出现计数)
8 sort -rn file | uniq -d ##显示出重复的行,不显示不重复的行
9 sort -rn file | uniq -u ##仅显示唯一行
10 vim file
11 cat file
12 sort file
13 sort -n file
14 sort -t : -k 3 -n file ##设置排序字段间隔符为:字段为3
15 sort -t : -k 3 -n file |uniq -c
16 history
------以%MEM排序,显示前5个进程的PID---------------------------------------------------
[root@localhost mnt]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 52840 6500 ? Ss 11:53 0:03 /usr/lib/systemd/systemd --switch
root 2 0.0 0.0 0 0 ? S 11:53 0:00 [kthreadd]
root 17 0.0 0.0 0 0 ? S< 11:53 0:00 [kintegrityd]
root 618 0.0 0.0 16748 992 ? SNs 11:53 0:00 /usr/sbin/alsactl -s -n 19 -c -E
root 620 0.0 1.1 329432 21008 ? Ssl 11:53 0:00 /usr/bin/python -Es /usr/sbin/fir
avahi 623 0.0 0.0 30028 1484 ? Ss 11:53 0:00 avahi-daemon: running [linux.loca
root 624 0.0 0.8 550008 15996 ? Ssl 11:53 0:00 /usr/bin/python -Es /usr/sbin/tun
root 629 0.0 0.2 283832 4432 ? Ssl 11:53 0:00 /usr/sbin/rsyslogd -n
root 635 0.0 0.2 209780 4368 ? Ss 11:53 0:00 /usr/bin/abrt-watch-log -F BUG: W
libstor+ 639 0.0 0.0 4336 364 ? Ss 11:53 0:00 /usr/bin/lsmd -d
root 32294 0.0 0.0 0 0 ? R 17:53 0:00 [kworker/0:3]
root 32370 0.0 0.0 107892 364 ? S 17:57 0:00 sleep 60
root 32379 0.0 0.0 123356 1320 pts/0 R+ 17:57 0:00 ps aux
[root@localhost mnt]# ps ax -o pid --sort -%mem |head -n 5
PID
2010
1947
737
2066
[root@localhost mnt]# ps ax -o pid --sort -%mem |grep PID -v |head -n 5
2010
1947
737
2066
620
---------------------------------------------------------------------------------
[root@localhost mnt]# vim file
[root@localhost mnt]# cat file
1
3
5
7
9
2
4
6
8
16
11
34
56
12
1
3
5
25
22
[root@localhost mnt]# sort file
1
1
11
12
16
2
22
25
3
3
34
4
5
5
56
6
7
8
9
[root@localhost mnt]# sort -n file
1
1
2
3
3
4
5
5
6
7
8
9
11
12
16
22
25
34
56
[root@localhost mnt]# sort -rn file
56
34
25
22
16
12
11
9
8
7
6
5
5
4
3
3
2
1
1
[root@localhost mnt]# sort -rnu file
56
34
25
22
16
12
11
9
8
7
6
5
4
3
2
1
[root@localhost mnt]# sort -rn file |uniq -c
1 56
1 34
1 25
1 22
1 16
1 12
1 11
1 9
1 8
1 7
1 6
2 5
1 4
2 3
1 2
2 1
[root@localhost mnt]# sort -rn file | uniq -d
5
3
1
[root@localhost mnt]# sort -rn file | uniq -u
56
34
25
22
16
12
11
9
8
7
6
4
2
[root@localhost mnt]# vim file
[root@localhost mnt]# cat file
44:a:1
35:a:3
2:a:5
23:a:7
5:a:9
7:a:2
5:a:4
1:a:6
9:a:8
3:a:16
3:a:11
3:a:34
3:a:56
3:a:12
23:a:1
8:a:3
6:a:5
99:a:25
8:a:22
[root@localhost mnt]# sort file
1:a:6
23:a:1
23:a:7
2:a:5
35:a:3
3:a:11
3:a:12
3:a:16
3:a:34
3:a:56
44:a:1
5:a:4
5:a:9
6:a:5
7:a:2
8:a:22
8:a:3
99:a:25
9:a:8
[root@localhost mnt]# sort -n file
1:a:6
2:a:5
3:a:11
3:a:12
3:a:16
3:a:34
3:a:56
5:a:4
5:a:9
6:a:5
7:a:2
8:a:22
8:a:3
9:a:8
23:a:1
23:a:7
35:a:3
44:a:1
99:a:25
[root@localhost mnt]# sort -t : -k 3 -n file
23:a:1
44:a:1
7:a:2
35:a:3
8:a:3
5:a:4
2:a:5
6:a:5
1:a:6
23:a:7
9:a:8
5:a:9
3:a:11
3:a:12
3:a:16
8:a:22
99:a:25
3:a:34
3:a:56
[root@localhost mnt]# sort -t : -k 3 -n file |uniq -c
1 23:a:1
1 44:a:1
1 7:a:2
1 35:a:3
1 8:a:3
1 5:a:4
1 2:a:5
1 6:a:5
1 1:a:6
1 23:a:7
1 9:a:8
1 5:a:9
1 3:a:11
1 3:a:12
1 3:a:16
1 8:a:22
1 99:a:25
1 3:a:34
1 3:a:56
--------------------------------------------------------------------------------
tr 命令
• tr 用于转字符 : 即 , 如果给定了两个字符范围 , 则只要发现某个字符位于第一个范围中 , 就会将其转换为第二个范围中对等的字符。该命令通常在 shell 脚本中使用 , 以按预期情况转换数据
• tr 'A-Z' 'a-z' <file
1 vim file
2 cat file
3 tr 'a-z' 'A-Z' < file
4 tr 'A-Z' 'a-z' < file
5 history
-------------------------------------------------------------
[root@localhost mnt]# vim file
[root@localhost mnt]# cat file
westos
WESTOS
[root@localhost mnt]# tr 'a-z' 'A-Z' < file
WESTOS
WESTOS
[root@localhost mnt]# tr 'A-Z' 'a-z' < file
westos
westos
[root@localhost mnt]# history
-------------------------------------------------------------
sed 命令
• sed 命令是流编辑器 , 用于对文本数据流执行编辑。假定要处理一个文件名 , sed 将对文件中的所有行执行搜索和替换 , 以将修改后的数据发送到标准输出 ; 即 , 其实际上并不修改现有文件。与 grep 一样 , sed 通常在管道中使用。
• 由于 sed 命令通常包含可以解释为 shell 元字符的字符 ,因此请按下面示例所示引用 sed 命令。默认情况下 , sed对文件中的所有行执行操作。在提供 sed 时 , 可带有地址。
s/old/new ##执行字符串转换,将old替换为new
d ##删除匹配的行
---------------------------------------------------------------------------
错误的添加用户方式:(这只是输出了,但是并没有添加用户)
[root@localhost mnt]# /mnt/file.sh 5
2
3
6
7
1
[root@localhost mnt]# cat file.sh
#!/bin/bash
for NUM in $(seq 1 $1)
do
sed -n ${NUM}p /mnt/userfile
done
[root@localhost mnt]# cat userfile
2
3
6
7
1
8
5
4
9
[root@localhost mnt]#
改正后:
[root@localhost mnt]# /mnt/file.sh 2
[root@localhost mnt]# id user1
uid=1001(user1) gid=1001(user1) groups=1001(user1)
[root@localhost mnt]# cat file.sh
#!/bin/bash
for NUM in $(seq 1 $1)
do
USERNAME=`sed -n ${NUM}p /mnt/userfile`
useradd $USERNAME
done
[root@localhost mnt]# id user3
id: user3: no such user
[root@localhost mnt]# cat userfile
user1
user2
user3
[root@localhost mnt]#
可以自动识别用户行数的
[root@localhost mnt]# vim file.sh
[root@localhost mnt]# mv /mnt/file.sh /bin/
[root@localhost mnt]# file.sh userfile
useradd: user 'user1' already exists
useradd: user 'user2' already exists
[root@localhost mnt]# id user3
uid=1003(user3) gid=1003(user3) groups=1003(user3)
[root@localhost mnt]# cat /bin/file.sh
#!/bin/bash
MAX_LINE=`wc -l $1 |awk -F " " '{print $1}'`
for NUM in `seq 1 $MAX_LINE`
do
USERNAME=`sed -n ${NUM}p $1`
useradd $USERNAME
done
[root@localhost mnt]#
不太完善的加密码的
[root@localhost mnt]# vim passwd
[root@localhost mnt]# vim passwd
[root@localhost mnt]# vim userfile
[root@localhost mnt]# vim /bin/file.sh
[root@localhost mnt]# file.sh userfile passwd
useradd: user 'user1' already exists
Changing password for user user1.
passwd: all authentication tokens updated successfully.
useradd: user 'user2' already exists
Changing password for user user2.
passwd: all authentication tokens updated successfully.
useradd: user 'user3' already exists
Changing password for user user3.
passwd: all authentication tokens updated successfully.
Changing password for user westos1.
passwd: all authentication tokens updated successfully.
Changing password for user westos2.
passwd: all authentication tokens updated successfully.
Changing password for user westos3.
passwd: all authentication tokens updated successfully.
[root@localhost mnt]# su - student
[student@localhost ~]$ passwd user1
passwd: Only root can specify a user name.
[student@localhost ~]$ exit
logout
[root@localhost mnt]# cat /bin/file.sh
#!/bin/bash
MAX_LINE=`wc -l $1 |awk -F " " '{print $1}'`
for NUM in `seq 1 $MAX_LINE`
do
USERNAME=`sed -n ${NUM}p $1`
PASSWD=$(sed -n ${NUM}p $2)
useradd $USERNAME >/dev/null
echo $PASSWD | passwd --stdin $USERNAME
done
[root@localhost mnt]#
-------------------------------------------------------------------------