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

文件查找和压缩

文件查找

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

locate [OPTION] PATTERN 非实时搜索

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

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

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

#立即更新locate数据库
[[email protected] ~]#undatadb

#示例
[[email protected] ~]#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或! 非;取反
#示例
[[email protected] ~]#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`的文件
[[email protected] dir1]$find -perm 755 -ls
[[email protected] dir1]
/*
96  0 -rwxrwxrwx 1 root root 0 May 5 21:56 ./f1
       ═╥═┉┰┉─┳━
        7  7  7
        ≠  ≠  ≠
        7  5  5
精确匹配权限为`755`的文件
*/

`find -perm -MODE : 匹配权限大于或等于`755`的文件`
[[email protected] ~]$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`的文件
[[email protected] ~]$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
[[email protected] ~]$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的文件
[[email protected] ~]$find /root -name "*.txt"

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

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

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

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

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

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

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

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

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

12.查找/home下的目录
[[email protected] ~]$find /home -type d -ls

xargs参数替换

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

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

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

#问题
[[email protected] ~]$find -name "*.txt" |xargs rm

#使用\0输出并分隔
[[email protected] ~]$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
[[email protected] /data]#cat pets
1 dog
2 cat
3 dog
4 cat
[[email protected] /data]#cat pets|sed ‘s/dog/cat/g‘
1 cat
2 cat
3 cat
4 cat
[[email protected] /data]#cat pets
1 dog
2 cat
3 dog
4 cat
注意:此处的修改仅是显示在屏幕输出了,实际文件内容并没有修改。

删除pets文件中的第2行
[[email protected] /data]#cat /data/pets |sed ‘2d‘
1 dog
3 dog
4 cat

仅显示pets文件的第2行
[[email protected] /data]#sed -n ‘2p‘ /data/pets
2 cat

把pets文件的第2行显示2遍
[[email protected] /data]#sed ‘2p‘ /data/pets
1 dog
2 cat
2 cat
3 dog
4 cat

显示pets文件的最后1行
[[email protected] /data]#sed -n ‘$p‘ /data/pets
4 cat

显示pets文件中包含dog字符串的所有的行
[[email protected] /data]#sed -n ‘/dog/p‘ /data/pets
1 dog
3 dog

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

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

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

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

显示pets文件的奇数行
[[email protected] /data]#sed -n ‘1~2p‘ /data/pets
1 dog
3 dog

显示pets文件的偶数行
[[email protected] /data]#sed -n ‘2~2p‘ /data/pets
2 cat
4 cat

在pets文件的第2行的下一行添加hello
[[email protected] /data]#sed  ‘2ahello‘ /data/pets
1 dog
2 cat
hello
3 dog
4 cat

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

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

把pets文件的第2行替换为hello
[[email protected] /data]#sed -r ‘2c\hello‘ /data/pets
1 dog
hello
3 dog
4 cat

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

在第2行后读入test.txt文件
[[email protected] /data]#sed ‘2rtest.txt‘ /data/pets
1 dog
2 cat
1 dog
2 cat
3 dog
3 dog
4 cat

不显示第2行
[[email protected] /data]#sed -n ‘2!p‘ /data/pets
1 dog
3 dog
4 cat

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

原文地址:https://blog.51cto.com/14547542/2445768

时间: 2024-07-30 08:43:44

马哥-文件查找和压缩-查找篇的相关文章

8.15_Linux之bash shell脚本编程入门篇(二)以及文件查找和压缩的使用

bash shell脚本编程入门篇(二) read命令的使用 作用: 使用read来把输入值分配给一个或多个shell变量: -p指定要显示的提示 -t TIMEOUT read从标准输入中读取值,给每个单词分配一个变量,所有剩余单词都被分配给最后一个变量 EX.read -p "Enter a filename:" FILE 条件选择之if的使用 if的作用:选择执行(注意:if语句可嵌套) 单分支 if 判断条件;then 条件为真的分支代码 fi EX.单分支案例 双分支 if

关于文件查找和解压缩

文件查找和解压缩在文件系统上查找符合条件的文件,文件查找的工具有两个,locate 和 find文件查找分为:            locate      非实时查找 (在数据库查找)             updatedb   更新数据库            经常用于搜索稳定的文件,比如配置文件            var/lib/mlocate/mlocate.db 数据库路径             find     实时查找 locate :       查询系统上预建的文件索引

Linux文件查找及压缩常用知识总结

一.文件查找 1.locate命令: locate KEYWORD 常用选项:     -i 执行区分大小写的搜索     -n  N只列举前N个匹配项目 查询系统上预建的文件索引数据库在:/var/lib/mlocate/mlocate.db上,由于事先建立索引,所以查找速度快. 2.find命令: 实时查找工具,通过遍历指定路径完成文件查找,查询的速度稍微慢点,精确查找,实时查找.可能只搜索用户具备读取和执行权限的目录. find - search for files in a direct

Linux 第十天: (08月15日) 文件查找和压缩

locate  非实时查找(数据库查找) /var/lib/mlocate/mlocate.dblocate -i 执行区分大小写的搜索locate -n N 只列举前N个匹配项目locate foo 搜索名称或路径中带有"foo"的文件locate -r '\.foo$'使用Regex来搜索以".foo"结尾的文件 find -name " 文件名称"支持使用glob *, ?, [], [^]find -iname" 文件名称&qu

Linux中的文件查找和解压缩

Linux的文件查找 概述: 本章将主要介绍在Linux中怎样查找文件和解压缩.需要我们掌握的知识有:locate和find 命令的使用,以及如何使用压缩和解压缩工具. 一.Linux文件查找 在文件系统上常常需要根据文件的各种属性去查找符合条件的文件,此前使用的grep,egrep,fgrep属于文本过滤.文本搜索工具:而文件查找工具有两个,locate和find 1.文件查找分为两类: □实时查找:偏历所有文件进行条件匹配       find □非实时查找(数据库查找):根据索引查找  l

文件查找和压缩

在linux系统上,有一个核心思想,那就是一切皆文件.那我们在和linux系统打交道的同时,不免要和文件打交道.既然如此,那掌握文件查找.压缩.解压缩和文件归档类工具是我们必不可少的一项技能. 文件查找工具:locate.find locate是一款非实时性查询工具,它是根据事先建立的索引数据库来查找文件,索引的构建是在系统不繁忙时自动创建,需要遍历整个根文件系统,极其消耗资源,管理员也可以手工创建.正因为如此,所以locate查询文件比较快,但不能够精确查找,都是模糊查询. locate -i

文件查找和压缩打包

作业1 编写一个脚本/root/bin/createuser.sh,脚本的执行语法必须是:createuser.sh -u username -m password,选项与参数间可支持多空格,但不能顺序颠倒. 当未指定正确的选项或参数时,以错误输出方式提示"createuser.sh -u username -m password "后退出脚本. 用户名必须以字母开头,可包括数字和_.否则不合法.以错误输出提示用户"用户名仅包含字母数据和下划线" 当用户名检测合法后

linux下面文件的查找和压缩

   文件的查找     1.脚本文件名的查询--which     1.which这个命令是根据PATH这个环境变量所规范的路径去查询"执行文件"的文件名. 2.文件名的查询--whereis .locate.find     whereis 和locate都是利用数据库查询数据,所以速度相当快,而且并没有实际查询硬盘. whereis -b:只查找二进制文件 -m:只找在说明文件manual路径下的文件 -s:只查找source源文件 locate: -i忽略大小写   -r后面可

文件的查找与压缩归档

文件的查找与压缩归档 locate find 压缩与归档 locate 主要用途 查询系统上预建的文件索引数据库 locate - find files by namelocate [OPTION]... PATTERN... locate查询文件时,会去搜索/var/lib/mlocate/mlocage.db, 这个数据库会在系统空闲时自动周期性进行,一般一天一次更新,手动更新命令为updatedb, 索引的构建需要遍历整个根文件系统,比较消耗资源.但locate有如下特性: 1. 模糊查找