Linux系统的文件查找

文件查找

inux上的所有资源都以文件的形式存在,如果是手工查找的话,势必会浪费太多的时间,所以文件查找有三种主要方式。

which :命令查找
find: 文件查找,针对文件名
locate:文件查找,依赖数据库

命令文件查找

which

  • 查找ls 命令的位置
    which ls
    //从PATH环境变量
    或者
    where is vim

任意文件查找

find

  • 语法:find [path…] [options] [expression] [action]
    命令 路径 选项 表达式 动作
    • 按文件名查找
[root@localhost ~]# find /etc   -name  "hosts"
/etc/hosts
/etc/avahi/hosts
[root@localhost ~]# find /etc   -iname  "hosts"
/etc/hosts
/etc/avahi/hosts
[root@localhost ~]# find /etc   -iname  "hos*"
/etc/host.conf
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
/etc/selinux/targeted/active/modules/100/hostname
/etc/avahi/hosts
/etc/hostname
[root@localhost ~]# 
文件查找成功     - " i "  代表着忽略大小写
  • 按文件大小查找
[root@localhost ~]# find /tmp -size  +5M
/tmp/255.txt        文件>5M
[root@localhost ~]# find /tmp -size  5M
/tmp/355.txt        文件=5M
[root@localhost ~]# find /tmp -size  -5M
/tmp/155.txt        文件<5M

size: 大小

  • 按照指定查找的目录深度
[root@localhost ~]# find / -maxdepth  3 -a -name "ifcfg-en*"
[root@localhost ~]#     查找文件目录不在三层里
[root@localhost ~]# find / -maxdepth  4 -a -name "ifcfg-en*"
/etc/sysconfig/network-scripts/ifcfg-ens33
[root@localhost ~]#     查找文件目录在四层里

-maxdepth: 指定遍历搜索的最大深度
-mindepth: 指定开始遍历搜索的最小深度

  • 按文件属主、属组查找

[root@localhost ~]# useradd  jack
[root@localhost ~]# groupadd  hr
[root@localhost ~]# find /home  -user jack
/home/jack    属主是jack的文件
/home/jack/.mozilla
/home/jack/.mozilla/extensions
/home/jack/.mozilla/plugins
/home/jack/.bash_logout
/home/jack/.bash_profile
/home/jack/.bashrc

属组与属主查询一样 最后查出 属组是hr的文件

          -user USERNAME  :查找属主为指定用户(UID)的文件

          -group GRPNAME  : 查找属组为指定组(GID)的文件

          -uid UserID     :查找属主为指定的UID号的文件

          -gid GroupID    :查找属组为指定的GID号的文件

          -nouser         :查找没有属主的文件

          -nogroup        :查找没有属组的文件
  • 按文件类型查找
[root@localhost ~]# find /tmp -type  f
/tmp/355.txt
/tmp/155.txt
/tmp/255.txt
/tmp/.X0-lock
/tmp/yum_save_tx.2020-08-04.11-17.6yfN_e.yumtx
[root@localhost ~]# find /dev -type  b
/dev/dm-1
/dev/dm-0
/dev/sr0
/dev/sda2
/dev/sda1
/dev/sda
[root@localhost ~]# 
              f: 普通文件

              d: 目录文件

              l: 符号链接文件

              s:套接字文件

              b: 块设备文件

              c: 字符设备文件

              p: 管道文件
  • 按文件权限查找
    语法:find . -perm 644 -ls
[root@localhost ~]# find . -perm 644 -ls
 -ls     是find的动作之一,精确权限

" . " :代表当前文件夹
“-perm” :代表选项
" -ls " :动作 (默认权限 print :打印)

  • 找到后处理的动作 ACTIONS
    找到后默认显示文件
[root@localhost ~]# find . -perm  715  -print    文件名
[root@localhost ~]# find . -perm  715  -ls      文件属性

找到后删除

[root@localhost ~]# find /etc -name "775*" -delete  删除

找到后复制

[root@localhost ~]# find     /etc     -name       "ifcfg*"      -ok     cp     -rvf   {}    /tmp    \;

" -ok " :后面转接命令

" v ":显示出来

任意文件查找

locate

[root@localhost]#touch  /etc/systemd/233.txt 创建一个新文件
[root@localhost]# updatedb       更新数据库
[root@localhost]# locate 233.txt    查找233.txt

文件打包及压缩

tar命令是Unix/Linux系统中备份文件的可靠方法,
几乎可以工作于任何环境中,它的使用权限是所有用户。
建议针对目录

打包、压缩

  • 语法:tar 选项 压缩包名称 源文件
# tar   -cf         etc.tar          /etc
# tar   -czf       etc-gzip.tar.gz         /etc/
# tar   -cjf       etc-bzip.tar.bz        /etc/
# tar   -cJf         etc-xzip.tar.xz        /etc/

// z是gzip
// j是bzip
// J是xzip
观察压缩文件包

[root@localhost]# ll -h etc*
-rw-r--r--. 1 root root  11M 10月 14 10:07 etc-gzip.tar.gz
-rw-r--r--. 1 root root 8.9M 10月 14 10:08 etc-bzip.tar.bz
-rw-r--r--. 1 root root 7.6M 10月 14 10:08 etc-xzip.tar.xz

压缩速度和压缩体积成反比
比例越高 时间越长 体积越小
比例越低 时间越短 体积越大

解压、解包

语法:tar -xf 文件名称

[root@loaclhost]# tar -tf       etc.tar        //  t查看f文件名

解压

[root@localhost]# tar xf etc3.tar.xz
[root@localhost]# tar -xvf etc2.tar.bz2 -C /tmp     //-C重定向到//tmp目录

猜你喜欢

转载自blog.csdn.net/m0_48654420/article/details/107793638