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

一、文件查找

1.locate命令:
locate KEYWORD
常用选项:
    -i 执行区分大小写的搜索
    -n  N只列举前N个匹配项目
查询系统上预建的文件索引数据库在:/var/lib/mlocate/mlocate.db上,由于事先建立索引,所以查找速度快。
2.find命令:
实时查找工具,通过遍历指定路径完成文件查找,查询的速度稍微慢点,精确查找,实时查找。可能只搜索用户具备读取和执行权限的目录。
find - search for files in a directory hierarchy
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]
find [OPTION]... [查找路径] [查找条件] [处理动作] 
查找路径:指定具体目标路径;默认为当前目录 
查找条件:指定的查找标准,可以文件名、大小、类型、 权限等标准进行;默认为找出指定路径下的所有文件 
处理动作:对符合条件的文件做操作,默认输出至屏幕

1)根据文件名和inode查找
    -name "文件名称":支持通配符 *,?[],[^]
    -iname "文件名称":不区分大小写
    -inum n 按照inode号查找 
2)根据属主、属组查找:
    -user UserName:按照用户名查找
    -group GroupName:按照组名查找 
    -uid:按照Uid进行检索 
    -gid:按照gid进行检索 
    -nouser:查找没有属主的文件 
    -nogroup:查找没有属组的文件 
3)文件类型查找
-type: 根据文件类型查找
     f: 普通文件
     d: 目录
     b: 块设备
     c: 字符设备
     l: 符号链接文件
     p: 命名管道
     s: 套接字    
4)组合条件(摩根定律)
    -a: 与,同时满足
    -o: 或,
    -not, !:非,取反
    非A,并且 非B: 非(A或B)        
    -not ( -user hadoop -o -iname "*.txt" )
    非A,或 非B: 非(A且B)
实例:
    !A -a !B = !(A -o B)
    !A -o !B = !(A -a B)
    find -name "file.txt" 
    find -iname "file.txt"
    find / -name "*.txt" 
    find /var/ -name "*log*" 
    find -user alren -group gentoo  
    find -user chen -not group chen  
    find -user gentoo -o -user archlnux 
    find /-user alren -o -uid 1001 
    find -not \(-user chen -o -user alren\)等价于find -not -user chen -a -not -user alren 
5)文件大小查找 
    -size [+|-]#UNIT 
        常用单位:k, M, G 
    #UNIT: (#-1, #] 
        如:6k 表示(5k,6k] 
    -#UNIT:[0,#-1] 
        如:-6k 表示[0,5k] 
    +#UNIT:(#,∞) 
        如:+6k 表示(6k,∞)
6)时间戳查找  
根据时间戳: 以“天”为单位; 如下#=1为例
-atime [+|-]#,  
#: [#,#+1) find /etc/ -atme 1 :表示大于等于1天,小于2天时间段被访问过;
+#: [#+1,∞] find /etc/ -atime +1 :表示一天之外包括一天被访问过
-#: [0,#)  find /etc/ -atime -1 :表示一天前被访问过;
-mtime 
-ctime 
以“分钟”为单位: -amin -mmin -cmin
7)根据权限查找:
-perm [+|-]MODE
    MODE:精确匹配
    +MODE: 任何一类用户的任何一位权限匹配;常用于查找某类用户的某特定权限是否存在(cetos6.x);
    /MODE:任何一类用户(u,g,o)对象的权限中只要能一位匹配即可,或关系,+ 从centos7开始淘汰 
    -MODE: 每类用户的指定要检查的权限位都匹配,则为或关系;
   find -perm 755 会匹配权限模式恰好是755的文件 
   只要当任意人有写权限时,find -perm +222就会匹配 
     如: find /var/ -perm +222 或者find /var/ -perm /222 只要任意用户有写的权限时即可被匹配
   只有当每个人都有写权限时,find -perm -222才会匹配
     如:find -perm -222 只有所有人都有读的权限时,才能被匹配到
   只有当其它人(other)有写权限时,find -perm -002才 会匹配
     如:find /var/ -perm -002 只有其他人有读的权限时才能匹配到    

处理动作:
-print:默认的处理动作,显示值屏幕
-ls:类似于对查找到的文件执行ls -l命令 
-delete:删除查找到的文件 
-exec COMMAND {} \;对查找到的每个文件执行由command指定的命令
有些命令不能接受过多参数,此时命令执行可能会失败,下面方式可规避此问题 
   find |xargs COMMAND 
FIDN实例:
find -name "*.conf" -exec cp {} {}.com \; #备份配置文件,并改名
find /tmp -ctime +3 -user chen -ok -rm {} \;#提示删除存在时间超过三天以上的chen用户临时文件
find ~ -perm -002 -exec chmod o-w {} \; #在你的主目录中寻找可被其它用户写入的文件 
find /data –type  f -perm 644  -name “*.sh” –exec chmod 755 {} \; 
find  /home –type d -|xargs rm -rf

二、压缩、解压缩及其归档工具

gzip命令:

1)gzip命令:
    gzip [OPTION]... FILE ... 
        选项:
            -d:解压缩,相当于gunzip  
            -c:将压缩或解压缩的结果输出至标准输出
[[email protected] ~]# gzip -c awk.txt >awk.gz
[[email protected] ~]# ls
awk.gz  awk.txt  passwd  test.sh  test.x
[[email protected] ~]# gzip -c passwd >passwdddddd.gz
[[email protected] ~]# ls
awk.gz  awk.txt  passwd  passwdddddd.gz  test.sh  test.x
[[email protected] ~]# gunzip passwdddddd.gz
[[email protected] ~]# ls
awk.gz  awk.txt  passwd  passwdddddd  test.sh  test.x
[[email protected] ~]# gzip -d awk.gz
[[email protected] ~]# ls
awk  awk.txt  passwd  passwdddddd  test.sh  test.x
[[email protected] ~]#

bzip2命令:

2)bzip2命令:
bzip2 [OPTION]... FILE ... 
    -k: keep, 保留原文件 
    -d:解压缩 
[[email protected] ~]# ls
awk  awk.txt  passwd  passwdddddd  test.sh  test.x
[[email protected] ~]# bzip2 -k passwd
[[email protected] ~]# ls
awk  awk.txt  passwd  passwd.bz2  passwdddddd  test.sh  test.x
[[email protected] ~]# bzip2 passwdddddd
[[email protected] ~]# ls
awk  awk.txt  passwd  passwd.bz2  passwdddddd.bz2  test.sh  test.x
[[email protected] ~]# bunzip2 passwdddddd.bz2
[[email protected] ~]# ls
awk  awk.txt  passwd  passwd.bz2  passwdddddd  test.sh  test.x
[[email protected] ~]# bunzip2 -d passwd.bz2
bunzip2: Output file passwd already exists.
[[email protected] ~]# ls
awk  awk.txt  passwd  passwd.bz2  passwdddddd  test.sh  test.x
[[email protected] ~]# mv passwd passwd1
[[email protected] ~]# bunzip2 -d passwd.bz2
[[email protected] ~]# ls
awk  awk.txt  passwd  passwd1  passwdddddd  test.sh  test.x
[[email protected] ~]#

zip命令:

3)zip命令:
    zip - package and compress (archive) files
    1)打包
        zip 打包后的文件名  要打包的文件
        zip passwd.zip passwd
    2)解压
        unzip .zip结尾的文件
        unzip passwd.zip 
[email protected] ~]# ls
awk  awk.txt  passwd  passwd1  passwdddddd  test.sh  test.x
[[email protected] ~]# zip -r passwd.zip passwd
  adding: passwd (deflated 63%)
[[email protected] ~]# ls
awk  awk.txt  passwd  passwd1  passwdddddd  passwd.zip  test.sh  test.x
[[email protected] ~]# unzip passwd.zip
Archive:  passwd.zip
replace passwd? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: passwd
[[email protected] ~]# ls
awk  awk.txt  passwd  passwd1  passwdddddd  passwd.zip  test.sh  test.x
[[email protected] ~]# rm -f passwd
[[email protected] ~]# unzip passwd.zip
Archive:  passwd.zip
  inflating: passwd
[[email protected] ~]# ls
awk  awk.txt  passwd  passwd1  passwdddddd  passwd.zip  test.sh  test.x

tar命令:

归档工具:
   tar [options] -f file.tar File1 ...
     -c: 创建归档
     -x: 展开归档
     -t: 不展开而直接查看被归档的文件
     -v:显示创压缩或解压缩过程
    tar -cvf archive.tar file1 创建一个非压缩的 tarball 
    tar -cvf archive.tar file1 file2 dir1 创建一个包含了‘file1‘,‘file2‘以及‘dir1‘的档案文件 
    tar -tf archive.tar 显示一个包中的内容 
    tar -xvf archive.tar 释放一个包 
    tar -xvf archive.tar -C /tmp 将压缩包释放到 /tmp目录下 
    tar -cvfj archive.tar.bz2 dir1 创建一个bzip2格式的压缩包 
    tar -xvfj archive.tar.bz2 解压一个bzip2格式的压缩包 
    tar -cvfz archive.tar.gz dir1 创建一个gzip格式的压缩包 
    tar -xvfz archive.tar.gz 解压一个gzip格式的压缩
[[email protected] ~]# ls
awk  awk.txt  passwd  passwd1  passwdddddd  passwd.zip  test.sh
[[email protected] ~]# tar -zcvf passwd.tar.gz passwd
passwd
[[email protected] ~]# ls
awk  awk.txt  passwd  passwd1  passwdddddd  passwd.tar.gz  passwd.zip  test.sh
[[email protected] ~]# tar -jcvf passwd.tar.bz2 passwd
passwd
[[email protected] ~]# ls
awk      passwd   passwdddddd     passwd.tar.gz  test.sh
awk.txt  passwd1  passwd.tar.bz2  passwd.zip
[[email protected] ~]# tar -zxvf passwd.tar.gz
passwd
[[email protected] ~]# ls
awk      passwd   passwdddddd     passwd.tar.gz  test.sh
awk.txt  passwd1  passwd.tar.bz2  passwd.zip
[[email protected] ~]# rm -f passwd
[[email protected] ~]# tar -zxvf passwd.tar.gz
passwd
[[email protected] ~]# ls
awk      passwd   passwdddddd     passwd.tar.gz  test.sh
awk.txt  passwd1  passwd.tar.bz2  passwd.zip
[[email protected] ~]# rm -f passwd
[[email protected] ~]# tar -jxvf passwd.tar.bz2
passwd
[[email protected] ~]# ls
awk passwd passwdddddd passwd.tar.gz test.sh
awk.txt passwd1 passwd.tar.bz2 passwd.zip

三、实战小练习

1、查找/var目录下属主为root,且属组为mail的所有文件 
[[email protected] ~]# find /var/ -user root-a  -group mail
/var/spool/mail
/var/spool/mail/root
/var/spool/mail/rooter
[[email protected] ~]#

2、查找/var目录下不属于root、lp、gdm的所有文件 
[[email protected] ~]# find /var/ -not \( -userroot -o -user lp -o -user gdm \)|wc -l
125
[[email protected] ~]# find /var/ -not -userroot -a -not -user lp -a -not -user gdm |wc -l
125
[[email protected] ~]#
 
3、查找/var目录下最近一周内其内容修改过,同时属主不为 root,也不是postfix的文件 
[[email protected]~]# find /var/ -mtime -7 -a -not -user root -a -not -user  postfix
/var/lib/setroubleshoot/setroubleshoot_database.xml
/var/spool/abrt/ccpp-2016-07-19-15:47:15-3568
[[email protected] ~]# find /var/ -mtime -7 -not \( -user root -o  -user postfix \)
/var/lib/setroubleshoot/setroubleshoot_database.xml
/var/spool/abrt/ccpp-2016-07-19-15:47:15-3568
[[email protected] ~]#
 
4、查找当前系统上没有属主或属组,且最近一个周内曾被访 问过的文件 
[[email protected] ~]# find / \( -nouser -o-nogroup \) -a -atime -7
find: ‘/proc/8419/task/8419/fd/6’: No suchfile or directory
find: ‘/proc/8419/task/8419/fdinfo/6’: Nosuch file or directory
find: ‘/proc/8419/fd/6’: No such file ordirectory
find: ‘/proc/8419/fdinfo/6’: No such fileor directory
[roo[email protected] ~]#
 
5、查找/etc目录下大于1M且类型为普通文件的所有文件 
[[email protected] ~]# find /etc/ -size +1M  -type f
/etc/selinux/targeted/policy/policy.29
/etc/udev/hwdb.bin
/etc/brltty/zh-tw.ctb
[[email protected] ~]#
 
6、查找/etc目录下所有用户都没有写权限的文件 
[[email protected] ~]# find /etc/  -not -perm  /222
[[email protected] ~]# find /etc/ -not -perm  +222 
[[email protected] ~]# find /etc/ -not -perm  /222 |wc -l
23
[[email protected] ~]# find /etc/ -not -perm +222 |wc -l
23
[[email protected] ~]#

7、查找/etc目录下至少有一类用户没有执行权限的文件 
[[email protected] ~]# find /etc/ -not-perm  -111

8、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件
[[email protected] ~]# find /etc/init.d/ -perm-113

小编总结也辛苦,如果觉得还可以,顺手点个赞:).......

时间: 2024-12-08 14:05:09

Linux文件查找及压缩常用知识总结的相关文章

linux文件查找和压缩

locate          非实查找,查找是根据全系统文件数据库进行的,                  #updatedb, 手动生成数据库,  locate查找速度快 find :   实时精确,支持众多查找标准,遍历指定目录的所有文件,速度慢 find    查找路径  查找标准 查找标准 查找以后处理的运作         查找路劲:默认为当前目录         查找标准:默认为指定路劲下的所有文件         处理运作:默认为显示 匹配标准:-maxdepth level

Linux中的文件查找和解压缩

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

linux基础学习-第十天(文件查找和压缩)

2016-08-12 授课内容: shell的流程控制 文件查找和压缩 shell的流程控制: 过程式编程语言: 顺序执行 选择执行 循环执行(未讲) 顺序执行: if语句: 单分支 if 判断条件; then 条件为真的分支代码 fi 双分支 if 判断条件; then 条件为真的分支代码 else 条件为假的分支代码 fi 多分支 if CONDITION1; then if-true elifCONDITION2; then if-ture elifCONDITION3; then if-

linux基础正则表达式、shell基础、文件查找和压缩

linux基础正则表达式.shell基础.文件查找和压缩 1.shell编程显示电脑的基本信息,初级基础脚本.只适合6.7版本的. COLOR="\033[1;36m" COLOREND="\033[0m" echo -e "CPU type is $COLOR `lscpu |grep 'Model name'|tr -s ' '|cut -d: -f2`$COLOREND" echo -e "Disk space is $COLOR

Linux文件查找.md

Linux 文件查找 在Linux系统的查找相关的命令: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 whereis whereis命令是定位可执行文件.源代码文件.帮助文件在文件系统中的位置.这些文件的属性应属于原始代码,二进制文件,或是帮助文件.whereis 程序还具有搜索源代码.指定备用搜索路径和搜索不寻常项的能力. 语法 whereis [-bmsu] 文件或者目录名称 参数 -b 定位可执

第十章、文件查找和压缩

第十章.文件查找和压缩 本章内容 使用locate命令 使用find命令 压缩和解压缩工具 文件查找 在文件系统上查找符合条件的文件 文件查找:locate, find 非实时查找(数据库查找):locate 实时查找:find locate 查询系统上预建的文件索引数据库 /var/lib/mlocate/mlocate.db 依赖于事先构建的索引 索引的构建是在系统较为空闲时自动进行(周期性任务),管理员手动更新数据库(updatedb) 索引构建过程需要遍历整个根文件系统,极消耗资源 工作

7-2 文件查找和压缩归档

文件查找和压缩归档 文件查找 locate命令 介绍 查询系统上预建的文件索引数据库 /var/lib/mlocate/mlocate.db 依赖于事先构建的索引 索引的构建是在系统较为空闲时自动进行(周期性任务),管理员手动更新数据库(updatedb) 索引构建过程需要遍历整个根文件系统,极消耗资源 工作特点: 查找速度快 模糊查找 非实时查找 搜索的是文件的全路径,不仅仅是文件名 可能只搜索用户具备读取和执行权限的目录 语法 locate KEYWORD 常用选项 -i 不区分大小写的搜索

Linux文件查找之find&locate

Linux文件查找之find&locate 一.概述 Linux系统核心的思想之一"一切皆文件",对于这么多的文件,如何快速查找过滤呢?下面我们就看看系统提供的文件查找命令find和locat,熟练使用find命令是运维人员的必经之路 二.find的用法及示例 1.find特点 查找速度略慢 精确查找 实时查找 只能搜索有读取和执行权限的目录 2.find用法 用法:find  [options]  [查找路径]  [ 查找条件]  [处理动作] 查找条件: 根据文件类型查找

关于文件查找和解压缩

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