目录

前言

查找命令

命令演示

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/content177729    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 -53    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/vg115799    0 drwxr-xr-x   2 root     root           60 10月 26 14:11 /dev/net15797    0 drwxr-xr-x   3 root     root          200 10月 26 14:11 /dev/snd18345    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 -518287    0 lrwxrwxrwx   1 root     root            7 10月 26 14:12 /dev/vg1/lv1 -> ../dm-217437    0 lrwxrwxrwx   1 root     root            3 10月 26 14:11 /dev/cdrom -> sr018346    0 lrwxrwxrwx   1 root     root           12 10月 26 14:11 /dev/snd/by-path/pci-0000:02:02.0 -> ../controlC014325    0 lrwxrwxrwx   1 root     root           25 10月 26 14:11 /dev/initctl -> /run/systemd/initctl/fifo11451    0 lrwxrwxrwx   1 root     root            7 10月 26 14:11 /dev/centos/swap -> ../dm-1
//在/dev/目录下查找设备文件,因为数据太多,就只看前五行内容
[root@localhost ~]# find /dev/ -type b -ls | head -543152    0 brw-rw----   1 root     disk       9,   0 10月 26 16:24 /dev/md039043    0 brw-rw----   1 root     disk       8,  33 10月 26 14:12 /dev/sdc139042    0 brw-rw----   1 root     disk       8,  22 10月 26 14:12 /dev/sdb639041    0 brw-rw----   1 root     disk       8,  21 10月 26 14:12 /dev/sdb539040    0 brw-rw----   1 root     disk       8,  20 10月 26 14:12 /dev/sdb4
//在/dev/目录下查找套链字文件
[root@localhost ~]# find /dev/ -type s -ls9223    0 srw-rw-rw-   1 root     root            0 10月 26 14:11 /dev/log

按文件权限查找文件

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

符号表示 数字表示 说明 符号表示 数字表示 说明
r 4 只读 rx 5 读和执行
w 2 只写 wx 3 写和执行
x 1 只执行 rwx 7 读、写和执行
rw 6 读和写 --- 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

总结

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


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

Linux文件查找find相关推荐

  1. Linux文件查找之findlocate

    Linux文件查找之find&locate 一.概述 Linux系统核心的思想之一"一切皆文件",对于这么多的文件,如何快速查找过滤呢?下面我们就看看系统提供的文件查找命令 ...

  2. Linux文件查找命令find,xargs详述

    Linux文件查找命令find,xargs详述 总结:zhy2111314 来自:LinuxSir.Org 整理:北南南北 摘要: 本文是find 命令的详细说明,可贵的是针对参数举了很多的实例,大量 ...

  3. Linux文件查找命令find用法整理(locate/find)

    Linux文件查找查找主要包括:locate和find 1.locate 用法简单,根据数据库查找,非实时,用法: locate FILENAME 手动更新数据库(时间可能较长) updatedb 2 ...

  4. linux进入文件全文搜索命令,Linux 文件查找命令详解

    大家好,我是"孤云幕雨":祝大家中秋快乐,今天给大家带来的是<Linux文件查找命令> 一.locate:依赖于事先构建的索引 v依赖于事先构建的索引:索引的构建是在系 ...

  5. linux 查找文件 locate,linux文件查找(find,locate)

    文件查找: locate: 非实时,模糊匹配,查找是根据全系统文件数据库进行的: # updatedb, 手动生成文件数据库 速度快 find: 实时 精确 支持众多查找标准 遍历指定目录中的所有文件 ...

  6. Linux文件查找与tar包管理、企业级sed应用 软件包管理与编译安装httpd

    总结 第7节 Linux文件查找与tar包管理.企业级sed应用 使用locate命令 使用find命令 压缩和解压缩工具 01-文件搜索 locate (00:02:30) locate test. ...

  7. linux检索docx内容,linux 文件查找和内容过滤命令.docx

    linux 文件查找和内容过滤命令 linux 文件查找和内容过滤命令 grep.fgrep和egrep命令 这组命令以指定模式搜索文件,并通知用户在什么文件中搜索到与指定的模式匹配的字符串,并打印出 ...

  8. linux文件查找命令find,locate

    简介 vi : visual interface, 可视化接口.vim(VI IMproved) vim编辑器:文本编辑器.文本:纯文本,ASCII text; unicode; 文本编辑器种类: 行 ...

  9. Linux文件查找find和locate

    目 录 第1章 locate文件查找    1 1.1 概述    1 1.2 locate文件查找的特性    1 第2章 文件查找概述    1 第3章    1 3.1 文件名查找    1 3 ...

  10. Linux文件查找工具之find “大宝剑”--转载

    原文地址:http://xinzong.blog.51cto.com/10018904/1749465 一.文件查找工具常用软件 locate: locate命令其实是find -name的另一种写法 ...

最新文章

  1. R语言logistic回归、判别分析(LDA)、多元自适应样条回归MARS分析案例:分析乳腺癌数据集明确细针穿刺肿瘤活检结果
  2. 人,与动物的本质区别,在哲学里说是制造和使用工具
  3. 内核-syn-ack RTO修改
  4. 在JSP客户端限制表单重复提交
  5. C# WinForm只允许运行一个窗体实例
  6. Visual Studio 2013开发 mini-filter driver step by step 内核中使用线程(7)
  7. Ubuntu下对双显卡的支持问题
  8. python argv,Python argv函数简介
  9. python通过hive transform处理数据
  10. SAP UI5页面动画效果的实现,实际借用了jQuery的库文件
  11. 接口 java 1614953826
  12. 小程序css之字体镂空
  13. DPDK EAL parameters(DPDK环境抽象层参数)-原始版本(F-Stack配置文件的配置参数)
  14. python核心编程第13章答案
  15. 万字长文!面试官问你:自定义View跟绘制流程懂吗?帮你搞定面试官
  16. 使用office tool plus清除office激活状态
  17. app图标icon大全
  18. C++学到什么程度可以找工作?
  19. Lect3 最优化Optimization
  20. badboy简介和回放

热门文章

  1. springMVC开发过程中遇到的404错误的两种情况总结
  2. dell服务器物理盘blink,Blink
  3. 全球公认最健康的作息时间表!全是知识点
  4. 计算机联锁控制系统的软件应具备信号操作功能,车站信号计算机联锁控制系统—软件PPT课件...
  5. 基于Multisim的自动售货的电路课程设计
  6. 微信企业号之构造网页授权链接
  7. java基于springboot的高考填报志愿综合参考系统
  8. php实现图片背景换色功能
  9. 数据库原理及应用实验报告-实验10-触发器
  10. 360企业版域环境msi包格式部署