Linux文件查找find

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

前言


 这篇文章将带领大家学习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:删除查找到的文件,慎用!-delete:删除查找到的文件,慎用!

 ● -ok COMMAND {} ; 对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会交互式要求用户确认-ok COMMAND {} ; 对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会交互式要求用户确认

 ● -exec 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

e57876266a634344a8854eea84870ed2.png


按文件类型查找文件


命令: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  权限   动作


符号表示 数字表示 说明 符号表示 数字表示 说明
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

总结


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



相关文章
|
4天前
|
Linux
Linux 中经常见到的 gz 文件
Linux 中经常见到的 gz 文件
|
14天前
|
网络协议 Unix Linux
Linux 多种方式实现文件共享(三)NFS 6
【8月更文挑战第6天】NFS 即网络文件系统,是一种使用于分布式文件系统的协议,NFS 功能是通过网络让不同的机器,不同的操作系统能够彼此分享各自的数据,让应用程序在客户端通过网络访问位于服务器磁盘中的数据
49 13
|
4天前
|
存储 Linux Windows
Linux zip命令:压缩文件或目录
我们经常会在 Windows 系统上使用 “.zip”格式压缩文件,其实“.zip”格式文件是 Windows 和 Linux 系统都通用的压缩文件类型,属于几种主流的压缩格式(zip、rar等)之一,是一种相当简单的分别压缩每个文件的存储格式,本节要讲的 zip 命令,类似于 Windows 系统中的 winzip 压缩程序,其基本格式如下: [root@localhost ~]#zip [选项] 压缩包名 源文件或源目录列表 注意,zip 压缩命令需要手工指定压缩之后的压缩包名,注意写清楚扩展名,以便解压缩时使用。 下面给大家举几个例子。 【例 1】zip 命令的基本使用。 [r
18 0
Linux zip命令:压缩文件或目录
|
7天前
|
Linux Shell 数据库
【绝技大公开】Linux文件查找新招式:打破常规,探索那些鲜为人知的技巧,让你成为真正的文件追踪大师!
【8月更文挑战第13天】文件查找是Linux用户必备技能,能大幅提升工作效率。本文介绍几种高效查找方法,包括使用`column`美化`find`输出、利用`locate`和`mlocate`快速搜索、编写脚本自动化任务、采用`fd`现代工具提升查找体验,以及结合`grep`和`rg`搜索文件内容。此外,还推荐了`Gnome Search Tool`和`Albert`等图形界面工具,为用户提供多样选择。掌握这些技巧,让你的工作更加得心应手。
21 2
|
13天前
|
存储 网络协议 Linux
Linux 多种方式实现文件共享(四)iSCSI 磁盘共享服务 7
【8月更文挑战第7天】iSCSI技术是一种新储存技术, iSCSI 提供了在 IP 网络封装 SCSI 命令,且以TCP/IP协议传输.
35 5
|
11天前
|
Linux
Linux专栏07:Linux基本指令之文件搜索指令
Linux专栏07:Linux基本指令之文件搜索指令
34 2
|
14天前
|
存储 Linux 开发工具
Linux 多种方式实现文件共享(二)Samba 5
【8月更文挑战第5天】Samba 文件共享
39 5
|
14天前
|
Linux Perl
Linux进行文件字符串替换
【8月更文挑战第5天】Linux进行文件字符串替换
74 3
|
4天前
|
Oracle Java 关系型数据库
简单记录在Linux上安装JDK环境的步骤,以及解决运行Java程序时出现Error Could not find or load main class XXX问题
本文记录了在Linux系统上安装JDK环境的步骤,并提供了解决运行Java程序时出现的"Error Could not find or load main class XXX"问题的方案,主要是通过重新配置和刷新JDK环境变量来解决。
16 0
|
5天前
|
Linux
Linux 堡垒机命令行中如何上传下载文件(SecureCRT - SFTP)
Linux 堡垒机命令行中如何上传下载文件(SecureCRT - SFTP)
25 0