【linux】centos7学习日记-文件查找

which 只能查询命令
    

[root@localhost ~]#which rpm

whereis    可以查询命令和配置文件的位置
  

 [root@localhost ~]#whereis rpm
    [root@localhost ~]#whereis passwd

whatis
  

    [root@localhost ~]#whatis rpm 和下面命令一样的效果
    [root@localhost ~]#man -f rpm

find

find [options] [path...] [expression] [action]
find 路径 条件 动作
    ===expression===
    

按文件名:
  

    [root@localhost ~]# find /etc -name "ifcfg-eth0"
    [root@localhost ~]# find /etc -iname "ifcfg-eth0" //-i忽略大小写
    [root@localhost ~]# find /etc -iname "ifcfg-eth*"

    按文件大小:
    

    [root@localhost ~]# find /etc -size +5M //大于5M
    [root@localhost ~]# find /etc -size 5M
    [root@localhost ~]# find /etc -size -5M
    [root@localhost ~]# find /etc -size +5M -ls //-ls找到的处理动作:查询详细信息

    按时间找(atime访问时间,mtime修改时间,ctime更改时间):
  

    [root@localhost ~]# find /etc -mtime +5 //修改时间超过5天
    [root@localhost ~]# find /etc -mtime 5 //修改时间等于5天
    [root@localhost ~]# find /etc -mtime -5 //修改时间5天以内

    按文件属主、属组找:
    

    [root@localhost ~]# find /home -user jack //属主是jack的文件
    [root@localhost ~]# find /home -group hr //属组是hr组的文件
    [root@localhost ~]# find /home -user jack -group hr
    [root@localhost ~]# find /home -user jack -a -group hr #-a 是而且的意思
    [root@localhost ~]# find /home -user jack -o -group hr #-o 是或者的意思
                            -not 是相反的意思

    按文件类型:
    

    [root@localhost ~]# find /dev -type f //f普通
    [root@localhost ~]# find /dev -type d //d目录
    [root@localhost ~]# find /dev -type l //l链接
    [root@localhost ~]# find /dev -type b //b块设备
    [root@localhost ~]# find /dev -type c //c字符设备
    [root@localhost ~]# find /dev -type s //s套接字
    [root@localhost ~]# find /dev -type p //p管道文件

    按文件权限:
    

    [root@localhost ~]# find . -perm 644 -ls
    [root@localhost ~]# find . -perm -644 -ls #等于或者大于644权限的文件
    [root@localhost ~]# find . -perm -600 -ls
    [root@localhost ~]# find . -perm -222 -ls

    按正则表达式:
  

    -regex pattern
    [root@localhost ~]# find /etc -regex '.*ifcfg-eth[0-9]'
    .* 任意多个字符
    [0-9] 任意一个数字
    + 前面字符一次或者多次
    \ 转义符  注:转义是 告诉shell不必特殊解释这个字符
    [root@localhost ~]# find /etc -regex '.*ifcfg-enp0s25'
    /etc/sysconfig/network-scripts/ifcfg-enp0s25
    找到后处理的动作 ACTIONS: (默认动作-print)
    -print: 显示
    -ls:类似ls -l的形式显示每一个文件的详细
    -delete: 删除匹配到的行
    -ok COMMAND {} \; 每一次操作都需要用户确认,{}表示引用找到的文件,是占位符
    -exec COMMAND {} \; 每次操作无需确认
    [root@localhost ~]# find /etc -name "ifcfg*"
    [root@localhost ~]# find /etc -name "ifcfg*" -print
    [root@localhost ~]# find /etc -name "ifcfg*" -ls
    [root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;
    [root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;
                                 find /etc -name “ifcfg*” -ok mv {} /tmp \;
    [root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;
    [root@localhost ~]# find /etc -name "ifcfg*" -delete

    当多个条件匹配时,如果使用-o 那个动作会去匹配最后一个条件 
    当多个条件匹配时,如果使用-o  每个条件前都加动作等同于使用(),每个条件都会执行动作
    当多个条件匹配时,如果使用-a     只有条件都满足才会执行动作
    无论使用-o 还是-a 只要有多个条件都()

  

 案例:
    

    [root@localhost ~]# mkdir dir1
    [root@localhost ~]# touch dir1/file{1..20}
    [root@localhost ~]# find /root/dir1 -name "file5"
    [root@localhost ~]# find /root/dir1 ! -name "file5"
    [root@localhost ~]# find /root/dir1 -name "file5" -o -name "file9"
    /root/dir1/file5
    /root/dir1/file9
    [root@localhost ~]# find /root/dir1 -name "file5" -o -name "file9" -ls
    1466515 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file9
    [root@localhost ~]# find /root/dir1 -name "file5" -ls -o -name "file9" -ls
    1466499 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file5
    1466515 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file9
    [root@localhost ~]# find /root/dir1 -name "file5" -ls -a -name "file9" -ls
    1466499 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file5
    1466515 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file9
    [root@localhost ~]# find /root/dir1 \( -name "file5" -o -name "file9" \) -ls
    1466499 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file5
    1466515 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file9
    [root@localhost ~]# find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \;
    removed ‘/root/dir1/file5’
    removed ‘/root/dir1/file9’


扩展知识:find结合xargs
    
    

    [root@localhost ~]# find . -name "localhost*.txt" |xargs rm -rf
    [root@localhost ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf     {} /var/tmp
    [root@localhost ~]# find . -type f -name "*.txt" |xargs -i cp {}     /tmp/
    加 -I 参数 需要事先指定替换字符
    加-i 参数直接用 {}就能代替管道之前的标准输出的内容

    小知识:更改主机名(永久修改) : hostnamectl set-hostname lcr

  

 打包压缩:
    

tar czf +打包去哪+打包名字 + 打包的内容
    c:建立新的文档
    f:指定存档或设备
    z:调用gzip的方式打包
    tar czf /mnt/file.tar.gz  ./* 
    /mnt/file1.tar.gz  这个位置可以指定打包后的路径(把打包的文件放到哪里).tar.gz  这个文件是文件的后缀

    解压:
    

     tar xvf  tar xf
    v:显示解压的过程
    x:解压的参数
    -C 指定解压路径
     tar xf etc.tar.gz
     tar xf /opt/Python-3.5.2.tar.xz -C  /usr/local/


    拓展->数据备份:
    

    [root@localhost ~]# yum -y install mariadb-server
    [root@localhost ~]# systemctl start mariadb
    [root@localhost ~]# mkdir /backup
    文件存放在: /var/lib/mysql
    案例1:mysql物理备份及恢复
    [root@localhost ~]# tar -cJf /backup/mysql.tar.xz /var/lib/mysql
    [root@localhost ~]# rm -rf /var/lib/mysql/*
    [root@localhost ~]# tar -xf /backup/mysql.tar.xz -C /
    案例2:mysql物理备份及恢复
    [root@localhost ~]# cd /var/lib/mysql
    [root@localhost mysql]# tar -cJf /backup/mysql.tar.xz *
    [root@localhost mysql]# tar -xf /backup/mysql.tar.xz -C /var/lib/mysql
    案例4:host A /etc (海量小文件) --------> host B /tmp
    常规方法:
    [root@localhost ~]# scp -r /etc 172.16.20.21:/tmp
    //此方法为远程备份

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

9527灯塔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值