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

【Linux基础】find在linux中查找文件

find作用

find主要用来查找文件(根据文件的名称查找,不能查找文件的内容)
find - search for files in a directory hierarchy/在目录层次结构中搜索文件

主要参数

-type 根据文件类型查找文件

-name 根据名称查找文件,固定写法 最好加上双引号

-iname 根据名称查找文件,不区分大小写,固定写法 最好加上双引号

-mtime 根据修改时间查找文件 +7 查找七天前文件 -10查找10天内文件 8查找往前数的第八天的文件

-size 根据文件大小查找文件 +10M 大于10M -1G小于1G 20M 等于20M

-inum 根据inode号查找文件

案例

#案例一 根据文件类型来查找文件
[root@wql ~]# pwd
/root[root@wql ~]# mkdir wql[root@wql ~]# cd wql[root@wql wql]# touch {1..3}.txt
[root@wql wql]# touch {1..3}.log
[root@wql wql]# touch {1..3}.TXT
[root@wql wql]# mkdir dir-{1..3}[root@wql wql]# ll
total 0
-rw-r--r-- 1 root root 0 Apr  8 01:56 1.log
-rw-r--r-- 1 root root 0 Apr  8 01:56 1.txt
-rw-r--r-- 1 root root 0 Apr  8 01:56 1.TXT
-rw-r--r-- 1 root root 0 Apr  8 01:56 2.log
-rw-r--r-- 1 root root 0 Apr  8 01:56 2.txt
-rw-r--r-- 1 root root 0 Apr  8 01:56 2.TXT
-rw-r--r-- 1 root root 0 Apr  8 01:56 3.log
-rw-r--r-- 1 root root 0 Apr  8 01:56 3.txt
-rw-r--r-- 1 root root 0 Apr  8 01:56 3.TXT
drwxr-xr-x 2 root root 6 Apr  8 01:56 dir-1
drwxr-xr-x 2 root root 6 Apr  8 01:56 dir-2
drwxr-xr-x 2 root root 6 Apr  8 01:56 dir-3默认不加路径参数 即为当前路径
[root@wql wql]# find -type f
./1.txt
./2.txt
./3.txt
./1.log
./2.log
./3.log
./1.TXT
./2.TXT
./3.TXT
[root@wql wql]# find . -type f
./1.txt
./2.txt
./3.txt
./1.log
./2.log
./3.log
./1.TXT
./2.TXT
./3.TXT
[root@wql wql]# find ./ -type f
./1.txt
./2.txt
./3.txt
./1.log
./2.log
./3.log
./1.TXT
./2.TXT
./3.TXT
[root@wql wql]# 默认rm跳过了.目录[root@wql wql]# find  -type d
.
./dir-1
./dir-2
./dir-3
[root@wql wql]# 
[root@wql wql]# 
[root@wql wql]# 
[root@wql wql]# find  -type d | xargs rm -rf 
rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘.’
[root@wql wql]# ll
total 0
-rw-r--r-- 1 root root 0 Apr  8 01:56 1.log
-rw-r--r-- 1 root root 0 Apr  8 01:56 1.txt
-rw-r--r-- 1 root root 0 Apr  8 01:56 1.TXT
-rw-r--r-- 1 root root 0 Apr  8 01:56 2.log
-rw-r--r-- 1 root root 0 Apr  8 01:56 2.txt
-rw-r--r-- 1 root root 0 Apr  8 01:56 2.TXT
-rw-r--r-- 1 root root 0 Apr  8 01:56 3.log
-rw-r--r-- 1 root root 0 Apr  8 01:56 3.txt
-rw-r--r-- 1 root root 0 Apr  8 01:56 3.TXT
[root@wql wql]# [root@wql wql]# rm -rf .
rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘.’
[root@wql wql]# 
[root@wql wql]# 
[root@wql wql]# rm -rf ..
rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘..
[root@wql wql]# find ./ -type f -name "*.txt"
./1.txt
./2.txt
./3.txt
[root@wql wql]# 
[root@wql wql]# find ./ -type f -iname "*.txt"
./1.txt
./2.txt
./3.txt
./1.TXT
./2.TXT
./3.TXT
[root@wql wql]#
#支持变量和通配符
[root@wql wql]# find ./ -type f -name "*$wql"
./1.txt
./2.txt
./3.txt
[root@wql wql]# #精确查找不带""未报错
[root@wql wql]# find ./ -type f -name  1.txt
./1.txt
[root@wql wql]# 
#但是使用通配符后 不带"" 报错
[root@wql wql]# find ./ -type f -name  *.txt
find: paths must precede expression: 2.txt
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@wql wql]# 
#案例根据inode号查找文件
[root@wql wql]# ll -i
total 0
33592651 -rw-r--r-- 1 root root 0 Apr  8 01:56 1.log
33592648 -rw-r--r-- 1 root root 0 Apr  8 01:56 1.txt
33592654 -rw-r--r-- 1 root root 0 Apr  8 01:56 1.TXT
33592652 -rw-r--r-- 1 root root 0 Apr  8 01:56 2.log
33592649 -rw-r--r-- 1 root root 0 Apr  8 01:56 2.txt
33592655 -rw-r--r-- 1 root root 0 Apr  8 01:56 2.TXT
33592653 -rw-r--r-- 1 root root 0 Apr  8 01:56 3.log
33592650 -rw-r--r-- 1 root root 0 Apr  8 01:56 3.txt
33592656 -rw-r--r-- 1 root root 0 Apr  8 01:56 3.TXT
[root@wql wql]# 
[root@wql wql]# find  -inum 33592651
./1.log
[root@wql wql]# 

[root@wql wql]# cat find ./ -type f
123
456
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]# touch {1…3}.log
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]# find ./ -type f -or name “.txt"
find: paths must precede expression: name
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path…] [expression]
[root@wql wql]# find ./ -type f -or -name "
.txt”
./1.txt
./2.txt
./1.log
./2.log
./3.log
[root@wql wql]#
[root@wql wql]#
[root@wql wql]# find ./ -type f -name “.txt"
./1.txt
./2.txt
[root@wql wql]# find ./ -type f -name "
.txt” -or -name “*.log”
./1.txt
./2.txt
./1.log
./2.log
./3.log
[root@wql wql]#

http://www.dtcms.com/a/304860.html

相关文章:

  • Jenkins 详解
  • 准大一GIS专业新生,如何挑选电脑?
  • 【Kotlin】const 修饰的编译期常量
  • 医疗超声成像专用AFE模拟前端
  • 【CSS】盒子类型
  • Qwen3-Coder:介绍及使用 -- 超强AI编程助手
  • CSRF漏洞原理及利用
  • 镜像源加速下载
  • 编辑距离:理论基础、算法演进与跨领域应用
  • 百度前端面试题目整理
  • 通过Power Automate获取SharePoint的Share Link
  • 计算机视觉(CV方向)算法基础
  • Apache Ignite 的连续查询(Continuous Queries)功能的详细说明
  • Apache Ignite 关于 容错(Fault Tolerance)的核心机制
  • 零件边界线提取处理原理详解
  • 如何解决人工智能在社会治理中面临的技术和伦理挑战?
  • 【工具】图床完全指南:从选择到搭建的全方位解决方案
  • 赢在AI时代:从创造力到编程力的教育突围
  • 聊聊自动化测试用例维护成本高应对策略
  • IntelliJ IDEA 配置 Maven 阿里云镜像加速源全流程
  • AAA 与 FTP:网络认证授权及文件传输的原理与实践
  • AT8236-4A单通道直流有刷电机驱动芯片
  • Qt 移动应用性能优化策略
  • 编程技术杂谈3.0
  • [CSP-J 2022] 逻辑表达式
  • 仓颉编程语言类型特点
  • 如何用USRP捕获手机信号波形(下)协议分析
  • 小孙学变频学习笔记(十二)机械特性的调整 机械特性的改善
  • 山东重工集团:以全自动化生产重构重卡制造业新范式
  • Docker运行Ollama