网站首页 > 教程分享 正文
在find命令中,"or"可以用来指定多个匹配条件,可以根据不同的需求使用不同的"or"语法。下面是5个使用"or"语法的实例:
1、查找名字匹配多个模式的文件
查找所有名字匹配"example1.txt"或"example2.txt"的文件:
find . -type f \( -name "example1.txt" -o -name "example2.txt" \)
查看当前目录下的文件
[root@hanyw-pcs001 ~]# ll
total 12
-rw-r--r-- 1 root root 4 Aug 9 23:52 1.py
-rw-r--r-- 1 root root 835 Jul 31 02:04 1.sh
-rw-------. 1 root root 1505 Apr 28 2021 anaconda-ks.cfg
查找1.py或者1.sh的文件,如下。
[root@hanyw-pcs001 ~]# find . -type f \( -name "1.sh" -o -name "1.py" \)
./1.sh
./1.py
2、查找指定目录下多个类型的文件
查找指定目录"/var/log"下所有扩展名为".log"或".txt"的文件:
find /var/log \( -name "*.log" -o -name "*.txt" \)
此处查找以.sh和.py结尾的文件。
[root@hanyw-pcs001 ~]# find . -type f \( -name "*.sh" -o -name "*.py" \)
./1.sh
./1.py
./2.py
3、查找匹配多个后缀名的文件
查找当前目录及其子目录下所有扩展名为".jpg"或".png"的文件:
find . -type f \( -name "*.jpg" -o -name "*.png" \)
此处查找以.sh和.py结尾的文件。
[root@hanyw-pcs001 ~]# ll
total 12
-rw-r--r-- 1 root root 4 Aug 9 23:52 1.py
-rw-r--r-- 1 root root 835 Jul 31 02:04 1.sh
-rw-r--r-- 1 root root 0 Aug 9 23:54 2.py
-rw-------. 1 root root 1505 Apr 28 2021 anaconda-ks.cfg
[root@hanyw-pcs001 ~]# mkdir py
[root@hanyw-pcs001 ~]# cp -av *.py py/
‘1.py’ -> ‘py/1.py’
‘2.py’ -> ‘py/2.py’
[root@hanyw-pcs001 ~]# ls -lhrt
total 12K
-rw-------. 1 root root 1.5K Apr 28 2021 anaconda-ks.cfg
-rw-r--r-- 1 root root 835 Jul 31 02:04 1.sh
-rw-r--r-- 1 root root 4 Aug 9 23:52 1.py
-rw-r--r-- 1 root root 0 Aug 9 23:54 2.py
drwxr-xr-x 2 root root 30 Aug 9 23:55 py
[root@hanyw-pcs001 ~]# tree
-bash: tree: command not found
[root@hanyw-pcs001 ~]# yum -y install tree
Loaded plugins: fastestmirror
Determining fastest mirrors
epel/x86_64/metalink | 9.4 kB 00:00:00
* base: mirrors.bfsu.edu.cn
* epel: mirror.lzu.edu.cn
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.bfsu.edu.cn
base | 3.6 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/3): epel/x86_64/updateinfo | 1.0 MB 00:00:03
(2/3): epel/x86_64/primary_db | 7.0 MB 00:00:35
(3/3): updates/7/x86_64/primary_db | 22 MB 00:01:36
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================
Package Arch Version Repository Size
========================================================================================================================
Installing:
tree x86_64 1.6.0-10.el7 base 46 k
Transaction Summary
========================================================================================================================
Install 1 Package
Total download size: 46 k
Installed size: 87 k
Downloading packages:
tree-1.6.0-10.el7.x86_64.rpm | 46 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : tree-1.6.0-10.el7.x86_64 1/1
Verifying : tree-1.6.0-10.el7.x86_64 1/1
Installed:
tree.x86_64 0:1.6.0-10.el7
Complete!
[root@hanyw-pcs001 ~]# tree
.
├── 1.py
├── 1.sh
├── 2.py
├── anaconda-ks.cfg
└── py
├── 1.py
└── 2.py
1 directory, 6 files
[root@hanyw-pcs001 ~]#
[root@hanyw-pcs001 ~]# find . -type f \( -name "*.sh" -o -name "*.py" \)
./1.sh
./1.py
./2.py
./py/1.py
./py/2.py
4、查找大小区间在指定范围内的文件
查找当前目录及其子目录下大小在1M~5M之间的所有文件:
find . -type f -size +1M -size -5M
这里先创建测试文件
[root@hanyw-pcs001 ~]# dd if=/dev/zero of=./1.txt bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.000863275 s, 1.2 GB/s
[root@hanyw-pcs001 ~]# ls -lhrt
total 1.1M
-rw-------. 1 root root 1.5K Apr 28 2021 anaconda-ks.cfg
-rw-r--r-- 1 root root 835 Jul 31 02:04 1.sh
-rw-r--r-- 1 root root 4 Aug 9 23:52 1.py
-rw-r--r-- 1 root root 0 Aug 9 23:54 2.py
drwxr-xr-x 2 root root 30 Aug 9 23:55 py
-rw-r--r-- 1 root root 1.0M Aug 10 00:03 1.txt
[root@hanyw-pcs001 ~]# dd if=/dev/zero of=./py/test.py bs=5M count=1
1+0 records in
1+0 records out
5242880 bytes (5.2 MB) copied, 0.00329269 s, 1.6 GB/s
[root@hanyw-pcs001 ~]# tree
.
├── 1.py
├── 1.sh
├── 1.txt
├── 2.py
├── anaconda-ks.cfg
└── py
├── 1.py
├── 2.py
└── test.py
1 directory, 8 files
[root@hanyw-pcs001 ~]#
[root@hanyw-pcs001 ~]# find . -type f -size +1M -size -5M
[root@hanyw-pcs001 ~]#
[root@hanyw-pcs001 ~]# find . -type f -size +1M -size -5M
[root@hanyw-pcs001 ~]# dd if=/dev/zero of=./py/test.py bs=3M count=1
1+0 records in
1+0 records out
3145728 bytes (3.1 MB) copied, 0.00481226 s, 654 MB/s
[root@hanyw-pcs001 ~]#
[root@hanyw-pcs001 ~]# find . -type f -size +1M -size -5M
./py/test.py
5、按照文件的创建时间或修改时间查找文件
查找当前目录及其子目录下最近5天内修改过或创建过的所有文件:
find . -type f \( -mtime -5 -o -ctime -5 \)
其中,"-mtime -5"表示最近5天内被修改过的文件,"-ctime -5"表示最近5天内被创建过的文件。
[root@hanyw-pcs001 ~]# find . -type f \( -mtime -5 -ctime -5 \)
./.bash_history
./.viminfo
./1.py
./2.py
./py/1.py
./py/2.py
./py/test.py
./1.txt
- 上一篇: linux命令之find和locate深度对比分析
- 下一篇: Linux常用查找文件方法总结
猜你喜欢
- 2025-04-29 3种方法找出哪个进程在监听一个特定的端口
- 2025-04-29 linux搜索命令
- 2025-04-29 Linux使用pidof命令来快速查找进程id
- 2025-04-29 [Linux Shell]简单的文件查找脚本
- 2025-04-29 如何在 Linux 中查找最大的文件?
- 2025-04-29 Linux上使用的文本内容搜索工具--recollgui | 统信 | 麒麟 | 方德
- 2025-04-29 Linux实战之:ldd查找缺失依赖
- 2025-04-29 在 Linux 中查找 IP 地址的 3 种简单方法
- 2025-04-29 在 Linux 中查找系统信息
- 2025-04-29 在linux目录行中找空目录
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- css导航条 (66)
- sqlinsert (63)
- js提交表单 (60)
- param (62)
- parentelement (65)
- jquery分享 (62)
- check约束 (64)
- curl_init (68)
- sql if语句 (69)
- import (66)
- chmod文件夹 (71)
- clearinterval (71)
- pythonrange (62)
- 数组长度 (61)
- javafx (59)
- 全局消息钩子 (64)
- sort排序 (62)
- jdbc (69)
- php网页源码 (59)
- assert h (69)
- httpclientjar (60)
- postgresql conf (59)
- winform开发 (59)
- mysql数字类型 (71)
- drawimage (61)
本文暂时没有评论,来添加一个吧(●'◡'●)