shell 文本处理及查找和文件查找

常用正则
定义释意
?0或1个
.1个字符
*设置其前一个字符任意次
+至少出现一次(1次或多次)等价于.*
POSIX字符类
定义释意
[:space:]空白字符
[:punct:]指定标点
[:lower:]小写字母
[:upper:]大写字母
[:alpha:]大小写字母
[:alnum:]数和大小写字母
[:digit:]
iconv -f gbk -t utf8 ton.txt > result.txt 					转化成utf8格式
find
find -not -user tom
find ./ -not \( -user tom -o -user ton \)
find -print           print:表示将结果输出到标准输出。 
find -user tom -ok rm {}   \;
find -user tom -print | xargs ls -ld
find -inum                 指定i结点
find -type d -empty      #找空文件夹
find / -perm -4000 -o -perm -2000        #0表示对其他位权限不限定进行查找
find -depth					#先处理每个目录的内容,然后再处理目录本身

与执行命令结合

find ./ -maxdepth 1  -type f -name "t*" -print0 | xargs -0 -n 1 echo
find . -type d -exec rm -rf {} \;   删除当前目录下的目录
find . -not  -name "test.c" -a -not -name "."  -exec rm -rf {} \; 删除当前目录下除test.c的所有文件
find . -type f -maxdepth 1 -name '*.txt' -exec sh -c "echo '{}' && echo 'successful'" \;	#执行一个短的脚本
[root@tom-virtual-machine tom]$find ./ -maxdepth 1  -type f -name "t*"      
./tttt
./text.txt
./ton.txt
./tom.txt
[root@tom-virtual-machine tom]$find ./ -mindepth 6  -type f -name "t*"  
./.local/share/evolution/tasks/system/tasks.ics
./test/.git/logs/refs/heads/test-tom
./test/.git/logs/refs/heads/ton-A

其中{} \;

  • {}代表find 找到的文件
  • \禁止转义
  • ;表示本行指令结束

args命令

[tom@tom-virtual-machine ~]$echo '+%Y/+%m/+%d' | xargs -d '/' -n 1 date   #将所有项作为标准输入来执行
2021
05
13

[tom@tom-virtual-machine ~]$echo '11 22   33 44' | xargs -n 1 echo        #将空格隔离的项分开为行
11
22
33
44
[tom@tom-virtual-machine ~]$echo  -e "tom\nton\nboy" | xargs echo		#合并所有的行到一行里面
tom ton boy
grep
 grep -n --color=auto "root" /etc/passwd
 -n 行号
 -s #silence 不显示回显
 -v 匹配取反
 #cat /etc/inittab | grep -v ^# | grep -v ^$  等价于   grep -Ev '^(#|$)'
	-perm 755     文件的权限正好是mode就匹配
	-perm -755    文件的权限包括mode就匹配(该文件还可以拥有额外的权限属性)      777也可以    every
	-perm +755    文件的权限部分满足mode就匹配(已弃用,find新版使用-perm /mode) 001也可以    any
 grep -R -l tom /etc      #含tom字符,搜索文件的含有指定字符 
 grep -U uid   -u euid  #用效id
 	  -A 2	匹配后继续向下打印2行
 	  -B 2  匹配后继续向上打印2行
 	  -C 2	向上向下各打印2行

在这里插入图片描述


nl      #添加行号打印
apropos inittab          查询配置文件信息

ok可用于确认

locate 不能查找到tmp下的东西即使updatedb
locate -i     不区分大小写

cut -c1-3  第一列到到三列 字符为单位  -b  以字节为单位 
cut -d: -f 1,7 				#第:为分隔符取第1和7列   deloimiter 
-d' ' 						#以空格为分隔符

sort -t: +2 -n /etc/passwd  	#从第二列开始按大小排  
-r 反向  
-u  移除重复行 
-k 第三列配合
-t SEP 使用指定分隔符来打印排序
-n 以数值排序  
-M 按月份排序

sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n   #先排第一列,1,1单指和一字段


uniq 只去除当前位置的前后的重复行,隔开则不去除   一般先用sort      -c  统计重复次数 
-d 合并显示重复行  
-u 只显示不重复行
echo * 输出当前文件夹下的各文件名称 
locate -r "正则表达式"

du -sh ./ 								#当前目录下总大小(各文件和)
du -sh   /etc      						#列出文件夹大小
-a       								#列出对应文件大小

#清空文本内容 
echo "" > test.txt
> test.txt
echo /dev/null > test.txt

iconv -f gbk -t utf8 **.txt > obj.txt    #转换gbk编码文件为utf-8编码文件
stat 									#查看文件详细信息


使用head 和 tail 可快速截取中间段指定长度的n行 ,先head后tail
cat -A    显示所有字符
cat -v    查看不可打印字符
cat x* > largefile		    #将小文件全并成一个大文件
split -b 650m largefile     #将大文件分块成多个650mb的文件
创建空文件
$ dd if=/dev/zero of=filename bs=1k count=5 				#5KB 零内容
$ dd if=/dev/urandom of=filename bs=1M count=7 				#7MB 随机内容
$ dd if=/dev/zero of=test bs=1024 count=0 seek=1024			#1Mb 空内容
$ touch filename 											#创建一个0B大小的文件(如果文件存在,更新该文件的修改时间)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值