Mommy, I found it! — 15 Practical Linux Find Command Examples

【车载以太网】【架构】整体架构与协议标准 车载以太网架构图 应用层:DHCP /SOMEIP/DoIP/XCP/UDPNM 传输层:TCP/IP 网络层:IPv4/IPv6/ARP/ICMP 链路层:IEEE Ethernet MAC+VLAN 物理层:Ethernet PHY: 100BASE-T1/1000BASE-T1/100BASE-Tx 阅读详情

 原文地址:http://www.thegeekstuff.com/2009/03/15-practical-linux-find-command-examples/

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.
In this article, let us review 15 practical examples of Linux find command that will be very useful to both newbies and experts.
First, create the following sample empty files under your home directory to try some of the find command examples mentioned below.

# vim create_sample_files.sh
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 -name "MyCProgram.c"
./backup/MyCProgram.c
./MyCProgram.c

2. Find Files Using Name and Ignoring Case

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 -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.

# 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 )

# 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.

# 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.

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

6. Finding Files by its 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.

# 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 -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 -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.

# 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

Find Command Examples Second Part

If you liked this Mommy article on find command, don’t forget to check-out the Daddy article of the find command — Daddy, I found it!, 15 Awesome Linux Find Command Examples (Part2)

Awesome Linux Articles

Following are few awesome 15 examples articles that you might find helpful.

YOLOv11 改进入门篇 | 从原理到代码:CBAM 注意力模块的插入与涨点验证 本文介绍了如何在YOLOv11模型中插入CBAM注意力模块以提升检测性能。CBAM包含通道和空间注意力两个子模块,通过双池化操作和MLP学习特征权重。文章详细解析了CBAM原理,提供了PyTorch实现代码,并建议在YOLOv11的Stage-3和Stage-4之间插入CBAM。实验表明,该方法在COCO数据集上使nano和small模型的mAP提升1.7-2.3个百分点,参数量仅增加0.27%,推理延迟增加0.1ms。最后给出了插入位置选择、通道缩减比等调优建议,验证了CBAM作为轻量级注意力模块的有效性 阅读详情

相关推荐

自学篇--使用keil5.23创建一个stm32工程并编译(固件包自带版本)

之前在本科学习阶段学习过一点51单片机的知识,可是因为基本没有实践过几次现在已经完全忘记了。。前几天老师突然丢给我几个板子说让我把程序编译、烧写环境装好,当时我就震惊了,不过经过三四天的自我摸索,之后参考他人的学习经验,现在已经基本能处理这个问题了,所以为了方便后面的学者,主要也是为了把自己这几天的学习成果记录一下,我在这里把这个过程写下来。(因为刚刚接触,很多原理性的东西都还不懂,只是一个操作步

yang_csdnForOBTS的博客 8659

find xargs指令浅尝

1、find命令的一般形式为; find pathname -options [-print -exec -ok ...] 2、find命令的参数; pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。

614

MTK Linux Charger驱动分析(二) - power_supply_core.c

的核心作用可以概括为:提供统一的电源管理框架;管理电源设备注册、属性访问、事件通知;封装sysfs 接口和 uevent 通知,屏蔽硬件细节;驱动开发者只需要实现,不关心底层通知和接口细节。简单来说,它是电源子系统的“大脑和中枢”,具体的电池/充电器只是“信息源”。

iTom1900 1488

【Linux】 find指令(文件查找)

每一种操作系统都有成千上万的文件组成,对于linux这样“一切皆文件”的操作系统来说更不例外,大家应该都能很轻松使用windows下的文件查找功能,但是对linux这一功能可能并不是很熟悉,其实想玩linux的你更要牢牢掌握这个命令,因为linux不像windows那样有固定的文件名后缀,并且因为linux阵营下百家争鸣的特性,一个相同的文件在不同的发行版,可能会有不同...

weixin_34124939的博客 643

Visual Studio Git 使用指南

Visual Studio 2022深度集成了Git功能,提供了直观的图形界面管理源代码。本文详细介绍了VS中Git的使用方法:从环境准备、克隆/创建仓库开始,重点讲解了"Git更改"窗口的日常操作,包括查看修改、暂存文件、提交推送等。同时涵盖了分支管理、查看历史、解决冲突等核心功能,以及储藏、重置等高级操作。文章还提供了实用技巧,如提交规范、频繁提交等最佳实践。VS的Git集成覆盖了日常开发需求,图形界面与命令行可互补使用,适合各层次开发者高效管理代码版本。

guigenyi的专栏 1567

计算机网络实验六 RIP 配置实验

Man! What can I say?

AClerk的博客 1592

linux find命令使用教程

find是用来在文件系统上查找复合条件的文件find命令有非常强大的灵活性,可以指定丰富的搜索条件(如文件权限属主属组文件类型日期和大小等)来定位系统中的文件和目录,同时还支持对对搜索的结果进行命令操作。

叶新东老师的博客 963

A2B音频总线在智能座舱中的应用

车载A2B链路音频是智能座舱的核心功能,涵盖车载音响、语音识别、e-Call、消噪及回声消除等应用,随着汽车智能网联化的发展,对音频的开发要求也越来越高,需要加入更先进、功能丰富的信息娱乐功能以满足消费者的用户体验。但传统的模拟并行音频信号传输方式,难以在功能增加与整车轻量化(线缆的重量及成本减少)之间取得均衡。这时候就需要A2B音频总线了。A2B音频总线用于连接车辆中的音频设备,如麦克风(MIC)、功放(AMP)、多媒体主机(Head Unit)和T-BOX等。通过一条低成本的非屏蔽双绞线UTP,进行跨距

怿星科技的博客 3934

linux-find command(transferred)

一、find 命令格式1、find命令的一般形式为;2、find命令的参数;3、find命令选项;4、使用exec或ok来执行shell命令;二、find命令的例子;1、查找当前用户主目录下的所有文件;2、为了在当前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件;3、为了查找系统中所有文件长度为0的普通文件,并列出它们的完整路径;4、查找/var/logs...

200

医学图像处理入门:如何用Python将CT灰度图转换为伪彩色图像(附代码)

本文详细介绍了如何使用Python将CT灰度图转换为伪彩色图像,提升医学图像的可读性和诊断效率。通过OpenCV和Matplotlib等工具,实现灰度值到彩色空间的映射,特别适用于突出显示特定组织密度差异。文章包含完整代码示例和临床实用技巧,适合医学影像分析初学者和Python开发者。

gitlab7runner的博客 326

复现:Mamba-UNet:降水临近预报的创新解决方案

Mamba-UNet通过其创新的双分支Mamba融合模块、多尺度时空注意力模块和动态分位数加权损失函数,在降水临近预报领域取得了显著的性能提升。它不仅在多种降水场景下表现出色,尤其在预测强降水事件方面展现出了强大的能力。这一创新模型为降水临近预报提供了一种新的解决方案,有望在未来的研究和应用中发挥更大的作用,为应对气候变化带来的挑战提供有力支持。随着技术的不断发展和优化,我们期待Mamba-UNet在更广泛的气象预报任务中发挥更大的价值,为各行各业提供更精准、更可靠的降水预报服务。

hanfeng5268的博客 913

Linux系统-find命令

find命令用来在指定目录下查找文件。任何在参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件(递归遍历文件夹)。并且将查找到的子目录和文件全部进行显示。

m0_58341177的博客 2356

【WorkBuddy专栏09】WorkBuddy多模态能力深度揭秘

【WorkBuddy专栏09】AI不止会聊天——WorkBuddy多模态能力深度揭秘

AI人人懂 2108

Mommy, I found it! — 15 Practical Linux Find Command Examples(PART 1)

by SATHIYAMOORTHY on MARCH 4, 2009 Apart from the basic operation of looking for files under a directory structure, you can also perform several practical operations using find command

fengzei886的专栏 527

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

OV9734 DATASHEET,完整规格书

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

Linux中find命令

1.find命令1.find命令Linux find 命令用于在指定目录下查找文件和目录。它可以使用不同的选项来过滤和限制查找的结果。2.基本语法find [⽂件路径] [选项选项的值-name *-type f|d。

weixin_69209121的博客 5933

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

linux系统查找文件夹命令,Linux系统文件查找命令find的基本使用及其高级用法

在Linux系统维护工作时,经常会查找文本文件。例如:假如:web服务器经常出现延迟,或者经常会出现一些参数的错误信息。1、考虑带宽问题2、考虑我的配置文件问题:例如:是不是被其它人更改过所造成的参数、延迟等信息,这样我们就会用到find命令来查找文件的相关信息,并加以修改。如果我们做系统运维这方面的工作时,会经常用到find命令来帮助我们完成文件的查找工作,所以我们要认真学习find命令的使用及...

weixin_35645077的博客 805

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

学习_Linux_Command_find_01

linux 命令 find 用法:find path -option [ -print ] [ -exec -ok command ] {} \; 使用说明: expression 中可使用的选项有二三十个之多,在此只介绍最常用的部分。 -exec:对搜索的结果执行指定的shell命令。注意格式要正确:"-exec 命令 {} \;"。注意“{}” 与\;之间有空格。 -mount,-x

221

linux command find

Linux commandfind 【Purpose】 Learning linux command nmon 【Eevironment】 Ubuntu 16.04 terminal 【Procdeure】 example1: find file size bigger than 50M find ./ -type f ...

weixin_30900589的博客 150

find(1) command

find 命令用于在指定目录查找文件。find 可以指定一些匹配条件,如按文件名、文件类型、文件属主甚至是时间戳来查找文件,默认递归查找。

Doublelv 的博客专栏。 2443

find command基本使用

find命令通常用于根据文件名查找文件,这是最基本用法。 find [path] -name/-iname [filename] path写要查找的路径,自动递归查找 filename写文件名,可以使用通配符*还有其他什么的表达式 具体细节请man find查阅文档。 ...

X 614

find command

find命令 -- 之查找指定时间内修改过的文件 比如我们要查找linux下指定时间内做过改动的文件,我们可以用find命令,其实find命令的功能十分强大,下面我们通过几个简单的例子来学习下find命令的简单用法: find /opt -iname "*" -atime 1 -type f 找出 /opt 下一天前访问过的文件 选项 OPTIONS 所有的选项都总是返回真值,它们总会被

弱水三千 只取一韶 1120

linux find command

linux find command

hellochenlian的专栏 910

win10解决l开机grub页面问题

关于win10启动后出现grub页面 出现原因:电脑非正常关机:死机/黑屏/强制关机等 eg: 笔者强制关机后 开机出现grub引导页面 因为之前安装过Linux所以grub可能被改变 考虑到开发机配置环境复杂选择查资料解决 grub1与grub2有区别 在grub2中没有root/rootnoverify等命令 所以开始解决找资料过程中 根据资料解决发现 grub can’t find … 其实...

qq_42239081的博客 1万+

linux指令 find指令详解

find查找 ,搜索具有即时性的特点,不需要数据库,相对会比较慢一些1、从指定路径下递归向下搜索文件2、支持按照各种条件方式搜索3、支持对搜索得到的文件进一步用指令语法: # find [路径] [参数] [表达式]常用参数-user可以根据文件拥有者来查找文件 # find /root -user root在root目录下查找...

weixin_33766805的博客 206

Linux find command

原贴:http://blog.chinaunix.net/u/19782/showart_374813.html     Linux find command

最实用的Linux博客 1827

(linux) find command

find命令的主要功能是在系统中查找文件或目录: find命令的一般形式为:find pathname -options [-print -exec -ok]print为对匹配文件输出到标准输出,默认为标准输出,故可不写exec为对匹配文件执行该参数给出的shell命令,形式为'command' {} \;ok与exec类似,更安全,每步需要用户确认。 1.-name 根据文件名匹配。例如:fin...

网络飞鸥 246
上一篇: 15 Practical Grep Command Examples In Linux / UNIX
下一篇: Unix Sed Tutorial: Printing File Lines using Address and Patterns
jackie_gnu
博客等级 码龄16年 3粉丝 114原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值