文件查找-find

find 命令的基本语法如下

命令 路径 选项 表达式 动作
find [path...] [options] [expression] [action]
查找 地区 小姐姐 18 约...
视频 路径 日韩 无码
# 举个栗子
[root@localhost ~]# find /etc/ -name '*.sh'
/etc/profile.d/colorgrep.sh
/etc/profile.d/colorls.sh
/etc/profile.d/which2.sh
/etc/profile.d/less.sh
/etc/profile.d/256term.sh
/etc/profile.d/lang.sh
/etc/profile.d/vim.sh
/etc/dhcp/dhclient-exit-hooks.d/azure-cloud.sh
/etc/kernel/postinst.d/51-dracut-rescue-postinst.sh

根据文件名查找文件

//创建文件
touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}

//查找/etc目录下包含ifcfg-eth0名称的文件
[root@zls ~]# find /etc -name "ifcfg-eth1"

//-i 忽略大小写
[root@zls ~]# find /etc -iname "ifcfg-eth1"
//查找/etc目录下包含ifcfg-eth名称所有文件
[root@zls ~]# find /etc/ -name "ifcfg-eth*"
[root@zls ~]# find /etc -iname "ifcfg-eth*"

//查找包含eth的文件
[root@localhost opt]# find /opt/ -name '*eth*'
[root@localhost opt]# find /opt/ -iname '*eth*'


find /root/dir1 ! (-name 'file5' -o -name 'file9' )

根据文件的大小查找

-size n[cwbkMG]
              `b'    block

              `c'    bytes 字节

              `w'    words 单词

              `k'    kb

              `M'    MB

              `G'    GB

# 查找大于5M的文件
[root@localhost ~]# find /etc/ -size +5M
/etc/udev/hwdb.bin

# 查找等于5M的文件
[root@localhost ~]# find /etc/ -size 5M

# 查找小于5M的文件
[root@localhost ~]# find /etc/ -size -5M

## 动作,查看
[root@localhost ~]# find /etc/ -size +5M -ls

## 动作,删除
[root@localhost ~]# find /tmp/ -size +5M -delete

## 集合name
[root@localhost ~]# find /etc/ -size -5M -name '*.sh'

## 需求:在/etc 找到 .sh  和 .conf 结尾的 小于5M的文件
[root@localhost ~]# find /etc/ -size -5M -name '*.sh' -o -name '*.db'

## 需求:在/etc/ 找到 大于3M 小于5M 名字以.sh结尾和.conf结尾
[root@localhost ~]# find /etc/ \( -size +3M -a -size -6M \) -a \( -name '*.sh' -o -name '*.conf' \)

## 需求:在/etc 目录下找到小于5M 并且 文件名不是以 .sh 结尾 或者 不是以 .db结尾
[root@localhost ~]# find /etc/ -size -5M ! \( -name '*.sh' -o -name '*.db' \)

根据文件类型查找

f:文件
d:目录
l:软连接
s:socket
p:管道文件
b:块设备
c:字符设备

find / -type f
find / -type d
find / -type b

find / -type f -ls

find / -type f|head -5|xargs ls -l

根据日期查找

-mtime

## 找七天之前的文件,(不包含今天)
[root@localhost ~]# find /opt/ -mtime +7 -name '*.txt'

## 找最近七天的文件
[root@localhost ~]# find /opt/ -mtime -7 -name '*.txt'

## 找第七天的(不包含今天)
[root@localhost ~]# find /opt/ -mtime 7 -name '*.txt'


## 企业需求:只保留N天的备份,其余的都删除
[root@localhost ~]#  find /opt/ ! -mtime -7 -name '*.txt' -delete
[root@localhost ~]#  find /opt/ ! -mtime -30 -name '*.txt' -delete

条件语句

-a:and 和,并且
-o:or 或者
!:取反

动作

-ls
-delete
-exec

find的动作很少用。

find-根据用户查找文件

-user:查找属主是X个用户
-group:查找属组是X个组
-nouser:查找没有用户
-nogroup:查找没有组

[root@localhost opt]# find ./ -user zls|xargs ls -l
-rw-r--r--. 1 zls qiandao 0 Apr 16 00:36 ./file1
-rw-r--r--. 1 zls zls     0 Apr 16 00:36 ./file3

[root@localhost opt]# find ./ -user zls -group qiandao
./file1

[root@localhost opt]# find ./ -user zls -o -group qiandao
./file1
./file3
./file4

find-根据深度查找

-maxdepth level

[root@localhost ~]# find /etc/ -maxdepth 2 -type f -name '*.conf'

find-根据文件权限查找

-perm

## 精确查找
[root@localhost opt]# find /opt/ -perm 644

## 包含指定权限的文件
root@localhost opt]# find /opt/ -perm -222

-222:and
/222:or

属主 属组 其它用户
- 1    2     3
  x    w     wx
  r-x  rwx   rwx
  
/ 1    2     3
  x    w     wx
  r-x  rwx   rwx
  rw   wx    rwx
  r-x  r     r

find-动作

-print:打印出查找的内容,find默认就会打印
-ls:查看找出的文件相信信息
[root@localhost ~]# find /opt/ ! -perm /222 -ls

-delete
[root@localhost opt]# find /opt/ -type d ! -name 'opt'|xargs rm -fr


-ok
语法: -ok \;
-exec
语法: -exec \;

## 拷贝找到的文件到/tmp下
[root@localhost opt]# find /opt/ -mtime +5 |xargs cp -t /tmp/
[root@localhost opt]# find /opt/ -mtime +5 -exec cp {} /tmp/ \;
[root@localhost opt]# find /opt/ -mtime +5 |xargs -I {} cp {} /tmp/
[root@localhost opt]# find /opt/ -name '*.txt' -ok  cp {} /tmp/ \;
< cp ... /opt/2020-04-01_file.txt > ? y
< cp ... /opt/2020-04-02_file.txt > ? y
< cp ... /opt/2020-04-03_file.txt > ? y
< cp ... /opt/2020-04-04_file.txt > ? y
< cp ... /opt/2020-04-05_file.txt > ? y
< cp ... /opt/2020-04-06_file.txt > ? y
< cp ... /opt/2020-04-07_file.txt > ? y
< cp ... /opt/2020-04-08_file.txt > ? y
< cp ... /opt/2020-04-09_file.txt > ? y
< cp ... /opt/2020-04-10_file.txt > ? y
< cp ... /opt/2020-04-11_file.txt > ? y
< cp ... /opt/2020-04-12_file.txt > ? y
< cp ... /opt/2020-04-13_file.txt > ? y
< cp ... /opt/2020-04-14_file.txt > ? y
< cp ... /opt/2020-04-15_file.txt > ? y
< cp ... /opt/2020-04-16_file.txt > ? y
< cp ... /opt/zls.txt > ? y


## find 结合xargs
#拷贝
find  / -type f |xargs cp -t /tmp
#查看
find  / -type f |xargs ls -l
#替换
find  / -type f |xargs sed -i 's###g'
#移动
find  / -type f |xargs mv -t /tmp
#删除
find  / -type f |xargs rm -fr

find 总结

## find
语法:find [路径] [选项] [参数] [动作]

## find选项:

# 按时间查找
-mtime
-ctime
-atime

# 按大小查找
-size

# 按用户查找
-user

# 按名字查找
-name

# 按权限查找
-perm

# 按文件类型查找
-type

# 按深度查找
-maxdepth

# 按组查找
-group

# 按没有属主的查找
-nouser

# 按没有属组的查找
-nogroup

## 动作
-ls
-exec
-ok
-delete
-print


## find 配合xargs

## 条件语句
-a
-o
!

猜你喜欢

转载自www.cnblogs.com/mdddm/p/12913909.html