马哥-文件查找和压缩-查找篇

文件查找和压缩

文件查找

文件查找:
locate:非实时查找
find:实时查找

locate [OPTION] PATTERN 非实时搜索

工作特点:
查找速度快
模糊查找
非实时查找
搜索的是文件的全路径,不仅仅是文件名
可能只搜索用户具备读取和执行权限的目录
locate

[OPTION]
    -i       #不区分大小写
    -n NUM   #显示前NUM行
    -r       #使用正则表达式

#locate命令搜索的数据库
[root@localhost /data/shell]#ll /var/lib/mlocate/mlocate.db 
-rw-r-----. 1 root slocate 3321213 Oct 10 09:11 /var/lib/mlocate/mlocate.db

#立即更新locate数据库
[root@localhost ~]#undatadb

#示例
[root@localhost ~]#locate -r '\.jpg$'
/usr/lib64/pygtk/2.0/demos/images/background.jpg
/usr/share/backgrounds/day.jpg
/usr/share/backgrounds/default.jpg
/usr/share/backgrounds/morning.jpg
/usr/share/backgrounds/night.jpg

find [OPTION] [/路径] [条件] [处理动作] 实时搜索文件

工作特点:
查找慢
精确查找
实时查找
可能只搜索用户具备读取和执行权限的目录

[OPTION]
    -ls   #用于显示属性

条件

文件属性条件

命令 用途
-regex 正则表达式,完整路径
-maxdepth NUM 搜索最大目录深度NUM
-mindapth NUM 搜索最小目录深度NUM
-depth 先处理目录内的文件,再处理指定目录
-name filename 查文件名,支持glob
-iname filename 查文件名,不区分大小写
-samefile name 相同inode号的文件
links NUM 连接数为NUM的文件
inum NUM 节点编号inode,NUM为inode

属主/组条件

命令 用途
-user username 指定用户UID的文件
-group groupname 指定组GID的文件
-uid userid 指定UID号的文件
-gid groupid 指定GID号的文件
-nouser 没有属主的文件
nogroup 没有属组的文件

文件类型条件

命令 用途
-type TYPE 根据文件类型查找
f 普通文件
d 目录
l 符号链接文件
s 套接字文件
b 块设备文件
c 字符设备文件
p 管道文件
-empty 空文件
-type d -empty 空目录

逻辑条件

命令 用途
-a 与;并且
-o 或;或者
-not或! 非;取反
#示例
[root@localhost ~]#find -not \( -user wang -o -user tom \)

文件大小查找条件

命令 用途
-size[+-]SIZE 单位k,M,G,c(byte)
SIZE ( SIZE-1,SIZE],6K表示为(5k,6k])
-SIZE [0,SIZE-1]-6K表示[0,5k]
+SIZE (SIZE,∞)+6k表示(6k,∞)
()   #为闭区间,代表不包含的意思
[]   #为开区间,表示包含的意思

文件时间戳条件

命令 用途
-atime、-mtime、-ctime[+-]TIMES 以天为单位
TIMES [TIMES,TIMES+1)
-TIMES [TIMES+1,∞)
+TIMES [0,TIMES)
()   #为闭区间,代表不包含的意思
[]   #为开区间,表示包含的意思

命令 用途
-amin 访问时间
-mmin 修改时间
-cmin 状态修改时间

权限查找条件

命令 用途
-perm[+-]MODE 权限查找
MODE 精确权限匹配
+MODE 任意一类权限匹配即可(Centos7/MODE)
-MODE 每一类对象都必须同时拥有指定权限,0表示不关注(MODE中的0)

示例

#示例文件
96  0 -rwxrwxrwx 1 root root 0 May 5 21:56 ./f1

#find -perm MODE : 精确匹配权限为`755`的文件
[root@CentOS7 dir1]$find -perm 755 -ls
[root@CentOS7 dir1]
/*
96  0 -rwxrwxrwx 1 root root 0 May 5 21:56 ./f1
       ═╥═┉┰┉─┳━
        7  7  7
        ≠  ≠  ≠
        7  5  5
精确匹配权限为`755`的文件
*/

`find -perm -MODE : 匹配权限大于或等于`755`的文件`
[root@localhost ~]$find -perm -755 -ls
96  0 -rwxrwxrwx 1 root root 0 May 5 21:56 ./f1
/*
96  0 -rwxrwxrwx 1 root root 0 May 5 21:56 ./f1
       ═╥═┉┰┉─┳━
        7  7  7
        И  И  И
        7  5  5
匹配权限大于或等于`755`的文件
*/

#find -perm /MODE : 匹配权限为`u=7或g=5或o=5`的文件
[root@localhost ~]$find -perm /755 -ls
96  0 -rwxrwxrwx 1 root root 0 May 5 21:56 ./f1
/*
96  0 -rwxrwxrwx 1 root root 0 May 5 21:56 ./f1
       ═╥═┉┰┉─┳━
        7  7  7
        ‖或‖或 ‖
        7  5  5
匹配权限为`u=7或g=5或o=5`的文件
*/

#特殊权限:SUID,SGID,Sticky
[root@localhost ~]$find -perm /7755
/*
96  0 -rwxrwxrwx 1 root root 0 May 5 21:56 ./f1
       ═╥═┉┰┉─┳━
    7   7  7  7
    ║   ‖或‖或 ‖
    ║   7  5  5
特殊 ║
权限 ║  
   s/s/t
   ↓ ↓ ↓
┌──────────重点──────────────┐
┃/7000表示s/s/t任意一位符合即可 ┃
└───────────────────────────┘
匹配权限为`u=7或g=5或o=5`的文件
*/

处理动作

命令 用途
-print 默认
-ls 对找到的文件执行ls -l
-delete 删除查找到的文件
fls file 所有文件长格式保存到file
-ok COMMAND {} \; 对查找到的内容执行COMMAND
-exec COMMAND {} \; 对查找到的内容执行COMMAND
-ok      #头
COMMAND  #命令
{}       #对查找到的内容的引用
\;       #结束

find示例

1. 查找 /root 下后缀为txt的文件
[root@localhost ~]$find /root -name "*.txt"

2. 查找属主为wang但不是root组的文件
[root@localhost ~]$find -user wang -not -group root

4. 查找属主不是wang也不是chen的文件
[root@localhost ~]$find -not \( -user wang -o -user chen \)`

5. /tmp 下查找属主不是wang也不是chen的文件并长格式显示
[root@localhost ~]$find /tmp -not \(-user root -o -name "f*" \) -ls

6. /etc 下排除 /etc/sane.d 查找后缀conf的文件
[root@localhost ~]$find /etc -path '/etc/sane.d' -a -prune -o -name "*.conf" 

7. /etc下排除/etc/sane.d和/etc/fonts查找后缀conf的文件
[root@localhost ~]$find /etc \( -path "/etc/sane.d" -o -path "/etc/fonts" \) -a -prune -o -name "*.conf" 

8. 查找后缀conf的文件并复制到当前文件夹下加.orig后缀
[root@localhost ~]$find -name "*.conf" -exec cp {} {}.orig \;

9.查找修改状态时间在[0,3)天的文件并删除
[root@localhost ~]$find /tmp -ctime +3 -user wang -ok rm {} \;

10.查找other权限>2的文件并去写权限
[root@localhost ~]$find ~ -perm -002 -exec chmod o-w {} \;

11./data查找权限为644且后缀为.sh的普通文件并修改权限为755
[root@localhost ~]$find /data -type f -perm 644 -name "*.sh" -exec chmod 755 {} \;

12.查找/home下的目录
[root@localhost ~]$find /home -type d -ls

xargs参数替换

xargs用于某个命令的参数,xargs可以读入stdin的数据,并可以以空格符或回车将stdin的数据分隔成arguments

#在文件很多的时候,使用rm无法直接删除
[root@localhost ~]$ls f*|xargs rm

find /sbin/ -perm +700 |ls -l
find /bin/ -perm /7000 |xargs ls -l
find /bin/ -prem -7000 |xargs ls -l

#问题
[root@localhost ~]$find -name "*.txt" |xargs rm

#使用\0输出并分隔
[root@localhost ~]$find -name "*.txt" -print0 |xargs -0 rm
#将文件以ASCII表中的0做分隔,找到以0做分隔的.txt结尾的文件删除

实验手册

利用sed 取出ifconfig ens33命令中本机的IPv4地址  
ifconfig ens33|sed -r '2!d ; s#(.*inet)(.*)( netmask.*)#\2#'  

删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符  
sed -r 's/^#[[:blank:]]+// ; /^#.*$/d' /data/fstab  

把/etc/httpd/conf/httpd.conf文件内的Linsten 80改为Listen 8081  
sed -r 's/Listen 80/Listen 8081/g' /etc/httpd/conf/httpd.conf

把pets文件中所有的dog修改为cat
[root@localhost /data]#cat pets
1 dog
2 cat
3 dog
4 cat
[root@localhost /data]#cat pets|sed 's/dog/cat/g'
1 cat
2 cat
3 cat
4 cat
[root@localhost /data]#cat pets
1 dog
2 cat
3 dog
4 cat
注意:此处的修改仅是显示在屏幕输出了,实际文件内容并没有修改。

删除pets文件中的第2行
[root@localhost /data]#cat /data/pets |sed '2d'
1 dog
3 dog
4 cat

仅显示pets文件的第2行
[root@localhost /data]#sed -n '2p' /data/pets 
2 cat

把pets文件的第2行显示2遍
[root@localhost /data]#sed '2p' /data/pets 
1 dog
2 cat
2 cat
3 dog
4 cat

显示pets文件的最后1行
[root@localhost /data]#sed -n '$p' /data/pets 
4 cat

显示pets文件中包含dog字符串的所有的行
[root@localhost /data]#sed -n '/dog/p' /data/pets 
1 dog
3 dog

显示pets文件中,包含2或4的行之间的所有行
[root@localhost /data]#sed -n '/2/,/4/p' /data/pets 
2 cat
3 dog
4 cat

显示pets文件中,第1行到第3行之间的所有行
[root@localhost /data]#sed -n '1,3p' /data/pets
1 dog
2 cat
3 dog

显示pets文件中第2行及后面的1行
[root@localhost /data]#sed -n '2,+1p' /data/pets 
2 cat
3 dog

显示pets文件中第1行和dog字符串之间的行
[root@localhost /data]#sed -n '1,/dog/p' /data/pets 
1 dog
2 cat
3 dog

显示pets文件的奇数行
[root@localhost /data]#sed -n '1~2p' /data/pets 
1 dog
3 dog

显示pets文件的偶数行
[root@localhost /data]#sed -n '2~2p' /data/pets 
2 cat
4 cat

在pets文件的第2行的下一行添加hello
[root@localhost /data]#sed  '2ahello' /data/pets 
1 dog
2 cat
hello
3 dog
4 cat

在pets文件的第2行的下一行添加2行内容为hello和world
[root@localhost /data]#sed  '2a\hello\nworld' /data/pets 
1 dog
2 cat
hello
world
3 dog
4 cat

在pets文件的第2行的前一行添加2行内容为hello和world
[root@localhost /data]#sed '2i\hello\nworld' /data/pets 
1 dog
hello
world
2 cat
3 dog
4 cat

把pets文件的第2行替换为hello
[root@localhost /data]#sed -r '2c\hello' /data/pets 
1 dog
hello
3 dog
4 cat

把pets文件的第1-3行内容,另存为test.txt文件
[root@localhost /data]#sed '1,3w test.txt' /data/pets 
1 dog
2 cat
3 dog
4 cat
[root@localhost /data]#cat /data/test.txt 
1 dog
2 cat
3 dog

在第2行后读入test.txt文件
[root@localhost /data]#sed '2rtest.txt' /data/pets 
1 dog
2 cat
1 dog
2 cat
3 dog
3 dog
4 cat

不显示第2行
[root@localhost /data]#sed -n '2!p' /data/pets 
1 dog
3 dog
4 cat

把pets文件中的每行内容前都编序号显示
[root@localhost /data]#sed  '=' /data/pets 
1
1 dog
2
2 cat
3
3 dog
4
4 cat

猜你喜欢

转载自blog.51cto.com/14547542/2445768