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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1) gzip 命令:
     gzip  [OPTION]... FILE ... 
         选项:
             -d:解压缩,相当于gunzip  
             -c:将压缩或解压缩的结果输出至标准输出
[root@centos7 ~] # gzip -c awk.txt >awk.gz
[root@centos7 ~] # ls
awk .gz   awk .txt   passwd   test .sh   test .x
[root@centos7 ~] # gzip -c passwd >passwdddddd.gz
[root@centos7 ~] # ls
awk .gz   awk .txt   passwd   passwdddddd.gz   test .sh   test .x
[root@centos7 ~] # gunzip passwdddddd.gz
[root@centos7 ~] # ls
awk .gz   awk .txt   passwd   passwdddddd   test .sh   test .x
[root@centos7 ~] # gzip -d awk.gz
[root@centos7 ~] # ls
awk   awk .txt   passwd   passwdddddd   test .sh   test .x
[root@centos7 ~] #

bzip2命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2) bzip2 命令:
bzip2  [OPTION]... FILE ... 
     -k: keep, 保留原文件 
     -d:解压缩 
[root@centos7 ~] # ls
awk   awk .txt   passwd   passwdddddd   test .sh   test .x
[root@centos7 ~] # bzip2 -k passwd
[root@centos7 ~] # ls
awk   awk .txt   passwd   passwd .bz2  passwdddddd   test .sh   test .x
[root@centos7 ~] # bzip2 passwdddddd
[root@centos7 ~] # ls
awk   awk .txt   passwd   passwd .bz2  passwdddddd.bz2   test .sh   test .x
[root@centos7 ~] # bunzip2 passwdddddd.bz2
[root@centos7 ~] # ls
awk   awk .txt   passwd   passwd .bz2  passwdddddd   test .sh   test .x
[root@centos7 ~] # bunzip2 -d passwd.bz2
bunzip2: Output  file  passwd  already exists.
[root@centos7 ~] # ls
awk   awk .txt   passwd   passwd .bz2  passwdddddd   test .sh   test .x
[root@centos7 ~] # mv passwd passwd1
[root@centos7 ~] # bunzip2 -d passwd.bz2
[root@centos7 ~] # ls
awk   awk .txt   passwd   passwd1  passwdddddd   test .sh   test .x
[root@centos7 ~] #

zip命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
3)zip命令:
     zip - package and compress (archive) files
     1)打包
         zip 打包后的文件名  要打包的文件
         zip  passwd .zip  passwd
     2)解压
         unzip .zip结尾的文件
         unzip  passwd .zip 
root@centos7 ~] # ls
awk   awk .txt   passwd   passwd1  passwdddddd   test .sh   test .x
[root@centos7 ~] # zip -r passwd.zip passwd
   adding:  passwd  (deflated 63%)
[root@centos7 ~] # ls
awk   awk .txt   passwd   passwd1  passwdddddd   passwd .zip   test .sh   test .x
[root@centos7 ~] # unzip passwd.zip
Archive:   passwd .zip
replace  passwd ? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
   inflating:  passwd
[root@centos7 ~] # ls
awk   awk .txt   passwd   passwd1  passwdddddd   passwd .zip   test .sh   test .x
[root@centos7 ~] # rm -f passwd
[root@centos7 ~] # unzip passwd.zip
Archive:   passwd .zip
   inflating:  passwd
[root@centos7 ~] # ls
awk   awk .txt   passwd   passwd1  passwdddddd   passwd .zip   test .sh   test .x

tar命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
归档工具:
    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 格式的压缩
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@centos7 ~] # ls
awk   awk .txt   passwd   passwd1  passwdddddd   passwd .zip   test .sh
[root@centos7 ~] # tar -zcvf passwd.tar.gz passwd
passwd
[root@centos7 ~] # ls
awk   awk .txt   passwd   passwd1  passwdddddd   passwd . tar .gz   passwd .zip   test .sh
[root@centos7 ~] # tar -jcvf passwd.tar.bz2 passwd
passwd
[root@centos7 ~] # ls
awk       passwd    passwdddddd      passwd . tar .gz   test .sh
awk .txt  passwd1   passwd . tar .bz2   passwd .zip
[root@centos7 ~] # tar -zxvf passwd.tar.gz
passwd
[root@centos7 ~] # ls
awk       passwd    passwdddddd      passwd . tar .gz   test .sh
awk .txt  passwd1   passwd . tar .bz2   passwd .zip
[root@centos7 ~] # rm -f passwd
[root@centos7 ~] # tar -zxvf passwd.tar.gz
passwd
[root@centos7 ~] # ls
awk       passwd    passwdddddd      passwd . tar .gz   test .sh
awk .txt  passwd1   passwd . tar .bz2   passwd .zip
[root@centos7 ~] # rm -f passwd
[root@centos7 ~] # tar -jxvf passwd.tar.bz2
passwd
[root@centos7 ~] # ls
awk  passwd  passwdddddd  passwd . tar .gz  test .sh
awk .txt passwd1  passwd . tar .bz2  passwd .zip


三、实战小练习

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

2、查找/var目录下不属于root、lp、gdm的所有文件 
[root@centos7 ~]# find /var/ -not \( -userroot -o -user lp -o -user gdm \)|wc -l
125
[root@centos7 ~]# find /var/ -not -userroot -a -not -user lp -a -not -user gdm |wc -l
125
[root@centos7 ~]#
 
3、查找/var目录下最近一周内其内容修改过,同时属主不为 root,也不是postfix的文件 
[root@centos7~]# 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
[root@centos7 ~]# 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
[root@centos7 ~]#
 
4、查找当前系统上没有属主或属组,且最近一个周内曾被访 问过的文件 
[root@centos7 ~]# 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
[root@centos7 ~]#
 
5、查找/etc目录下大于1M且类型为普通文件的所有文件 
[root@centos7 ~]# find /etc/ -size +1M  -type f
/etc/selinux/targeted/policy/policy.29
/etc/udev/hwdb.bin
/etc/brltty/zh-tw.ctb
[root@centos7 ~]#
 
6、查找/etc目录下所有用户都没有写权限的文件 
[root@centos7 ~]# find /etc/  -not -perm  /222
[root@centos6 ~]# find /etc/ -not -perm  +222 
[root@centos7 ~]# find /etc/ -not -perm  /222 |wc -l
23
[root@centos6 ~]# find /etc/ -not -perm +222 |wc -l
23
[root@centos6 ~]#

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

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

本文转自chengong1013 51CTO博客,原文链接:http://blog.51cto.com/purify/1838930,如需转载请自行联系原作者

相关文章
|
24天前
|
Linux Shell
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
78 1
|
27天前
|
Linux 数据安全/隐私保护 Windows
Linux入门指南:linux权限究竟是什么?和文件有什么关系?
Linux入门指南:linux权限究竟是什么?和文件有什么关系?
30 0
|
22天前
|
人工智能 安全 Linux
【Linux】Linux之间如何互传文件(详细讲解)
【Linux】Linux之间如何互传文件(详细讲解)
|
2天前
|
固态存储 Ubuntu Linux
Linux(29) 多线程快速解压缩|删除|监视大型文件
Linux(29) 多线程快速解压缩|删除|监视大型文件
10 1
|
2天前
|
Ubuntu Linux 数据安全/隐私保护
Linux(24) 如何在Ubuntu中操作rootfs.img文件
Linux(24) 如何在Ubuntu中操作rootfs.img文件
2 0
|
6天前
|
安全 Linux 开发工具
Linux中可引起文件时间戳改变的相关命令
【4月更文挑战第12天】Linux中可引起文件时间戳改变的相关命令
12 0
|
8天前
|
Linux Shell 开发工具
Linux文件常用操作
Linux文件常用操作(几乎覆盖所有日常使用)
81 0
|
9天前
|
Linux 内存技术 Perl
【ZYNQ】制作从 QSPI Flash 启动 Linux 的启动文件
【ZYNQ】制作从 QSPI Flash 启动 Linux 的启动文件
|
15天前
|
Linux
linux 超过4个G的文件传不上去的解决办法
linux 超过4个G的文件传不上去的解决办法
9 0
|
15天前
|
Linux 索引
linux 文件查找 和文件管理常用命令
linux 文件查找 和文件管理常用命令
22 0