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

总结


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



相关文章
|
27天前
|
Linux Shell
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
79 1
|
25天前
|
人工智能 安全 Linux
【Linux】Linux之间如何互传文件(详细讲解)
【Linux】Linux之间如何互传文件(详细讲解)
|
1天前
|
Linux Go 数据安全/隐私保护
Linux 中的文件属性解析
在 Linux 系统中,每个文件和目录有一组属性控制其操作和访问权限。了解这些属性对有效管理文件至关重要。文件属性包括:文件类型(如 `-` 表示普通文件,`d` 表示目录),权限(如 `rwx` 表示所有者权限,`r-x` 表示组和其他用户权限),所有者,组,硬链接数,文件大小和最后修改时间。通过 `chown` 和 `chmod` 命令可更改文件所有者、所属组及权限。此外,还有特殊权限(如 SUID、SGID)和 ACL(访问控制列表)提供更精细的访问控制。
|
2天前
|
人工智能 Linux
Linux查找大文件的方法
Linux查找大文件的方法
|
5天前
|
固态存储 Ubuntu Linux
Linux(29) 多线程快速解压缩|删除|监视大型文件
Linux(29) 多线程快速解压缩|删除|监视大型文件
11 1
|
5天前
|
Ubuntu Linux 数据安全/隐私保护
Linux(24) 如何在Ubuntu中操作rootfs.img文件
Linux(24) 如何在Ubuntu中操作rootfs.img文件
9 0
|
9天前
|
安全 Linux 开发工具
Linux中可引起文件时间戳改变的相关命令
【4月更文挑战第12天】Linux中可引起文件时间戳改变的相关命令
20 0
|
11天前
|
Linux Shell 开发工具
Linux文件常用操作
Linux文件常用操作(几乎覆盖所有日常使用)
85 0
|
12天前
|
Linux 内存技术 Perl
【ZYNQ】制作从 QSPI Flash 启动 Linux 的启动文件
【ZYNQ】制作从 QSPI Flash 启动 Linux 的启动文件
|
15天前
|
Linux
Linux 指令|date|cal|find|grep|热键
Linux 指令|date|cal|find|grep|热键