15条 linux Find 命令实际使用方法

【UV打印机】PrintExp打印软件教程(二)-主界面介绍 PrintExp软件主界面如下图所示PrintExp软件的主界面主要包括主菜单区,工具栏区,界面显示区,状态栏。以下是上述部分的介绍。 阅读详情

This article is written by SathiyaMoorthy

作者: SathiyaMoorthy
 
Apart from the basic operation of looking for files under a directory structure, you can also perform several practical operations using find command that will make your command line journey easy.

Find命令除了在目录结构中查找文件之外,还可以用来做一些其他比较有实际意义的操作。
 
In this article, let us review 15 practical examples of Linux find command that will be very useful to both newbies and experts.

本文中的15条tips,对老鸟和菜鸟都会很有帮助

First, create the following sample empty files under your home directory to try some of the find command examples mentioned below.

首先在你的HOME目录下建立一些空的文件,并尝试下面的一些命令。

# vim create_sample_files.sh  //使用vi创建文件,并输入下面的内容
touch MybashProgram.sh
touch mycprogram.c
touch MyCProgram.c
touch Program.c

mkdir backup
cd backup

touch MybashProgram.sh
touch mycprogram.c
touch MyCProgram.c
touch Program.c

# chmod +x create_sample_files.sh //添加可运行属性

# ./create_sample_files.sh //运行脚本

# ls -R //查看执行的结果
.:
backup MybashProgram.sh MyCProgram.c
create_sample_files.sh mycprogram.c Program.c

./backup:
MybashProgram.sh mycprogram.c MyCProgram.c Program.c

1. Find Files Using Name 根据文件名查找

This is a basic usage of the find command. This example finds all files with name — MyCProgram.c in the current directory and all it’s sub-directories.

这个是Find命令最基本的操作,下面的例子是在当前目录和它所有子目录中查找MyCProgramm.c

# find -name "MyCProgram.c"
./backup/MyCProgram.c
./MyCProgram.c

2. Find Files Using Name and Ignoring Case 在1的基础上忽略文件名的大小写

This is a basic usage of the find command. This example finds all files with name — MyCProgram.c (ignoring the case) in the current directory and all it’s sub-directories.

这也是Find命令的基本操作之一.下面的例子是在当前目录及所有子目录中查找MyCProgram.c(忽略大小写)

# find -iname "MyCProgram.c"
./mycprogram.c
./backup/mycprogram.c
./backup/MyCProgram.c
./MyCProgram.c

3. Limit Search To Specific Directory Level Using mindepth and maxdepth

     查找指定目录并且设置查找的目录深度

Find the passwd file under all sub-directories starting from root directory.

下面的例子是在根目录及所有子目录中查找passwd文件

# find / -name passwd
./usr/share/doc/nss_ldap-253/pam.d/passwd
./usr/bin/passwd
./etc/pam.d/passwd
./etc/passwd


Find the passwd file under root and one level down. (i.e root — level 1, and one sub-directory — level 2)

查找根目录和根目录的和只展开一级的子目录中查找
# find -maxdepth 2 -name passwd
./etc/passwd


Find the passwd file under root and two levels down. (i.e root — level 1, and two sub-directories — level 2 and 3 )

在根目录和根目录下展开两级查找passwd文件

# find / -maxdepth 3 -name passwd
./usr/bin/passwd
./etc/pam.d/passwd
./etc/passwd


Find the password file between sub-directory level 2 and 4.

在根目录的第二级和第四级之间查找

# find -mindepth 3 -maxdepth 5 -name passwd
./usr/bin/passwd
./etc/pam.d/passwd

4. Executing Commands on the Files Found by the Find Command.

     查找到文件后,执行其他的命令

In the example below, the find command calculates the md5sum of all the files with the name MyCProgram.c (ignoring case). {} is replaced by the current file name.

下面的例子展示了在查找到文件后,计算文件的MD5值

# find -iname "MyCProgram.c" -exec md5sum {} /;
d41d8cd98f00b204e9800998ecf8427e ./mycprogram.c
d41d8cd98f00b204e9800998ecf8427e ./backup/mycprogram.c
d41d8cd98f00b204e9800998ecf8427e ./backup/MyCProgram.c
d41d8cd98f00b204e9800998ecf8427e ./MyCProgram.c

5. Inverting the match. 查找不匹配的文件

Shows the files or directories whose name are not MyCProgram.c .Since the maxdepth is 1, this will look only under current directory.

查找文件名不是MyCProgramm.c的文件,注意由于maxdepth是1,将只在当前目录中查找。

# find -maxdepth 1 -not -iname "MyCProgram.c"
.
./MybashProgram.sh
./create_sample_files.sh
./backup
./Program.c

6. Finding Files by its inode Number. 根据inode number查找

Every file has an unique inode number, using that we can identify that file. Create two files with similar name. i.e one file with a space at the end.

某些时候,有些文件的文件名非常相似,肉眼比较难以分辨,这时候可以根据他们唯一的inode number进行查找

# touch "test-file-name"

# touch "test-file-name " //这个文件名最后有一个空格

[Note: There is a space at the end]

# ls -1 test*
test-file-name
test-file-name


From the ls output, you cannot identify which file has the space at the end. Using option -i, you can view the inode number of the file, which will be different for these two files.

使用ls命令查看的时候,我们不能分辨这两个文件,使用ls命令的选项-i则可以查看他们的inode number

# ls -i1 test*
16187429 test-file-name
16187430 test-file-name


You can specify inode number on a find command as shown below. In this example, find command renames a file using the inode number.

在使用find命令的时候可以指定inode number

# find -inum 16187430 -exec mv {} new-test-file-name /;

# ls -i1 *test*
16187430 new-test-file-name
16187429 test-file-name


You can use this technique when you want to do some operation with the files which are named poorly as shown in the example below. For example, the file with name — file?.txt has a special character in it. If you try to execute “rm file?.txt”, all the following three files will get removed. So, follow the steps below to delete only the “file?.txt” file.

这个技巧还可以适用于当文件名中存在特殊字符的情况

# ls
file1.txt file2.txt file?.txt

Find the inode numbers of each file.
# ls -i1
804178 file1.txt
804179 file2.txt
804180 file?.txt


Use the inode number to remove the file that had special character in it as shown below.

使用inodenumber删除包含特殊字符的文件

# find -inum 804180 -exec rm {} /;

# ls

file1.txt file2.txt
[Note: The file with name "file?.txt" is now removed]

7. Find file based on the File-Permissions

根据文件的权限查找

Following operations are possible.

可以进行以下的这几种操作

  • Find files that match exact permission
  • Check whether the given permission matches, irrespective of other permission bits
  • Search by giving octal / symbolic representation


For this example, let us assume that the directory contains the following files. Please note that the file-permissions on these files are different.

# ls -l
total 0
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read
---------- 1 root root 0 2009-02-19 20:31 no_for_all
-rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
-rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read
----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read


Find files which has read permission to group. Use the following command to find all files that are readable by the world in your home directory, irrespective of other permissions for that file.

# find . -perm -g=r -type f -exec ls -l {} /;
-rw-r--r-- 1 root root 0 2009-02-19 20:30 ./everybody_read
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 ./all_for_all
----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read
-rw-r----- 1 root root 0 2009-02-19 20:27 ./others_can_also_read


Find files which has read permission only to group.

# find . -perm g=r -type f -exec ls -l {} /;
----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read


Find files which has read permission only to group [ search by octal ]

# find . -perm 040 -type f -exec ls -l {} /;
----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read

8. Find all empty files (zero byte file) in your home directory and it’s subdirectory

Most files of the following command output will be lock-files and place holders created by other applications.

# find ~ -empty


List all the empty files only in your home directory.

# find . -maxdepth 1 -empty


List only the non-hidden empty files only in the current directory.

# find . -maxdepth 1 -empty -not -name ".*"

9. Finding the Top 5 Big Files

The following command will display the top 5 largest file in the current directory and it’s subdirectory. This may take a while to execute depending on the total number of files the command has to process.

# find . -type f -exec ls -s {} /; | sort -n -r | head -5

10. Finding the Top 5 Small Files

Technique is same as finding the bigger files, but the only difference the sort is ascending order.

# find . -type f -exec ls -s {} /; | sort -n  | head -5


In the above command, most probably you will get to see only the ZERO byte files ( empty files ). So, you can use the following command to list the smaller files other than the ZERO byte files.

# find . -not -empty -type f -exec ls -s {} /; | sort -n  | head -5

11. Find Files Based on file-type using option -type

Find only the socket files.

# find . -type s


Find all directories

# find . -type d


Find only the normal files

# find . -type f


Find all the hidden files

# find . -type f -name ".*"


Find all the hidden directories

# find -type d -name ".*"

12. Find files by comparing with the modification time of other file.

Show files which are modified after the specified file. The following find command displays all the files that are created/modified after ordinary_file.

# ls -lrt
total 0
-rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read
----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read
-rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
---------- 1 root root 0 2009-02-19 20:31 no_for_all

# find -newer ordinary_file
.
./everybody_read
./all_for_all
./no_for_all

13. Find Files by Size

Using the -size option you can find files by size.

Find files bigger than the given size

# find ~ -size +100M


Find files smaller than the given size

# find ~ -size -100M


Find files that matches the exact given size

# find ~ -size 100M


Note: – means less than the give size, + means more than the given size, and no symbol means exact given size.

14. Create Alias for Frequent Find Operations

If you find some thing as pretty useful, then you can make it as an alias. And execute it whenever you want.


Remove the files named a.out frequently.

# alias rmao="find . -iname a.out -exec rm {} /;"
# rmao


Remove the core files generated by c program.

# alias rmc="find . -iname core -exec rm {} /;"
# rmc

 

15. Remove big archive files using find command

The following command removes *.zip files that are over 100M.

# find / -type f -name *.zip -size +100M -exec rm -i {} /;"

Remove all *.tar file that are over 100M using the alias rm100m (Remove 100M). Use the similar concepts and create alias like rm1g, rm2g, rm5g to remove file size greater than 1G, 2G and 5G respectively.

# alias rm100m="find / -type f -name *.tar -size +100M -exec rm -i {} /;"
# alias rm1g="find / -type f -name *.tar -size +1G -exec rm -i {} /;"
# alias rm2g="find / -type f -name *.tar -size +2G -exec rm -i {} /;"
# alias rm5g="find / -type f -name *.tar -size +5G -exec rm -i {} /;"

# rm100m
# rm1g
# rm2g
# rm5g


This article was written by SathiyaMoorthy, author of numbertotext Vim plugin, which will replace the numbers with the equivalent text inside Vim. The Geek Stuff welcomes your tips and guest articles.

How do you use find command? What is your favorite find command example? Please leave a comment and let us know. Also, if you would like to know how to use find command under a specific scenario, please let us know your questions in the comments section.

Post from: The Geek Stuff

YOLOv8的特征蒸馏代码应用讲解(CWD/MGD/mimic/在线蒸馏/逻辑蒸馏) 基于YOLOv8的逻辑蒸馏、特征蒸馏、在线蒸馏等方法代码介绍。 阅读详情

相关推荐

基于扩展卡尔曼滤波(EKF)结合到达时间(TDOA)、到达频率(FDOA)实现多无人机无源定位,MATLAB代码

该程序实现了一种结合到达时间差(TDOA)和到达频率差(FDOA)技术的多无人机定位方法,利用扩展卡尔曼滤波(EKF)对单一目标进行定位。程序通过模拟多无人机的运动及其与目标的相对位置,估计目标的状态(位置和速度)。

985工科博士毕业,专攻定位、导航和滤波等算法研究。从业10年,主要使用MATLAB 1270

linux运维笔记6

不同系统之间的文件传输 ####1.文件归档#### 1. 文件归档,就是把多个文件变成一个归档文件 2. tar c       ##创建     f       ##指定归档文件名称     t       ##显示归档文件中的内容     r       ##向归档文件中添加文件     --get       ##取出单个文件     --delete    ##删

njd_step的博客 282

国产谷歌地球,地形分析秒杀同款地图软件

咱们做地质、林业、水文、水利、电力、GIS、地图、水保、环境的,需要经常用到各种数据,比如影像、地形、矢量、模型等,但目前80%的规划大数据展示都离不开ArcGIS,令人头大的是很多朋友连ArcGIS软件安装起来都费劲。。。 那不会ArcGIS又装不了Google Earth怎么开展工作? 今天推荐大家一款国产免费专业地图浏览下载测量分析神器—图新地球 图新地球,国产三维gis软件。可以查看高清卫星影像地形以及其他很多常用电子地图,对地图需求的掌握远胜过Google Maps,而且上手比国外软件要简单

GISWorld3D的博客 3695

初识Linux及常用命令

本文主要介绍了一些常用的Linux基本命令以及一些基本操作!!!

sdfewtg的博客 621

Unity3D Pico VR 手势交互实战:基于MRTK3实现抓握与射线控制

本文针对Pico VR原生手势交互功能不足的问题,提供了基于MRTK3实现高级手势交互的实战解决方案。通过将MRTK3成熟的交互框架与Pico的手势追踪能力相结合,详细讲解了从环境配置、SDK导入、核心适配到真机调试的全流程,帮助开发者高效实现抓握与射线控制等复杂交互,有效提升Pico VR应用的开发效率与体验。

weixin_29011667的博客 401

JetLinks社区版VS企业版:手把手教你用PostgreSQL+Redis搭建物联网中台(Win7实测)

本文详细对比了JetLinks社区版与企业版的核心差异,并提供了在Windows 7系统上,使用PostgreSQL和Redis搭建物联网中台的完整部署教程。文章深入解析了从环境准备、后端服务构建到前端部署的每一步,为开发者在资源有限环境下构建高性价比物联网平台提供了清晰的实战指南。

w1x2y3的博客 315

Linux命令学习_07

Linux命令学习 uptime 作用:输出计算机的持续在线时间(计算机从开机到现在运行的时间) [root@tedu ~]# uptime uname 作用:获取计算机系统相关信息 语法1:#uname 获取操作系统的类型 语法2 : #uname -a all,表示获取全部的系统信息**(类型、全部主机名、内核版本、发布时间、开源计划)** netstat -tnlp 作用:查看网络连接状态 语法:#netstat -tnlp 选项说明: -t:表示只列出tc

weixin_43678040的博客 177

基于AMD AU15P FPGA的SLVS-EC桥PCIe设计方案分享

各位FPGAer周末愉快,今天熊猫君分享一个基于AMD AU15P FPGA的SLVS-EC桥PCIe设计方案。

haoxingheng的专栏 1453

Linux命令--文本查找命令find的用法详解

find 命令主要用于查找目录和文件,可以指定多种参数进行匹配。 用法:find +查找路径 +命令参数 [输出形式] 查找路径:告诉find在哪查找 命令参数:指定要查找的文件属性,属性包括多种,类型、名称、大小、修改时间等等 常用的参数:-name 按文件名进行查找 -user 按照文件的属主进行查找 -mtime 按照文件的更改时间来查找,-n 表示文件修改时间距今天n...

菜鸟博客 1089

「转载」vaptcha 等手势验证码图像识别与轨迹提取(思路)

作者:文安哲 从博客重开以后就说要更文章,鸽了蛮久了,赶紧补上一篇嫖嫖各位老公的流量。 最近看群里面大家讨论研究手势验证码比较多,然后我也顺带研究做了一下,给各位老公们分享一下做的过程。 首先,一起来康康手势验证码长啥样! 大概就是长这个样子,需要按照图像中的那条轨迹再图片上滑动,不得不说这类验证码确实体验感拉满,让人很想立马关掉这个网站。 那么言归正传,这类验证码该怎么做识...

AI悦创·编程私教1v1 3618

2025年软件测试职业院校技能大赛—ERP管理平台-采购管理采购入库模块测试用例

竞赛对接软件测试相关标准,以“ERP管理平台” 为被测系统,围绕软件测试领域的主流技术及工具,考查选手面 对实际问题的综合分析能力,对测试文档、测试用例的设计能力, 对单元测试、自动化测试、性能测试、接口测试等测试工具及方 法的掌握程度,检验选手团队协作能力、组织规划能力、方法运用能力等。

我是网络安全兼技能大赛工程师,专注网络安全技术学习与技能大赛实战!持续分享最新的技术文章,帮你少走弯路,一起在技术赛道上进步~ 417

find使用例子

http://johnmeister.com/CS/UNIX/FIND/find-usage.html sudo find / -type f -name *.jpg -exec cp {} . \; find . -type f -size +10000 -exec ls -al {} \; find . -atime +1 -type f -exec mv {} TMP ...

hanyh 287

★教程2:fpga学习教程入门100例目录【含程序操作讲解视频】

★基础入门 1.FPGA开发软件安装

FPGA/MATLAB学习教程/源码/项目合作开发 7万+

【Linux】find命令详解

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结 前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。 提示:以下是本篇文章正文内容,下面案例可供参考 一、pandas是什么? 示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。 二、使用步骤 1.引入库 代码

Derical的博客 1万+

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

15 个实用的 Linux find 命令示例

除了在目录结构下查找文件的基本操作外,我们还可以使用find命令执行一些实际操作,这将使我们的命令行之旅变得轻松。在本文中,让我们回顾 15 个 Linux find命令的实际示例,它们对新手和专家都非常有用。首先,在主目录下创建以下示例空文件,从而方便尝试下面提到的一些find命令示例。查看运行结果如下。

迹忆客 3340

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

linux find命令事例,15个实用的Linux find命令示例

除了在一个目录结构下查找文件这种基本的操作,你还可以用find命令实现一些实用的操作,使你的命令行之旅更加简易。本文将介绍15种无论是于新手还是老鸟都非常有用的Linux find命令。首先,在你的home目录下面创建下面的空文件,来测试下面的find命令示例。#vimcreate_sample_files.shtouchMybashProgram.shtouchmycprogram.ct...

weixin_30322901的博客 364

Linux find命令详解与实际使用

exec选项处理匹配到的文件时,find命令将所有匹配到的文件一起传递给-exec执行,系统传递给-exec的命令长度有限制,如果find的结果比较多,可能会出现溢出错误。这种情况就可以使用xargs命令,find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,它会先处理最先获取的一部分文件,然后处理下一批,直到全部处理完成。另外,在部分系统中-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,可能导致进程过多,xargs命令则只有一个进程。

trayvontang的博客 1759

Linux之 grep、find、ls、wc 命令

linux命令是对Linux系统进行管理的命令。对于Linux系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与之前的DOS命令类似。linux命令在系统中有两种类型:内置Shell命令和Linux命令。

Linux运维老纪的博客 1383

15个常用find命令示例

除了在一个目录结构下查找文件这种基本的操作,你还可以用find命令实现一些实用的操作,使你的命令行之旅更加简易。 本文将介绍15种无论是于新手还是老鸟都非常有用的Linux find命令。 首先,在你的home目录下面创建下面的空文件,来测试下面的find命令示例。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

tianshui121的专栏 837

妈咪,我找到了! -- 15个实用的Linux find命令示例

除了在一个目录结构下查找文件这种基本的操作,你还可以用find命令实现一些实用的操作,使你的命令行之旅更加简易。 本文将介绍15种无论是于新手还是老鸟都非常有用的Linux find命令。 首先,在你的home目录下面创建下面的空文件,来测试下面的find命令示例。 01 # vim create_sample_files.sh

蜡笔小辛的专栏 2257

Linux常用20条命令

Linux常用20条命令假装这里有个标题cdls 假装这里有个标题 Linux下命令如此之多,但我们所常用命令却并非如此。参考了众多博客后,以下20条常用命令送给大家 cd 对于目录或是说文件夹的打开与关闭是Linux下最基础的命令,而cd命令正是这么一个有趣的目录处理命令 cd /home #打开home目录(注意cd与/之间有一个空格) #cd ./home与 cd home 基本上没差别这个...

qq_43605229的博客 936

Linux find命令

Linux中find命令出现频率较高用法 Linux 查找命令是Linux系统中最重要和最常用的命令之一。查找用于根据与参数匹配的文件指定的条件来搜索和查找文件和目录列表的命令。查找可以在各种条件下使用,您可以通过权限,用户,组,文件类型,日期,大小等可能的条件查找文件。本文将分为五个部分,从基本到提前使用find命令。 第一部分:查找名称查找文件的基本查找命令 第二部分:根据他们的权限查找文件 第三部分:基于所有者和组的搜索文件 第四部分:根据日期和时间查找文件和目录 第五部分:根据大小查找文件和目录

weixin_48321071的博客 1812
上一篇: 练习 3.16 删除一组数据中的相同数据
下一篇: 求两个整型数字的最大公约数问题
magicpang
博客等级 码龄21年 11粉丝 39原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值