当前位置: 首页 > news >正文

Linux文件查找find

目录

前言

查找命令

命令演示

1.which:命令查找

2.locate命令

3.find命令(主要使用这个命令进行查找文件)

1)语法

2)find的用法介绍

按文件名查找

手动写入指定大小数据到文件内,介绍一下dd命令。

按文件大小查找

指定查找的目录深度

按文件属主、属组查找

 按文件类型查找文件

按文件权限查找文件

测试不同处理动作

总结


前言

        这篇文章将带领大家学习Linux中的文件查找,通过这篇文章学会如何在Linux中查找文件,主要学习find命令,通过find命令,按照一定的条件查找相应的文件。接下来进入学习吧。


查找命令

  •         which:命令查找。
  •         find:文件查找,针对文件名。
  •         locate:文件查找,依赖数据库。

下面来针对这三个命令进行介绍。


命令演示

1.which:命令查找

命令:which  需要查找的命令

代码如下(示例):

//查找ls命令
[root@localhost ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
//查找cp命令
[root@localhost ~]# which cp
alias cp='cp -i'
	/usr/bin/cp
//查找rm命令
[root@localhost ~]# which rm
alias rm='rm -i'
	/usr/bin/rm
//查找touch命令
[root@localhost ~]# which touch 
/usr/bin/touch

        可以看到比如查找ls,cp,rm等命令的时候会出现两行内容,alias代表着别称比如查找ls命令的输出内容的第一行“alias ls='ls --color=auto'”表示ls是ls --color=auto的别名。ls和ls --color=auto的效果是一样的,同样我们也可以自己命名一些命令的别称,通过alias命令,下面来演示。

代码如下(示例):

[root@localhost ~]# chakan
bash: chakan: 未找到命令...
[root@localhost ~]# alias chakan='ls'
[root@localhost ~]# chakan
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@localhost ~]# 

        在创建别名之前,chakan命令是找不到的,使用alias命令让chakan命令成为ls的别名,再输入chakan就被系统当做ls来运行了,是很有意思的实验,大家可以自己试试给一些命令创建别名。

2.locate命令

命令:locate  需要查找的文件

代码如下(示例):

//查找file1文件,发现并没有找到
[root@localhost ~]# locate  file1
//创建一个file1文件
[root@localhost ~]# touch /test/file1
//发现依然没有找到file1
[root@localhost ~]# locate file1
//更新数据库
[root@localhost ~]# updatedb
//找到了file1
[root@localhost ~]# locate file1
/test/file1

        locate是依赖于数据库查找文件,起初没有file1文件,但是当创建好file1文件后因为数据库没有更新,所以依然没有找到file1文件,重启计算机或者用updatedb命令来更新数据库,才能用locate命令查找到。

3.find命令(主要使用这个命令进行查找文件)

1)语法

命令:find [path...] [options] [expression] [ation]
           命令  路径      选项          表达式       动作

2)find的用法介绍

  • 按照文件名查找:find  路径  -name  文件名  动作
  • 按文件大小查找:find   路径    -size   文件大小范围   动作
  • 指定查找的目录级别:find  路径  -maxdepth   -a  -name  文件名   动作 //-a是and的意思
  • 按文件属主、属组查找:find 路径 -user或者-group  用户名或者组名  动作
  • 按文件类型查找:find  路径 -type  文件类型  动作
  • 按文件权限查找:find  路径   -perm  权限   动作

处理动作

  • -print:默认的处理动作,显示至屏幕
  • -ls:类似于对查找到的文件执行“ls -l”命令
  • -fls file:查找到的所有文件的长格式信息保存至指定文件中,相当于 -ls > file
  • -delete:删除查找到的文件,慎用!
  • -ok COMMAND {} ; 对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会交互式要求用户确认
  • -exec COMMAND {} ; 对查找到的每个文件执行由COMMAND指定的命令
  • {}: 用于引用查找到的文件名称自身

下面示例的动作都以ls为例演示。

按文件名查找

命令:find  路径   -name或者-iname    文件名  动作

-i忽略大小写

代码如下(示例):

//在test目录下查找file3文件并且执行ls动作
[root@localhost ~]# find /test   -name file3 -ls
51844073    0 -rwxrwxrwx   1 sure2   sure1   0 10月 28 10:54 /test/content1/content1-2/content1-3/file3
//在test目录下查找file8文件并且执行ls动作
[root@localhost ~]# find /test   -name file8 -ls
51844094    0 -rw-r--r--   1 sure3   sure5   0 10月 28 10:54 /test/file8
//在test目录下查找file9文件并且执行ls动作
[root@localhost ~]# find /test   -name file9 -ls
51844095    0 -rw-r--r--   1 root    root      0 10月 28 10:54 /test/file9
//在test目录下查找file10文件并且执行ls动作
[root@localhost ~]# find /test   -name file10 -ls
51843352    0 -rw-r--r--   1 root    root    0 10月 28 10:54 /test/file10
//查找FILE2文件查不到文件
[root@localhost ~]# find /test/ -name FILE2
//使用-iname查找FILE2,查到了文件file2,-iname忽略文件名大小写
[root@localhost ~]# find /test/ -iname FILE2
/test/content1/content1-2/content1-3/file2

如代码所示,查找到指定文件后执行ls动作

手动写入指定大小数据到文件内,介绍一下dd命令。

命令:dd  if=/dev/zero   of=/test/file8  bs1M  count=15

  • if:指定从哪里读
  • of:指定写到哪里
  • bs:一次读取和写入多少字节,单位默认为字节,可指定单位为k(KB),M(MB),G(GB)
  • count:操作次数

向不同的文件写入了不同大小的数据。

代码如下(示例):

[root@localhost ~]# dd if=/dev/zero  of=/test/file8 bs=1M count=15
记录了15+0 的读入
记录了15+0 的写出
15728640字节(16 MB)已复制,0.0377968 秒,416 MB/秒
[root@localhost ~]# dd if=/dev/zero  of=/test/file9 bs=1M count=3
记录了3+0 的读入
记录了3+0 的写出
3145728字节(3.1 MB)已复制,0.0091718 秒,343 MB/秒
[root@localhost ~]# dd if=/dev/zero  of=/test/file10 bs=1M count=20
记录了20+0 的读入
记录了20+0 的写出
20971520字节(21 MB)已复制,0.104728 秒,200 MB/秒
[root@localhost ~]# dd if=/dev/zero  of=/test/content1/content1-2/content1-3/file1 bs=1M count=20
记录了20+0 的读入
记录了20+0 的写出
20971520字节(21 MB)已复制,0.963111 秒,21.8 MB/秒
[root@localhost ~]# dd if=/dev/zero  of=/test/content1/content1-2/content1-3/file2 bs=1M count=2
记录了2+0 的读入
记录了2+0 的写出
2097152字节(2.1 MB)已复制,0.14584 秒,14.4 MB/秒
[root@localhost ~]# dd if=/dev/zero  of=/test/content1/content1-2/content1-3/file3 bs=1M count=10
记录了10+0 的读入
记录了10+0 的写出
10485760字节(10 MB)已复制,1.32703 秒,7.9 MB/秒
[root@localhost ~]# dd if=/dev/zero of=/test/content1/content1-2/file4 bs=1M count=5
记录了5+0 的读入
记录了5+0 的写出
5242880字节(5.2 MB)已复制,0.37709 秒,13.9 MB/秒
[root@localhost ~]# dd if=/dev/zero of=/test/content1/content1-2/file5 bs=1M count=5
记录了5+0 的读入
记录了5+0 的写出
5242880字节(5.2 MB)已复制,0.344967 秒,15.2 MB/秒

按文件大小查找

命令:find 路径  -size  大小范围  动作

代码如下(示例):

//-5M是查找空间小于5M文件
[root@localhost ~]# find /test/  -size -5M  -ls 
51844050    0 drwxr-xr-x   3 root   root        62 10月 28 11:08 /test/
51844095 3072 -rw-r--r--   1 root   root    3145728 10月 28 11:21 /test/file9
51845907    0 drwxr-xr-x   3 root   root        50 10月 28 11:08 /test/content1
 77729    0 drwxr-xr-x   3 root   root         50 10月 28 11:08 /test/content1/content1-2
18365143    0 drwxr-xr-x   2 root   root        45 10月 28 11:08 /test/content1/content1-2/content1-3
51844090 2048 -rwx--x--x   1 sure3  sure4   2097152 10月 28 11:22 /test/content1/content1-2/content1-3/file2
51844087    0 -rwx--x--x   1 sure3  sure3      0 10月 28 10:54 /test/content1/content1-2/file4
51844091    0 -rwxrwxrwx   1 sure4  sure5      0 10月 28 10:54 /test/content1/content1-2/file5
51844092    0 -rw-r--r--   1 sure5  sure6      0 10月 28 10:54 /test/content1/file6
51844093    0 -rwxrwxrwx   1 sure1  sure2      0 10月 28 10:54 /test/content1/file7
//查找空间等于5M的文件
[root@localhost ~]# find /test/  -size 5M -ls
51844087 5120 -rwx--x--x   1 sure3    sure3     5242880 10月 28 13:07 /test/content1/content1-2/file4
51844091 5120 -rwxrwxrwx   1 sure4    sure5     5242880 10月 28 13:07 /test/content1/content1-2/file5
//查找空间大于5M的文件
[root@localhost ~]# find /test/  -size +5M -ls
51844094 15360 -rw-r--r--   1 sure3    sure5    15728640 10月 28 11:20 /test/file8
51843352 20480 -rw-r--r--   1 root     root     20971520 10月 28 11:21 /test/file10
51844072 20480 -rwxrwxrwx   1 sure1    sure2    20971520 10月 28 11:21 /test/content1/content1-2/content1-3/file1
51844073 10240 -rwxrwxrwx   1 sure2    sure1    10485760 10月 28 11:22 /test/content1/content1-2/content1-3/file3

指定查找的目录深度

命令:find 一级目录  -maxdepth 目录深度  -a -name  文件名   动作

-a表示“and”的意思

代码如下(示例):

//在/test/作为一级目录的情况下最大深度为2的目录下查找file1
[root@localhost ~]# find /test/ -maxdepth 2 -name file1  -ls
//在/test/作为一级目录的情况下最大深度为3的目录下查找file1
[root@localhost ~]# find /test/ -maxdepth 3 -name file1  -ls
//在/test/作为一级目录的情况下最大深度为4的目录下查找file1
[root@localhost ~]# find /test/ -maxdepth 4 -name file1  -ls
51844072 20480 -rwxrwxrwx   1 sure1    sure2    20971520 10月 28 11:21 /test/content1/content1-2/content1-3/file1

按文件属主、属组查找

命令:find  路径  -user    属主                                  //查找指定属主的文件
           find  路径  -group  属组                               //查找指定属组的文件

代码如下(示例):

//在test/目录查找属组为sure1的文件
[root@localhost ~]# find /test/ -user sure1 -ls
51844072 20480 -rwxrwxrwx   1 sure1  sure2   20971520 10月 28 11:21 /test/content1/content1-2/content1-3/file1
51844093    0 -rwxrwxrwx   1 sure1  sure2    0 10月 28 10:54 /test/content1/file7
//在test/目录查找属主为sure5的文件
[root@localhost ~]# find  /test/ -group sure5 -ls
51844094 15360 -rw-r--r--  1 sure3  sure5  15728640 10月 28 11:20 /test/file8
51844091 5120 -rwxrwxrwx   1 sure4   sure5   5242880 10月 28 13:07 /test/content1/content1-2/file5

 按文件类型查找文件

命令:find 路径  -type  文件类型

文件类型:

  • b:设备文件(块设备)存储设别硬盘,U盘/dev/sda,/dev/sda1
  • c:设备文件(字符设备)打印机,终端/dev/tty1
  • l:链接文件
  • s:套链字文件
  • p:管道文件

代码如下(示例):

//在/dev/目录下查找目录文件,因为数据太多,就只看前五行内容
[root@localhost ~]# find /dev/ -type d -ls | head -5
     3    0 drwxr-xr-x  21 root     root         3820 10月 27 09:53 /dev/
 18286    0 drwxr-xr-x   2 root     root           60 10月 26 14:11 /dev/vg1
 15799    0 drwxr-xr-x   2 root     root           60 10月 26 14:11 /dev/net
 15797    0 drwxr-xr-x   3 root     root          200 10月 26 14:11 /dev/snd
 18345    0 drwxr-xr-x   2 root     root           60 10月 26 14:11 /dev/snd/by-path
//在/dev/目录下查找链接文件,因为数据太多,就只看前五行内容
[root@localhost ~]# find /dev/ -type l -ls | head -5
 18287    0 lrwxrwxrwx   1 root     root            7 10月 26 14:12 /dev/vg1/lv1 -> ../dm-2
 17437    0 lrwxrwxrwx   1 root     root            3 10月 26 14:11 /dev/cdrom -> sr0
 18346    0 lrwxrwxrwx   1 root     root           12 10月 26 14:11 /dev/snd/by-path/pci-0000:02:02.0 -> ../controlC0
 14325    0 lrwxrwxrwx   1 root     root           25 10月 26 14:11 /dev/initctl -> /run/systemd/initctl/fifo
 11451    0 lrwxrwxrwx   1 root     root            7 10月 26 14:11 /dev/centos/swap -> ../dm-1
//在/dev/目录下查找设备文件,因为数据太多,就只看前五行内容
[root@localhost ~]# find /dev/ -type b -ls | head -5
 43152    0 brw-rw----   1 root     disk       9,   0 10月 26 16:24 /dev/md0
 39043    0 brw-rw----   1 root     disk       8,  33 10月 26 14:12 /dev/sdc1
 39042    0 brw-rw----   1 root     disk       8,  22 10月 26 14:12 /dev/sdb6
 39041    0 brw-rw----   1 root     disk       8,  21 10月 26 14:12 /dev/sdb5
 39040    0 brw-rw----   1 root     disk       8,  20 10月 26 14:12 /dev/sdb4
//在/dev/目录下查找套链字文件
[root@localhost ~]# find /dev/ -type s -ls
  9223    0 srw-rw-rw-   1 root     root            0 10月 26 14:11 /dev/log

按文件权限查找文件

命令:find  路径  -perm  权限   动作

符号表示数字表示说明符号表示数字表示说明
r4只读rx5读和执行
w2只写wx3写和执行
x1只执行rwx7读、写和执行
rw6读和写---0无权限

 代码如下(示例):

//在test/目录中找权限为777的文件
[root@localhost ~]# find /test/ -perm 777 -ls
51844072 20480 -rwxrwxrwx   1 sure1    sure2    20971520 10月 28 11:21 /test/content1/content1-2/content1-3/file1
51844073 10240 -rwxrwxrwx   1 sure2    sure1    10485760 10月 28 11:22 /test/content1/content1-2/content1-3/file3
51844091 5120 -rwxrwxrwx   1 sure4    sure5     5242880 10月 28 13:07 /test/content1/content1-2/file5
51844093    0 -rwxrwxrwx   1 sure1    sure2           0 10月 28 10:54 /test/content1/file7
//在test/目录中找权限为644的文件
[root@localhost ~]# find /test/ -perm 644 -ls
51844094 15360 -rw-r--r--   1 sure3    sure5    15728640 10月 28 11:20 /test/file8
51844095 3072 -rw-r--r--   1 root     root      3145728 10月 28 11:21 /test/file9
51843352 20480 -rw-r--r--   1 root     root     20971520 10月 28 11:21 /test/file10
51844092    0 -rw-r--r--   1 sure5    sure6           0 10月 28 10:54 /test/content1/file6

 权限数字的顺序代表的是:属主权限,属组权限,其他用户权限。

测试不同处理动作

1)对查找到的文件进行删除

命令:在查找命令后面加上  -delete

代码如下(示例):

//查找file2文件,并删除
[root@localhost ~]# find /test/ -name file2 -ls  -delete
51844090 2048 -rwx--x--x   1 sure3    sure4     2097152 10月 28 11:22 /test/content1/content1-2/content1-3/file2
//查不到file2文件了
[root@localhost ~]# find /test/ -name file2 -ls
[root@localhost ~]# 

2)对查到的文件进行复制

命令:在查找命令的处理动作位置加上  -ok cp  -rf  {}  目标路径 \;

{}:原本cp命令是:cp -rf 原文件路径  目标路径,这个{}表示前面查到的文件路径。

\; :结束语。

代码如下(示例):

//原本tmp目录下没有file3文件
[root@localhost ~]# ls /tmp |grep file3

[root@localhost ~]# find /test/ -name file3 -ok cp -rfv {} /tmp \;
< cp ... /test/content1/content1-2/content1-3/file3 > ? yes
"/test/content1/content1-2/content1-3/file3" -> "/tmp/file3"
//file3文件复制到了tmp目录
[root@localhost ~]# ll /tmp |grep file3
-rwxr-xr-x. 1 root root 10485760 10月 28 14:03 file3

总结

        本篇文章了解文件查找,学习了如何使用命令查找相应的文件,学习根据各种限制来查找相对应的文件,并对查找到的文件做一些处理,本篇文章分享就到这结束了,感谢观看。


创作不易,动动小手给个点赞加关注吧,有什么意见评论区告诉我,一起学习。

相关文章:

  • Vue--》Vue中实现数据代理
  • 深度学习入门(十) 模型选择、过拟合和欠拟合
  • RK3399驱动开发 | 12 - AP6255 SDIO WiFi 调试(基于linux4.4.194内核)
  • 牛客网-《刷C语言百题》第二期
  • 测试开发需要掌握哪些技能?
  • 巴什博弈——范围拿物品问题
  • 【Mybatisplus】初识Mybatisplus+SpringBoot整合
  • 【编程碎笔】-Java中关于next(),nextInt(),nextLine()的深度解剖
  • 2023年荆州市高新技术企业申报条件以及奖励补贴政策(附申报时间)汇总!
  • macOS Ventura 正式版你确定不更新,好用到爆的功能你不想尝试一下?
  • 云存储架构框架设计 | 最佳实践
  • 阿里巴巴面试题- - -多线程并发篇(三十)
  • 计算机网络【UDP与TCP协议(三次握手、四次挥手)】
  • Linux进程控制
  • Unity 分享 功能 用Unity Native Share Plugin 实现链接、图片、视频等文件的分享+ 安卓 Ios 都可以,代码图文详解
  • 基于javaweb的嘟嘟二手书商城系统(java+jsp+springboot+mysql+thymeleaf+ftp)
  • 2.1.1 操作系统之进程的定义、特征、组成、组织
  • 一文了解数据结构
  • [LeetCode刷题笔记]4 - 寻找两个正序数组的中位数(归并 / 递归 / 二分查找)
  • 字符串的读入(char与string)