Linux命令之find(二)
接上一篇Linux命令之find(一)
(1).实例
1.列出当前目录下及子目录下所有的.txt文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [xf@xuexi ~]$ ls 1.txt 3.txt b.txt 公共 视频 文档 音乐 2.txt a.txt c.txt 模板 图片 下载 桌面 [xf@xuexi ~]$ find . -name "*.txt" ]./.cache/tracker/db-version.txt ./.cache/tracker/db-locale.txt ./.cache/tracker/parser-sha1.txt ./.cache/tracker/locale- for -miner-user-guides.txt ./.cache/tracker/locale- for -miner-apps.txt ./.cache/tracker/last-crawl.txt ./.cache/tracker/first-index.txt ./1.txt ./2.txt ./3.txt ./a.txt ./b.txt ./c.txt |
2.查找当前用户下更改时间在一天内或一天以上的.txt文件
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 29 30 31 32 33 34 35 | [xf@xuexi ~]$ find . -mtime -1 -name "*.txt" //一天内 ./.cache/tracker/locale- for -miner-user-guides.txt ./.cache/tracker/locale- for -miner-apps.txt ./1.txt ./2.txt ./3.txt ./a.txt ./b.txt ./c.txt [xf@xuexi ~]$ stat 1.txt 文件: "1.txt" 大小:0 块:0 IO 块:4096 普通空文件 设备:803h/2051d Inode:52016194 硬链接:1 权限:(0664/-rw-rw-r--) Uid:( 1000/ xf) Gid:( 1000/ xf) 环境:unconfined_u:object_r:user_home_t:s0 最近访问:2019-03-04 15:33:04.998746697 +0800 最近更改:2019-03-04 15:33:04.998746697 +0800 最近改动:2019-03-04 15:33:04.998746697 +0800 创建时间:- [xf@xuexi ~]$ find . -mtime +1 -name "*.txt" //一天以上 ./.cache/tracker/db-version.txt ./.cache/tracker/db-locale.txt ./.cache/tracker/parser-sha1.txt ./.cache/tracker/last-crawl.txt ./.cache/tracker/first-index.txt [xf@xuexi ~]$ stat ./.cache/tracker/db-version.txt 文件: "./.cache/tracker/db-version.txt" 大小:2 块:8 IO 块:4096 普通文件 设备:803h/2051d Inode:35360 硬链接:1 权限:(0644/-rw-r--r--) Uid:( 1000/ xf) Gid:( 1000/ xf) 环境:unconfined_u:object_r:cache_home_t:s0 最近访问:2019-03-04 10:05:12.993515697 +0800 最近更改:2018-09-06 11:43:45.770953885 +0800 最近改动:2018-09-06 11:43:45.770953885 +0800 创建时间:- |
3.对查找内容执行相应命令
1 2 3 4 5 6 7 8 9 10 11 12 13 | exi ~]$ touch {1,2,3}.back [xf@xuexi ~]$ find . -name "*.back" ./1.back ./2.back ./3.back [xf@xuexi ~]$ find . -name "*.back" -exec ls -l {} \; -rw-rw-r--. 1 xf xf 0 3月 4 18:39 ./1.back -rw-rw-r--. 1 xf xf 0 3月 4 18:39 ./2.back -rw-rw-r--. 1 xf xf 0 3月 4 18:39 ./3.back [xf@xuexi ~]$ ls -l ./1.back;ls -l ./2.back;ls -l ./3.back; //拆分执行 -rw-rw-r--. 1 xf xf 0 3月 4 18:39 ./1.back -rw-rw-r--. 1 xf xf 0 3月 4 18:39 ./2.back -rw-rw-r--. 1 xf xf 0 3月 4 18:39 ./3.back |
4.与xargs -i联合使用,find的结果值代替其中的{}
1 2 3 4 | [xf@xuexi ~]$ find . -name "*.back" | xargs -i ls -l {} -rw-rw-r--. 1 xf xf 0 3月 4 18:39 ./1.back -rw-rw-r--. 1 xf xf 0 3月 4 18:39 ./2.back -rw-rw-r--. 1 xf xf 0 3月 4 18:39 ./3.back |
5.查找多个类型文件
1 2 3 4 5 6 7 8 9 | [xf@xuexi ~]$ touch a.pdf [xf@xuexi ~]$ find . -name "*.back" -o -name "*.pdf" //-o或者,也可写成-or ./1.back ./2.back ./3.back ./a.pdf [xf@xuexi ~]$ find . -size +20k -a -size -30k //-a并且,也可写成-and ./.config/pulse/93c68f6a5c7b462db558e8e995b4212a-card-database.tdb ./.local/share/evolution/addressbook/system/contacts.db |
6.按照权限查找
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [root@xuexi ~]# mkdir aaa [root@xuexi ~]# mkdir bbb [root@xuexi ~]# touch a.sh [root@xuexi ~]# chmod 777 aaa [root@xuexi ~]# chmod 1777 bbb [root@xuexi ~]# chmod 4777 a.sh [root@xuexi ~]# find /root/ -perm 777 //权限为777的所有文件或文件夹 /root/aaa [root@xuexi ~]# find /root/ -perm -777 //权限至少为777的所有文件或文件夹 /root/aaa /root/bbb /root/a.sh [root@xuexi ~]# find /root -type f -perm -777 //权限至少为777的所有普通文件。-type f指定普通文件 /root/a.sh |
7.查找目录深度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [root@xuexi ~]# find /bin -maxdepth 1 -perm -755 //bin后没有/,那么bin为第一层 /bin [root@xuexi ~]# find /bin/ -maxdepth 1 -perm -755 //bin后有/,则下一层为第一层 /bin/cp /bin/lua /bin/captoinfo /bin/csplit /bin/clear /bin/cut /bin/luac /bin/infocmp /bin/fmt /bin/unxz //比较多,只截取部分 |
8.查找所有属于用户xf的文件,并复制到指定目录
1 2 3 4 5 6 7 8 9 10 | [root@xuexi ~]# mkdir findresults [root@xuexi ~]# ls 1.txt core.3160 initial-setup-ks.cfg 模板 图片 下载 桌面 anaconda-ks.cfg findresults 公共 视频 文档 音乐 [root@xuexi ~]# find / -user xf -exec cp -a {} /root/findresults/ \; find: ‘/proc/11379/task/11379/fd/5’: 没有那个文件或目录 find: ‘/proc/11379/task/11379/fdinfo/5’: 没有那个文件或目录 find: ‘/proc/11379/fd/6’: 没有那个文件或目录 find: ‘/proc/11379/fdinfo/6’: 没有那个文件或目录 cp: 无法以目录 "/home/xf" 来覆盖非目录 "/root/findresults/xf" |
上面的命令是没有问题的,这里涉及到同一目录下文件与文件、文件夹与文件夹,文件与文件夹都不能同名。Windows文件与文件夹能够同名是因为文件有扩展名,否则也不能重名。
这时我们可以临时修改一下文件名,以便可以继续执行
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 | [root@xuexi ~]# mv / var /spool/mail/xf / var /spool/mail/xf.mail //临时修改一下文件名 [root@xuexi ~]# rm -rf /root/findresults/* //清空原来的复制文件 [root@xuexi ~]# find / -user xf -exec cp -a {} /root/findresults/ \; //重新执行 find: ‘/proc/12438/task/12438/fd/5’: 没有那个文件或目录 find: ‘/proc/12438/task/12438/fdinfo/5’: 没有那个文件或目录 find: ‘/proc/12438/fd/6’: 没有那个文件或目录 find: ‘/proc/12438/fdinfo/6’: 没有那个文件或目录 cp: "/root/findresults/.esd-1000" 与 "/root/findresults/.esd-1000" 为同一文件 cp: "/root/findresults/.mozilla" 与 "/root/findresults/.mozilla" 为同一文件 cp: "/root/findresults/.bash_logout" 与 "/root/findresults/.bash_logout" 为同一文件 cp: "/root/findresults/.bash_profile" 与 "/root/findresults/.bash_profile" 为同一文件 cp: "/root/findresults/.bashrc" 与 "/root/findresults/.bashrc" 为同一文件 cp: "/root/findresults/.cache" 与 "/root/findresults/.cache" 为同一文件 cp: "/root/findresults/.changed" 与 "/root/findresults/.changed" 为同一文件 cp: "/root/findresults/.dbus" 与 "/root/findresults/.dbus" 为同一文件 cp: "/root/findresults/.config" 与 "/root/findresults/.config" 为同一文件 cp: "/root/findresults/.ICEauthority" 与 "/root/findresults/.ICEauthority" 为同一文件 cp: "/root/findresults/.local" 与 "/root/findresults/.local" 为同一文件 cp: "/root/findresults/.esd_auth" 与 "/root/findresults/.esd_auth" 为同一文件 cp: "/root/findresults/.bash_history" 与 "/root/findresults/.bash_history" 为同一文件 cp: "/root/findresults/.lesshst" 与 "/root/findresults/.lesshst" 为同一文件 cp: "/root/findresults/.viminfo" 与 "/root/findresults/.viminfo" 为同一文件 [root@xuexi ~]# mv / var /spool/mail/xf.mail / var /spool/mail/xf //最后再改回来 [root@xuexi ~]# ls / var /spool/mail/ root rpc xf |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性