linux下的小工具

一.bash的基本特性

1.常见快捷键
ctrl+c 终止前台运行的程序
ctrl+z 将前台运行的程序挂起到后台
ctrl+d 退出 等价exit,logout
ctrl+l 清屏
ctrl+a |home 光标移到命令行的最前端
ctrl+e |end 光标移到命令行的后端
ctrl+u 删除光标前所有字符
ctrl+k 删除光标后所有字符
ctrl+r 搜索历史命令

2.常用通配符

*匹配0个或任意多个字符

?匹配单个字符

[]匹配中括号中任意单个字符

{}匹配大括号中任意字符串

[root@yunwei1 tmp]# ls
1.txt 3.txt file1 file11.jpg file13.jpg file2 file3 file4.jpg file6.jpg file8.jpg inittab vsftpd.conf
2.txt 4.txt file10.jpg file12.jpg file1.jpg file2.jpg file3.jpg file5.jpg file7.jpg file9.jpg passwd
[root@yunwei1 tmp]# rm -rf [1-4]*
[root@yunwei1 tmp]# ls
file1 file11.jpg file13.jpg file2 file3 file4.jpg file6.jpg file8.jpg inittab vsftpd.conf
file10.jpg file12.jpg file1.jpg file2.jpg file3.jpg file5.jpg file7.jpg file9.jpg passwd

3.bash中的引号

双引号"":可以将引号内的内容看成一个整体,也可以引用变量

单引号'':可以将引号内的内容看成一个整体,但是不可以引用变量

反撇号`:作用和$()相同,引号或括号里面的内容会优先执行,如果有嵌套,反撇号不能用

二.常用小工具

1.行过滤工具grep

(一)语法格式

语法格式
grep 选项 关键字 文件名
常用选项
-i 不区分大小写
-v 查找不包含指定内容的行,反向选择
-A 显示匹配行及其后面多少行,如-A5
-B 显示匹配行及其前面多少行,如-B4
-C 显示匹配行及其前后多少行,如-C3
-n 显示行号
-w 按单词搜索
-o 打印匹配关键字
^关键字 显示以关键字开头的行
关键字$ 显示以关键字结尾的行
^$ 匹配空行
--color=auto:可以将匹配行上的关键字加上颜色

-c 统计匹配到的次数(行数)
-r 逐层遍历目录查找 文件名可以是目录
-l 列出匹配的文件名
-L 列出不匹配的文件名
-e 使用正则表达式
-E 使用扩展正则匹配

案例如下

[root@yunwei1 tmp]# grep root test 
root:x:0:0:root:/root:/bin/bash
[root@yunwei1 tmp]# grep -c root test 
1
[root@yunwei1 tmp]# grep -l root test 
test
[root@yunwei1 tmp]# grep -L aaad test
test
[root@yunwei1 tmp]# grep -L root test

(二).别名定义

临时定义
alias grep='grep --color=auto'

全局配置
在配置文件/etc/bashrc文件最后添加alias grep='grep --color=auto'

2.列截取工具cut

语法格式
cut 选项 文件名

-d 以分隔符进行分隔,默认为制表符,\t
-f 分隔后截取第几列,
-c 截取第几个字符, 1-5表示第1到第5个字符; 1,3表示第1,3个字符

案例

[root@yunwei1 tmp]# grep -v "^#" inittab |cut -d ':' -f 2
3
[root@yunwei1 tmp]#
[root@yunwei1 tmp]# grep -v "^#" inittab |cut -c 1,4 第1,第四个字符
i3
[root@yunwei1 tmp]# grep -v "^#" inittab |cut -c 1-4 1到4个字符
id:3
[root@yunwei1 tmp]#

3.排序工具sort

sort是将每一行作为一个单位,从首字符向后,依次按ASII码值进行比较,最后将他们升序输出
语法格式
sort 选项 文件
-n 按数字排序输出
-u 去除重复行
-r 降序排列,默认是升序
-o 将排序结果输出到指定文件中

-t 指定分隔符
-k 第几列
-b 忽略前导空格
-R 随机排序,每次运行结果排序都不同

sort 文件 默认以ASII排序输出


案例如下

[root@yunwei1 tmp]# sort test 
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shelluser:x:503:504::/home/shelluser:/bin/bash
user02:x:501:501::/home/user02:/bin/bash
user03:x:502:502::/home/user03:/bin/bash
[root@yunwei1 tmp]# 
[root@yunwei1 tmp]# sort -r test 
user03:x:502:502::/home/user03:/bin/bash
user02:x:501:501::/home/user02:/bin/bash
shelluser:x:503:504::/home/shelluser:/bin/bash
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@yunwei1 tmp]# 
[root@yunwei1 tmp]# sort -n -t: -k 3 test 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
user02:x:501:501::/home/user02:/bin/bash
user03:x:502:502::/home/user03:/bin/bash
shelluser:x:503:504::/home/shelluser:/bin/bash
[root@yunwei1 tmp]#

4.去重工具uniq

用于去除连续重复行
语法格式
uniq 选项 文件
-c 统计重复次数
-i 忽略大小写
-d 只显示重复行

sort file 去除file中连续的重复

案例

[root@yunwei1 tmp]# cat file 
aab
11
12
11
11
5555
5555
34
dd
DD
[root@yunwei1 tmp]# uniq file 
aab
11
12
11
5555
34
dd
DD
[root@yunwei1 tmp]# 
[root@yunwei1 tmp]# uniq -i file 忽略大小写,最后一行的DD被去除
aab
11
12
11
5555
34
dd
[root@yunwei1 tmp]# uniq -ic file 
1 aab
1 11
1 12
2 11
2 5555
1 34
2 dd
[root@yunwei1 tmp]# 

 5.tee工具

从标准输入读取内容写到标准输出和指定的文件中

tee 选项 文件
-a 追加重定向

案例

[root@yunwei1 tmp]# echo hello |tee 1.txt
hello
[root@yunwei1 tmp]# cat 1.txt 
hello
[root@yunwei1 tmp]# echo good |tee -a 1.txt 
good
[root@yunwei1 tmp]# cat 1.txt 
hello
good
[root@yunwei1 tmp]# 

6.tr工具

用来从标准输入中通过替换或删除操作进行字符替换

主要用于删除文件中的控制字符或进行字符转换

tr 字符串1 字符串2 <文件

命名的执行结果|tee 字符串1 字符串2 用字符串2替换字符串1

tr 选项 字符串 <文件
结果|tr 选项 字符串
选项
-d 删除
-s 删除所有重复出现的字符序列,只保留只一个,即将重复出现字符串压缩为一个字符串

补充:匹配字符的相关内容
a-z 任意小写
A-Z 任意大写
0-9 任意数字

案例

[root@yunwei1 tmp]# cat 2.txt 
aa bb cc d
12
11 22 33
[root@yunwei1 tmp]# tr " " "#" <2.txt 将空格替换为#
aa#####bb###cc#d
12
11###22##33
[root@yunwei1 tmp]# tr " " "#" <2.txt |tr -s "#" 将#号压缩
aa#bb#cc#d
12
11#22#33
[root@yunwei1 tmp]# 
[root@yunwei1 tmp]# tr " " "#" <2.txt |tr -s "#"|tr -d "a-z"
###
12
11#22#33

7.其他工具

(一)wc工具

wc -l 行数
wc -w 单词数
wc -c 字符数

案例

[root@yunwei1 tmp]# cat test |wc -l
8
[root@yunwei1 tmp]# cat test |wc -w
8
[root@yunwei1 tmp]# cat test |wc -c
312
[root@yunwei1 tmp]# 

(二)du和df工具

du 用于统计文件的大小
常见选项:
-h: 以人性化的方式查看 KB、MB、GB
-s: 显示总和

案例

[root@yunwei1 tmp]# du -sh .
60K    .
[root@yunwei1 tmp]# du -sh ./*
4.0K    ./1.txt
4.0K    ./2.txt
8.0K    ./dir
4.0K    ./file
4.0K    ./inittab
4.0K    ./test
8.0K    ./vsftpd.conf
[root@yunwei1 tmp]# 

df 用户查看文件系统已挂载盘使用情况
-h:以人性化的方式查看 KB、MB、GB
-H:使用1000位进制单位,而不是1024
-T:显示文件系统类型
-i:列出inode信息

案例

[root@yunwei1 tmp]# df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
ext4 18003272 5693760 11388324 34% /
tmpfs tmpfs 502056 0 502056 0% /dev/shm
/dev/sda1 ext4 487652 36031 426021 8% /boot
[root@yunwei1 tmp]#

猜你喜欢

转载自www.cnblogs.com/golinux/p/10809055.html