Linux系统管理:文件查找命令find

图片[1]-Linux系统管理:文件查找命令find-不念博客

文件查找命令find

有些时候,我们可能会忘了某个文件所在的位置,此时就需要通过find来查找。

还有些时候,我想要找到,某个目录下,所有小于1k的文件。……

还还还有些时候,我们想找到,某个目录下,所有以.sh结尾的脚本。

Linux系统中的find命令在查找文件时非常有用而且方便。

find命令的语法

命令   路径     选项(按xx查找)       表达式         动作
find[path...][options]           [expression][action]
查找   地区     小姐姐               18           约...
find     /etc/       -name "*conf*" -a -name "*config*"

示例:

#按文件名查找


-name
-iname   忽略大小写


如
[root@localhost ~]# touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1} 创建文件


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


[root@localhost ~]# find /etc/ -iname "ifcfg-eth1"     -i 忽略大小写
/etc/sysconfig/network-scripts/ifcfg-eth1
/etc/sysconfig/network-scripts/IFCFG-ETH1


[root@localhost ~]# find /etc/ -iname "ifcfg-eth*"     * 查找所有名字以ifcfg-eth开头的文件
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/ifcfg-eth1
/etc/sysconfig/network-scripts/IFCFG-ETH1


[root@localhost ~]# find / -name "xxx*"
/root/xxx
/root/xxx1
/var/spool/mail/xxx
/tmp/xxx1
/home/xxx
[root@localhost ~]# find / -name *"xxx*"               两个* 查找/下所有名字包含xxx的文件
/root/xxx
/root/xxx1
/var/spool/mail/xxx
/tmp/xxx1
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/crypto/qat/qat_c3xxx
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/crypto/qat/qat_c3xxx/qat_c3xxx.ko.xz
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/crypto/qat/qat_c3xxxvf
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/crypto/qat/qat_c3xxxvf/qat_c3xxxvf.ko.xz
......

# 按照文件类型找


find 路径 -type (f d l等)
-type


f 普通文件
d 目录
l 链接
b 块设备文件   就那些磁盘 很少
c 字符设备文件
s 安全套接字文件
p 管道文件
等等


如:
find / -type f
find /etc -type f


-a 和 并且
-o 或者
!取反(非)


[root@localhost ~]# find / -type d -name 'conf'     查找根下d类型,名字conf的文件
/proc/sys/net/ipv4/conf
/proc/sys/net/ipv6/conf


[root@localhost ~]# find /etc/ -name "*conf*" -a -name "*config*"     /etc下 名字有这两种的
/etc/chkconfig.d
/etc/systemd/system/multi-user.target.wants/rhel-configure.service
/etc/pki/nss-legacy/nss-rhel7.config
/etc/pm/config.d
......




[root@localhost ~]# find /etc/ -name "*conf*" -o -name "*config*"   比上面出来多得多
/etc/resolv.conf
/etc/chkconfig.d
/etc/libaudit.conf
/etc/security/pwquality.conf
/etc/security/access.conf
/etc/security/chroot.conf
......


[root@localhost ~]# find /etc/ -type d ! -name "conf"     /etc/下名字不带conf的目录 出来一大堆
......


[root@localhost ~]# find /etc -type f ! -name "*config*"   /etc下所有不带"*config*"的文件
# 按文件大小找


find 路径 -size ±/number
+     大于
-     小于
number 等于


[root@localhost ~]# find /root -size 1k     查找/root下1k的文件
/root
/root/.bash_logout
/root/.bash_profile
...
#看大小
[root@localhost ~]# du -sh /root/.bash_logout   由于1k占用1个block,出来的是4k。(计算机最小单位1个block是4k)
4.0K/root/.bash_logout                   搜11k的话 出来是12k   所以用+-多一些




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


[root@localhost ~]# ll -h /etc/udev/hwdb.bin     看一下大小
-r--r--r--. 1 root root 7.6M Aug 17 15:33 /etc/udev/hwdb.bin

小练习

# 在/opt下建1000个文件 然后一次全部删掉 要求用find命令  
[root@localhost opt]# touch {1..1000}

find /opt -size -1k |xargs rm -fr

find -name '{1..1000}' |xargs rm -fr {1..1000}   前面的其实没用   认的后面的 ~~~~~
# 按文件的用户查找


find 路径 选项 xxx
-user
-group
-nouser
-nogroup


[root@localhost ~]# find /home -user xxx
/home/xxx
/home/xxx/.bash_logout
/home/xxx/.bash_profile
/home/xxx/.bashrc
/home/xxx/.bash_history
/home/xxx/test1
[root@localhost ~]# cd /home
[root@localhost home]# ll
total 0
drwx------. 2 xxx xxx      96 Aug 25 12:14 xxx
drwx------. 2 zls students 62 Aug 18 11:34 zls
[root@localhost home]# find /home -user xxx -group xxx
/home/xxx
/home/xxx/.bash_logout
/home/xxx/.bash_profile
/home/xxx/.bashrc
/home/xxx/.bash_history
/home/xxx/test1
[root@localhost home]# find -user xxx -group xxx
./xxx
./xxx/.bash_logout
./xxx/.bash_profile
./xxx/.bashrc
./xxx/.bash_history
./xxx/test1
# 按时间查找


stat里面看下
[root@localhost opt]# stat /opt/1000.txt
File: ‘/opt/1000.txt’
Size: 0         Blocks: 0         IO Block: 4096   regular empty file
Device: 803h/2051dInode: 17159663   Links: 1
Access: (0644/-rw-r--r--) Uid: (    0/   root)   Gid: (    0/   root)
Context: unconfined_u:object_r:usr_t:s0
Access: 2022-08-25 11:24:34.827131101 +0800     # Access:接近、进入、访问
Modify: 2022-08-25 11:24:34.827131101 +0800
Change: 2022-08-25 11:24:34.827131101 +0800


-atime 文件访问时间
-mtime 文件内容创建、修改时间
-ctime 文件属性修改时间


±/=Num
7   查找第7天的文件
+7   查找7天以前的文件(不包含今天,不包含第7天)
-7   查找最近7天的文件,不建议使用(包含当天的文件)


如:
# find /opt -mtime -1
# 按文件的权限查找
find 路径 -perm (权限值)


精确匹配
find / -perm 022


模糊匹配 包含-后面的对应权限
find / -perm -022


find . -perm 644 -ls
# 按照深度查找
find 路径 -maxdepth 10000 显示全部


//打印目录的时候,只打印1级目录(按深度查找) 
例
[root@db04 ~]# find /home/ -maxdepth 1 -type d  -user zls

find处理动作

当查找到一个文件后, 需要对文件进行如何处理, 默认动作 -print

动作	        含义
-print	  打印查找到的内容(默认)
-ls	      以长格式显示找到东西的详细信息
-delete	  删除查找到的文件(仅能删除空目录)
-ok	      后面跟自定义shell命令(会提示是否操作)
-exec	  后面跟自定义shell命令(标准写法-exec \;)


-exec { \ ;
© 版权声明
THE END
喜欢就支持一下吧
点赞146赞赏 分享
评论 抢沙发
头像
欢迎光临不念博客,留下您的想法和建议,祝您有愉快的一天~
提交
头像

昵称

取消
昵称代码图片

    暂无评论内容