Linux系统命令从入门到完全忘记(六)

1,560次阅读
没有评论

Linux系统命令从入门到完全忘记(六)

Linux三剑客

awk

概要: 三剑客老大,擅长取列

语法格式: awk ‘ pattern {action} ‘ 参数:

参数 说明
print 打印
-F 指定分隔符’[分隔符1分隔符2]’
NR 行号
$0,$1,$NF $0整行,$1第一列,$NF最后一行
+ 多个分隔符看作为一个

使用示例:

  • 打印出/etc/passwd文件中第一列所有的用户名
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
<span class="line">8</span>
<span class="line">9</span>
<span class="line">10</span>
<span class="line">11</span>
<span class="line">12</span>
<span class="line">13</span>
<span class="line">14</span>
<span class="line">15</span>
<span class="line">16</span>
<span class="line">17</span>
<span class="line">18</span>
<span class="line">19</span>
<span class="line">20</span>
<span class="line">21</span>
<span class="line">22</span>
<span class="line">23</span>
<span class="line">24</span>
<span class="line">25</span>
<span class="line">[root@ansheng ~]<span class="comment"># awk –F ” :”  '{print $1}' /etc/passwd</span></span>
<span class="line">root</span>
<span class="line">bin</span>
<span class="line">daemon</span>
<span class="line">adm</span>
<span class="line">lp</span>
<span class="line">sync</span>
<span class="line">shutdown</span>
<span class="line">halt</span>
<span class="line">mail</span>
<span class="line">uucp</span>
<span class="line">operator</span>
<span class="line">games</span>
<span class="line">gopher</span>
<span class="line">ftp</span>
<span class="line">nobody</span>
<span class="line">dbus</span>
<span class="line">vcsa</span>
<span class="line">abrt</span>
<span class="line">haldaemon</span>
<span class="line">ntp</span>
<span class="line">saslauth</span>
<span class="line">postfix</span>
<span class="line">sshd</span>
<span class="line">tcpdump</span>

sed

概要: 文本处理工具,取行、替换、删除、新增、选取,字符的过滤

语法格式: sed [-nefri] “command” 输入文本

参数:

参数 说明
-d 删除
-p 打印
-i 改变文件内容
s 编辑替换
-n 取消默认输出
g 全部替换

使用示例:

  • 打印出当前目录下的linux文件中的内容第3行
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
<span class="line">[root@ansheng ~]<span class="comment"># cat -n linux </span></span>
<span class="line">     1	linux</span>
<span class="line">     2	centos</span>
<span class="line">     3	miniux</span>
<span class="line">     4	nuix</span>
<span class="line">[root@ansheng ~]<span class="comment"># sed -n '3p' linux </span></span>
<span class="line">miniux</span>
  • 显示/etc/passwd文件的第一行到第五行
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
<span class="line">[root@ansheng ~]<span class="comment"># sed -n '5,10p' /etc/passwd</span></span>
<span class="line">lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin</span>
<span class="line">sync:x:5:0:sync:/sbin:/bin/sync</span>
<span class="line">shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown</span>
<span class="line">halt:x:7:0:halt:/sbin:/sbin/halt</span>
<span class="line">mail:x:8:12:mail:/var/spool/mail:/sbin/nologin</span>
<span class="line">uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin</span>
  • 删除当前目录下的file文件内容2到5行
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
8
9
10
11
12
13
14
15
16
17
18
[root@ansheng ~]# cat file 
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
8
9
10
[root@ansheng ~]# sed '2,5d' file 
<span class="line">1</span>
6
7
8
9
10
<span class="line">1</span>
<span class="line">sed <span class="string">'/My/,/You/d'</span> datafile</span>

删除包含”My”的行到包含”You”的行之间的行

<span class="line">1</span>
<span class="line">sed <span class="string">'/My/,10d'</span> datafile</span>

删除包含”My”的行到第十行的内容

  • 将当前目录下的linux文件中包含ansheng的内容全部替换为redhat
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
8
9
10
11
12
13
<span class="line">[root@ansheng ~]<span class="comment"># cat linux </span></span>
<span class="line">linux</span>
<span class="line">centos</span>
<span class="line">miniux</span>
<span class="line">nuix</span>
<span class="line">ansheng</span>
<span class="line">[root@ansheng ~]<span class="comment"># sed -i 's#ansheng#redhat#g' linux </span></span>
<span class="line">[root@ansheng ~]<span class="comment"># cat linux </span></span>
<span class="line">linux</span>
<span class="line">centos</span>
<span class="line">miniux</span>
<span class="line">nuix</span>
<span class="line">redhat</span>

grep

概要: 过滤指定字符串

语法格式: grep [OPTIONS] PATTERN [FILE…] grep [OPTIONS] [-e PATTERN | -f FILE] [FILE…]

参数:

参数 说明
-i 不区分大小写
-v 排除指定字符串
-E 过滤多个字符串
-n 显示字符串所在文件中的行
-o 只输出匹配出来的字符串
-A N 匹配内容的后N行
-B N 匹配内容的前N行
-C N 匹配内容的前后N行
–color=auto 过滤出来的字符串以颜色显示

元字符

符号 说明
. 匹配任意单个字符
[] 匹配指定范围内的任意单个字符
[^] 匹配指定范围内的任意单个字符

字符集合

符号 说明
[[:digit:]] 数字
[[:lower:]] 小写字母
[[:upper:]] 大写字母
[[:punct:]] 标点符号
[[:space:]] 空白字符
[[:alpha:]] 所有字母
[[:alnum:]] 所有数字和字母

匹配次数(贪婪模式,尽可能长的去匹配):

符号 说明
* 匹配其前面的字符任意次
.* 任意长度的任意字符
匹配其前面的字符一次或0次
{m,n} 匹配其前面的字符至少m次,至多n次

位置锚定:

符号 说明
^ 锚定行首,此字符后面的任意内容必须出现在行首
$ 锚定行尾,此字符后面的任意内容必须出现在行尾
^$ 空白行
< (\b)其后面的任意字符必须作为单词的首部出现
> (\b)其前面的任意字符必须作为单词的尾部出现

分组:

符号 说明
() 分组
a b or,或者

使用示例:

  • 过滤出当前目录下面的ett文件中包含”ansheng”的字符串,并且显示该字符串所在行。
<span class="line">1</span>
2
3
4
5
6
<span class="line">[root@anshenglinux ~]<span class="comment"># cat -n ett </span></span>
<span class="line">     1	linux</span>
<span class="line">     2	ansheng</span>
<span class="line">     3	Linux</span>
<span class="line">[root@anshenglinux ~]<span class="comment"># grep -n "ansheng" ett </span></span>
<span class="line">2:ansheng</span>
  • 过滤出当前目录下的”linux”文件中包含LINUX和linux的字符串

方法1:

<span class="line">1</span>
2
3
<span class="line">[root@anshenglinux ~]<span class="comment"># grep -E "linux|LINUX" ett </span></span>
<span class="line">linux</span>
<span class="line">LINUX</span>

方法2:

<span class="line">1</span>
2
3
<span class="line">[root@anshenglinux ~]<span class="comment"># grep -i "linux" ett </span></span>
<span class="line">linux</span>
<span class="line">LINUX</span>
  • 排除当前目录下包含linux文件中包含“ansheng”的字符串
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
<span class="line">[root@anshenglinux ~]<span class="comment"># cat ett </span></span>
<span class="line">linux</span>
<span class="line">ansheng</span>
<span class="line">LINUX</span>
<span class="line">[root@anshenglinux ~]<span class="comment"># grep -v "ansheng" ett </span></span>
<span class="line">linux</span>
<span class="line">LINUX</span>

其它命令

xargs

概要: 取前一个命令的输出作为另一个命令的参数

语法格式: | xargs Command file

使用示例:

  • 删除/tmp下面的所有后缀为.sh的文件
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
<span class="line">[root@linux tmp]<span class="comment"># pwd </span></span>
<span class="line">/tmp</span>
<span class="line">[root@linux tmp]<span class="comment"># ls</span></span>
<span class="line">a.sh  b.sh  c  d</span>
<span class="line">[root@linux tmp]<span class="comment"># find ./ -type f -name "*.sh"|xargs rm -f</span></span>
<span class="line">[root@linux tmp]<span class="comment"># ls</span></span>
<span class="line">c  d</span>

vi/vim

概要: 文本编辑器

语法格式: vim file

参数:

命令模式:

参数 说明
i 在光标所在字符前插入
I 在光标所在字符后插入
a 在光标所在字符后插入
A 在光标所在行尾插入
o 在光标下插入新行
O 在光标上插入新行
gg 到第一行
GG 到最后一行
nG 到第n行
0 移动到行首
$ 移动到行尾
x 删除光标所在处字符
nx 删除光标所在处后面N个字符
dd 删除或剪切光标所在行
ddd 删除或剪切光标下N行
D 删除光标所在出至行尾的内容
yy 复制当前行
nyy 复制当前行以下n行
P/p 粘贴光标所在行下或行上
r 替换光标所在处字符
R 从光标所在处开始替换字符,按Esc结束
u 取消上一步操作
ZZ 保存修改退出

编辑模式:

参数 说明
set nu 显示行号
set nonu 取消行号
w 保存
q 退出
强制
n 到第N行
n1,n2d 删除指定范围的行
/string 搜索指定字符串,搜索时忽略大小写:set ic,取消:set noic,n搜索指定字符串下一个出现的位置
%s/源字符串/替换后的字符串/g 全文替换s,g不询问是否替换
n1,n2s/源字符串/替换后的字符串/g 指定范围内替换
r 文件/命令 把文件内容或者命令执行结果导入到当前光标所在处后面
! 命令 查找命令所在路径

alias

概要: 查看或设置别名

语法格式: alias[别名]=[指令]

使用示例:

  • 查看当前已设置的别名
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
8
<span class="line">[root@ansheng ~]<span class="comment"># alias </span></span>
<span class="line"><span class="built_in">alias</span> cp=<span class="string">'cp -i'</span></span>
<span class="line"><span class="built_in">alias</span> l.=<span class="string">'ls -d .* --color=auto'</span></span>
<span class="line"><span class="built_in">alias</span> ll=<span class="string">'ls -l --color=auto'</span></span>
<span class="line"><span class="built_in">alias</span> ls=<span class="string">'ls --color=auto'</span></span>
<span class="line"><span class="built_in">alias</span> mv=<span class="string">'mv -i'</span></span>
<span class="line"><span class="built_in">alias</span> rm=<span class="string">'rm -i'</span></span>
<span class="line"><span class="built_in">alias</span> <span class="built_in">which</span>=<span class="string">'alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'</span></span>
  • 请使用别名功能设置执行ansheng命令就输出“ansheng ansheng”
<span class="line">1</span>
2
3
<span class="line">[root@ansheng ~]<span class="comment"># alias ansheng="echo 'ansheng ansheng'"</span></span>
<span class="line">[root@ansheng ~]<span class="comment"># ansheng</span></span>
<span class="line">ansheng ansheng</span>

高级应用:

  • 请设置grep命令过滤出来的字符串都显示有颜色,并且在系统下次重启之后也生效
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
8
9
10
11
12
[root@ansheng ~]# cat ett 
<span class="line">1</span>
2
3
4
5
[root@ansheng ~]# grep "3" ett 
3
[root@ansheng ~]# echo "alias grep='grep --color=auto'" >> /etc/bashrc   
[root@ansheng ~]# source /etc/bashrc 
[root@ansheng ~]# grep "3" ett 
3

/etc/bashrc是全局变量,用户变量请添加到自己家目录下面的“.bashrc”, 有些自己定义的别名,就可以放在自己家目录的.bashrc里,因为个人爱好问题,有些人不需要

unalias

概要: 取消别名

语法格式: unalias 别名

使用示例:

  • 删除已设置的别名ansheng
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@ansheng ~]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias ansheng='echo wocao'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@ansheng ~]# unalias ansheng
<span class="line">[root@ansheng ~]<span class="comment"># alias </span></span>
<span class="line"><span class="built_in">alias</span> cp=<span class="string">'cp -i'</span></span>
<span class="line"><span class="built_in">alias</span> l.=<span class="string">'ls -d .* --color=auto'</span></span>
<span class="line"><span class="built_in">alias</span> ll=<span class="string">'ls -l --color=auto'</span></span>
<span class="line"><span class="built_in">alias</span> ls=<span class="string">'ls --color=auto'</span></span>
<span class="line"><span class="built_in">alias</span> mv=<span class="string">'mv -i'</span></span>
<span class="line"><span class="built_in">alias</span> rm=<span class="string">'rm -i'</span></span>
<span class="line"><span class="built_in">alias</span> <span class="built_in">which</span>=<span class="string">'alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'</span></span>
[root@ansheng ~]# ansheng
-bash: ansheng: command not found

yum

概要: 安装或卸载软件

语法格式: yum [参数] [软件名]

参数:

参数 概述
install 安装
remove 卸载
groupinstall 安装软件组
-y 不提示
update 升级全部软件或升级指定软件

使用示例:

  • 安装dhcp服务
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
<span class="line">8</span>
<span class="line">9</span>
<span class="line">10</span>
<span class="line">11</span>
<span class="line">12</span>
<span class="line">13</span>
<span class="line">14</span>
<span class="line">15</span>
<span class="line">16</span>
<span class="line">17</span>
<span class="line">18</span>
<span class="line">19</span>
<span class="line">20</span>
<span class="line">21</span>
<span class="line">22</span>
<span class="line">23</span>
<span class="line">24</span>
<span class="line">25</span>
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<span class="line">[root@ansheng ~]<span class="comment"># yum -y install dhcp</span></span>
<span class="line">Loaded plugins: fastestmirror, security</span>
<span class="line">Setting up Install Process</span>
<span class="line">Loading mirror speeds from cached hostfile</span>
<span class="line"> * base: mirrors.aliyun.com</span>
<span class="line"> * extras: mirrors.aliyun.com</span>
<span class="line"> * updates: mirrors.aliyun.com</span>
<span class="line">Resolving Dependencies</span>
<span class="line">--> Running transaction check</span>
<span class="line">---> Package dhcp.x86_64 12:4.1.1-43.P1.el6.centos will be installed</span>
<span class="line">--> Processing Dependency: portreserve <span class="keyword">for</span> package: 12:dhcp-4.1.1-43.P1.el6.centos.x86_64</span>
<span class="line">--> Running transaction check</span>
<span class="line">---> Package portreserve.x86_64 0:0.0.4-9.el6 will be installed</span>
<span class="line">--> Finished Dependency Resolution</span>

<span class="line">Dependencies Resolved</span>

<span class="line">=======================================================================================</span>
<span class="line"> Package            Arch          Version                            Repository   Size</span>
<span class="line">=======================================================================================</span>
<span class="line">Installing:</span>
<span class="line"> dhcp               x86_64        12:4.1.1-43.P1.el6.centos          base        819 k</span>
<span class="line">Installing <span class="keyword">for</span> dependencies:</span>
<span class="line"> portreserve        x86_64        0.0.4-9.el6                        base         23 k</span>

<span class="line">Transaction Summary</span>
<span class="line">=======================================================================================</span>
<span class="line">Install       2 Package(s)</span>

<span class="line">Total download size: 842 k</span>
<span class="line">Installed size: 1.9 M</span>
<span class="line">Downloading Packages:</span>
<span class="line">(1/2): dhcp-4.1.1-43.P1.el6.centos.x86_64.rpm                   | 819 kB     00:00     </span>
<span class="line">(2/2): portreserve-0.0.4-9.el6.x86_64.rpm                       |  23 kB     00:00     </span>
<span class="line">---------------------------------------------------------------------------------------</span>
<span class="line">Total                                                  1.1 MB/s | 842 kB     00:00     </span>
<span class="line">Running rpm_check_debug</span>
<span class="line">Running Transaction Test</span>
<span class="line">Transaction Test Succeeded</span>
<span class="line">Running Transaction</span>
<span class="line">  Installing : portreserve-0.0.4-9.el6.x86_64                                      1/2 </span>
<span class="line">  Installing : 12:dhcp-4.1.1-43.P1.el6.centos.x86_64                               2/2 </span>
<span class="line">  Verifying  : 12:dhcp-4.1.1-43.P1.el6.centos.x86_64                               1/2 </span>
<span class="line">  Verifying  : portreserve-0.0.4-9.el6.x86_64                                      2/2 </span>

<span class="line">Installed:</span>
<span class="line">  dhcp.x86_64 12:4.1.1-43.P1.el6.centos                                                </span>

<span class="line">Dependency Installed:</span>
<span class="line">  portreserve.x86_64 0:0.0.4-9.el6                                                     </span>

<span class="line">Complete!</span>
  • 卸载dhcp服务
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
<span class="line">8</span>
<span class="line">9</span>
<span class="line">10</span>
<span class="line">11</span>
<span class="line">12</span>
<span class="line">13</span>
<span class="line">14</span>
<span class="line">15</span>
<span class="line">16</span>
<span class="line">17</span>
<span class="line">18</span>
<span class="line">19</span>
<span class="line">20</span>
<span class="line">21</span>
<span class="line">22</span>
<span class="line">23</span>
<span class="line">24</span>
<span class="line">25</span>
26
27
28
29
30
31
32
33
34
35
36
37
<span class="line">[root@ansheng ~]<span class="comment"># rpm -q dhcp</span></span>
<span class="line">dhcp-4.1.1-43.P1.el6.centos.x86_64</span>
<span class="line">[root@ansheng ~]<span class="comment"># yum -y remove dhcp</span></span>
<span class="line">Loaded plugins: fastestmirror, security</span>
<span class="line">Setting up Remove Process</span>
<span class="line">Resolving Dependencies</span>
<span class="line">--> Running transaction check</span>
<span class="line">---> Package dhcp.x86_64 12:4.1.1-43.P1.el6.centos will be erased</span>
<span class="line">--> Finished Dependency Resolution</span>

<span class="line">Dependencies Resolved</span>

<span class="line">=======================================================================================</span>
<span class="line"> Package       Arch            Version                            Repository      Size</span>
<span class="line">=======================================================================================</span>
<span class="line">Removing:</span>
<span class="line"> dhcp          x86_64          12:4.1.1-43.P1.el6.centos          @base          1.9 M</span>

<span class="line">Transaction Summary</span>
<span class="line">=======================================================================================</span>
<span class="line">Remove        1 Package(s)</span>

<span class="line">Installed size: 1.9 M</span>
<span class="line">Downloading Packages:</span>
<span class="line">Running rpm_check_debug</span>
<span class="line">Running Transaction Test</span>
<span class="line">Transaction Test Succeeded</span>
<span class="line">Running Transaction</span>
<span class="line">  Erasing    : 12:dhcp-4.1.1-43.P1.el6.centos.x86_64                               1/1 </span>
<span class="line">  Verifying  : 12:dhcp-4.1.1-43.P1.el6.centos.x86_64                               1/1 </span>

<span class="line">Removed:</span>
<span class="line">  dhcp.x86_64 12:4.1.1-43.P1.el6.centos                                                </span>

<span class="line">Complete!</span>
<span class="line">[root@ansheng ~]<span class="comment"># rpm -q dhcp</span></span>
<span class="line">package dhcp is not installed</span>

rpm

概要: 安装.RPM结尾的软件

语法格式: rpm [参数] [文件]

参数:

参数 说明
-i 安装软件
-U 升级软件
-v 显示过程
-q 查询安装的软件信息
-l 显示安装文件所在路径
–nodeps 忽略软件包的依赖关系强行安装
–force 忽略软件包及文件的冲突

使用示例:

  • 查看已安装的软件”http”的安装版本信息
<span class="line">1</span>
2
<span class="line">[root@ansheng ~]<span class="comment"># rpm -q httpd</span></span>
<span class="line">httpd-2.2.15-39.el6.centos.x86_64</span>

locate

概要: 根据locate数据库查找文件或目录,updatedb更新库

语法格式: locate 文件或目录名

使用示例:

  • 查找文件名为“ifcfg-eth0”的文件
<span class="line">1</span>
2
<span class="line">[root@ansheng ~]<span class="comment"># locate  ifcfg-eth0 </span></span>
<span class="line">/etc/sysconfig/network-scripts/ifcfg-eth0</span>

runlevel

概要: 查看当前操作系统的运行级别,init切换运行级别

语法格式: runlebel

使用示例:

  • 查看当前操作系统的运行级别
<span class="line">1</span>
2
<span class="line">[root@ansheng ~]<span class="comment"># runlevel </span></span>
<span class="line">N 3</span>
admin
版权声明:本站原创文章,由admin2016-07-12发表,共计7729字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)