程序员的知识教程库

网站首页 > 教程分享 正文

跟老韩学Linux,find指令or的实例

henian88 2025-04-29 00:34:16 教程分享 6 ℃ 0 评论

在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

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表