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

2,750次阅读
没有评论

共计 7729 个字符,预计需要花费 20 分钟才能阅读完成。

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

Linux三剑客

awk

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

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

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

使用示例:

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

sed

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

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

参数:

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

使用示例:

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

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

1
sed '/My/,10d' datafile

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

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

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

方法1:

1
2
3
[root@anshenglinux ~]# grep -E "linux|LINUX" ett 
linux
LINUX

方法2:

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

其它命令

xargs

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

语法格式:
| xargs Command file

使用示例:

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

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[别名]=[指令]

使用示例:

  • 查看当前已设置的别名
1
2
3
4
5
6
7
8
[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 rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
  • 请使用别名功能设置执行ansheng命令就输出“ansheng ansheng”
1
2
3
[root@ansheng ~]# alias ansheng="echo 'ansheng ansheng'"
[root@ansheng ~]# ansheng
ansheng ansheng

高级应用:

  • 请设置grep命令过滤出来的字符串都显示有颜色,并且在系统下次重启之后也生效
1
2
3
4
5
6
7
8
9
10
11
12
[root@ansheng ~]# cat ett 
1
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
1
2
3
4
5
6
7
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
[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 rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@ansheng ~]# ansheng
-bash: ansheng: command not found

yum

概要:
安装或卸载软件

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

参数:

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

使用示例:

  • 安装dhcp服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
[root@ansheng ~]# yum -y install dhcp
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package dhcp.x86_64 12:4.1.1-43.P1.el6.centos will be installed
--> Processing Dependency: portreserve for package: 12:dhcp-4.1.1-43.P1.el6.centos.x86_64
--> Running transaction check
---> Package portreserve.x86_64 0:0.0.4-9.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================
 Package            Arch          Version                            Repository   Size
=======================================================================================
Installing:
 dhcp               x86_64        12:4.1.1-43.P1.el6.centos          base        819 k
Installing for dependencies:
 portreserve        x86_64        0.0.4-9.el6                        base         23 k

Transaction Summary
=======================================================================================
Install       2 Package(s)

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

Installed:
  dhcp.x86_64 12:4.1.1-43.P1.el6.centos                                                

Dependency Installed:
  portreserve.x86_64 0:0.0.4-9.el6                                                     

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

Dependencies Resolved

=======================================================================================
 Package       Arch            Version                            Repository      Size
=======================================================================================
Removing:
 dhcp          x86_64          12:4.1.1-43.P1.el6.centos          @base          1.9 M

Transaction Summary
=======================================================================================
Remove        1 Package(s)

Installed size: 1.9 M
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Erasing    : 12:dhcp-4.1.1-43.P1.el6.centos.x86_64                               1/1 
  Verifying  : 12:dhcp-4.1.1-43.P1.el6.centos.x86_64                               1/1 

Removed:
  dhcp.x86_64 12:4.1.1-43.P1.el6.centos                                                

Complete!
[root@ansheng ~]# rpm -q dhcp
package dhcp is not installed

rpm

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

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

参数:

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

使用示例:

  • 查看已安装的软件”http”的安装版本信息
1
2
[root@ansheng ~]# rpm -q httpd
httpd-2.2.15-39.el6.centos.x86_64

locate

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

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

使用示例:

  • 查找文件名为“ifcfg-eth0”的文件
1
2
[root@ansheng ~]# locate  ifcfg-eth0 
/etc/sysconfig/network-scripts/ifcfg-eth0

runlevel

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

语法格式:
runlebel

使用示例:

  • 查看当前操作系统的运行级别
1
2
[root@ansheng ~]# runlevel 
N 3
正文完
请博主喝杯咖啡吧!
post-qrcode
 
admin
版权声明:本站原创文章,由 admin 2016-07-12发表,共计7729字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码