处理文本常用工具
find
grep
sort
uniq
tr
cut
paste
wc
sed
awk
find
- 查找 .txt文件
find ~/Desktop/test/ -name "*.txt"
- 正则查找
find ~/Desktop/test/ -regex ".*.txt" -regex忽略大小写
- 查找非txt文件
find ~/Desktop/test/ ! -name "*.txt"
- 指定深度搜索
find ~/Desktop/test/ -maxdepth 1 -type f maxdepth指定搜索深度
- 最近X天被访问过的所有文件
find ~/Desktop/test/log -atime 2 -type f
- 寻找大于 2k(指定大小)的文件
find ~/Desktop/test/ -type f -size +2k
- 按权限查找
find ~/Desktop/test/ -type f -perm 777
- 按用户查找
find ~/Desktop/test/ -type f -user mysql
- 删除当前目录下所有的.txt文件
find ~/Desktop/test/ -type f -name ".txt" -delete
- 将找到的文件全都copy到另一个目录
find ./ -type f -mtime +10 -name "*.txt" -exec cp {} OLD;
grep
- 在多级目录中对文本递归搜索
grep -r "test" ./
- 匹配多个模式
grep -e "test" -e "te" ./log/1.txt
sort
sort -nrk 1 ./log/1.txt
-n 按数字进行排序 VS -d 按字典序进行排序
-r 逆序排序
-k N 指定按第N列排序
uniq
- 消除重复行
sort -nrk 1 ./log/1.txt | uniq
- 统计各行在文件中重复的次数
sort ./log/1.txt | uniq -c
- 找出重复行
sort ./log/1.txt | uniq -d
tr
- 加密
echo 12345 | tr '0-9' '98765432120'
- 删除字符
cat file | tr -d '0-9'
- 删除非数字字符
cat file | tr -d -c '0-9'
- 获取所有数字
cat file | tr -c '0-9'
- 压缩字符串
cat file | tr -s ' ' 压缩空格
cut
- 截取文件的第2列和第4列
cut -f2,4 file
- 去文件除第3列的所有列
cut -f3 -complement file
- -d 指定定界符
cut -f2 -d";" file
- 打印第1到5个字符
cut -c1-5 file
- 打印前两个字符
cut -c-2 file
paste
- 拼接两个文本
paste file1 file2
wc
- 统计行数
wc -l file
- 统计单词数
wc -w file
- 统计字符数
wc -c file
sed
awk
录播屏幕操作
录制操作
script -t 2>time.file -a scriptfile
time.file是时序记录文件,记录了几秒执行的命令;scriptfile是命令执行记录文件,记录下执行是哪些命令,这两个文件名称都可以自定义
script -q -t 2>time.file -a scriptfile
加上-q 参数可以使script命令以静默模式运行,不显示script启动和exit的命令,用户可以完全察觉不到在录屏。
要停下来,只用按下按下组合键 Ctrl+D
结束录制,执行exit
命令也行。
播放操作
scriptreplay time.file scriptfile
- 添加这个参数-d(divisor number )可以调整播放速度的倍数(小数为慢放)
- -f( flush) 每次操作后都立即刷新缓存 客户端
script -f demo
演示端scriptreplay -f demo
- 参数-c(command )可以直接执行命令,而非是交互式的shell,可以直接传入一个脚本,例如:
script -q "file.out" -c "/root/hello.sh"
查看压缩文件的常用方法
vim编辑器
tar
rar
unrar
zip
unzip
zipinfo
zcat
zless
less