find

本文详细介绍Linux/Unix中的find命令,从基本用法到高级技巧,帮助读者掌握如何通过find搜索文件、执行操作及结合xargs使用的方法。
find
find is one of the most useful Linux/Unix tools around, but most people use only a fraction of its power. Many Linux/Unix questions seen online can be solved using the find command alone; it's simply a matter of becoming familiar with its options.

The power of find lets you do anything from finding all your .jpg files to seeing "all of Michael's files that have the execute bit set and have been modified since yesterday." When combined with xargs, a properly wielded find command can make many common tasks ten times easier.


Basics
Let's start simple and get progressively more advanced. To begin with we'll look at finding things by name. Remember that the first argument you give find is where to look.

Find all files with something in their name:
find . -name "*.jpg"


...
./Pictures/iPhoto Library/Data/2006/Roll 20/00697_bluewaters_1440x900.jpg
./Pictures/iPhoto Library/Data/2006/Roll 20/00705_cloudyday_1440x900.jpg
./Pictures/iPhoto Library/Data/2006/Roll 20/00710_fragile_1600x1200.jpg
./Pictures/iPhoto Library/Data/2006/Roll 20/00713_coolemoticon_1440x900.jpg
./Pictures/iPhoto Library/Data/2006/Roll 20/00714_cloudyday_1440x900.jpg
...


Note that by default when you give a location to start from (in our case "."), the find command starts there and drills all the way down during its search. So in this case I started from my home directory and it found the files all the way down in "~/Pictures/iPhoto Library/Data/2006/Roll 20" as well.

** Placing quotes around the search criteria avoids issues with wildcard characters and is probably a good habit to get into. You can also use -iname instead of -name; it's the same but it's case insensitive

Find all files that belong to a certain user:
find . -user daniel


...
./Music/iTunes/iTunes Music/Tool/Undertow/01 Intolerance.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/02 Prison Sex.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/03 Sober.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/04 Bottom.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/05 Crawl Away.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/06 Swamp Song.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/07 Undertow.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/08 4 Degrees.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/09 Flood.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/69 Disgustipated.m4a
...


** Also works for groups (-group)

Find only directories, regular files, links, or sockets:
find . -type d


...
./Development/envelope
./Development/mhp
./Development/mservers
./Development/mservers/fortune100
./Development/mst
./Development/mst/nmap
./Development/mst/services
...


Those are all directories, and to look for the others (files, links, or sockets), just substitute f, l, s for the d in the command above.

Find files that are over a gigabyte in size
find ~/Movies -size +1024M


...
/Movies/Comedy/Funny.mpg
/Movies/Drama/Sad.mpg
...


Combining Arguments
You can also combine arguments using and, or, and not. By default if you use two different arguments you're and'ing them. If you want to use or you give the -o option, and if you want to get everything except something, you use the ! option.

Find only regular files, owned by daniel, that are also jpg images
find . -user daniel -type f -name *.jpg


...
./Pictures/iPhoto Library/autumn_woods.jpg
./Pictures/iPhoto Library/blue_forest.jpg
./Pictures/iPhoto Library/brothers.jpg
...


Now do the same, but exclude anything named autumn
find . -user daniel -type f -name *.jpg ! -name autumn*


...
./Pictures/iPhoto Library/blue_forest.jpg
./Pictures/iPhoto Library/brothers.jpg
...


Forensics
find also has a number of options that help one answer forensics-oriented questions such as when a file was last changed or what files have had their permissions modified recently.

Find all files in /etc owned by root that have been modified within the last day
find /etc -user root -mtime -1


...
/etc/passwd
...


The checks you can use here are:

-atime: when the file was last accessed
-ctime: when the file's permissions were last changed
-mtime: when the file's data was last modified
These searches are done in 24 hour increments and followed by a number n. If you want to match the exact 24 hour period you use n by itself. More frequently, however, you'll want to say everything since yesterday, or everything "more than 3 days ago." This is accomplished using the -n and +n options respectively.

There are also minute versions of the atime, ctime, and mtime arguments:

-amin: when (in minutes) the file was last accessed
-cmin: when (in minutes) the file's permissions were last changed
-mmin: when (in minutes) the file's data was last modified
Show me all files in /etc owned by root that have been accessed within the last two minutes
find /etc -user root -amin -2


...
/etc/hosts
/etc/resolv.conf
...


A list of a few other forensics-oriented options:

-nouser: shows output that's not associated with an existing userid
-nogroup: shows output not associated with an existing groupid
-links n: file has n links
-newer file: file was modified more recently than file.
-perm mode: file has mode permissions.
Show me all files in ~ with wide open permissions
find ~ -perm 777


...
~/testfile.txt
~/lab.rtf
...


Combining find With xargs
This is the piece that we've been leading up to -- performing an action on the stuff that we find with find. So while it's interesting to say, "Show me this stuff", it's far more useful to say, "Take every text file owned by Jason that's hasn't been accessed in 60 days and move it to the backup folder."


Cookbook Examples Of find in action** Be sure to test these on your system before using them for important files

Find all files on your system that are world writable. The 0002 denotes a 2 in the "other" field in the file permissions, which is the write bit
find / -perm -0002


Collect files that are not owned by valid users and delete them
find / -nouser -print0 | xargs -0 rm


Clean the images off of your *nix desktop
find ~/Desktop -name "*.jpg" -o -name "*.gif" -o -name "*.png" -print0 | xargs -0 mv --target-directory ~/Pictures

** The -print0 option terminates results with a null character instead of the default newline, making it cleaner and less likely to balk in many cases

Correct the permissions on your web directory
find /your/webdir/ -type d -print0 | xargs -0 chmod 755
find /your/webdir -type f | xargs chmod 644


Show a list of files in /etc that have been modified since last month
find /etc -mtime -30


A Final Thought
There is a bit of a debate in some circles about using xargs vs. the -exec option that's built into find itself. To me, however, it's not much of a debate; -exec isn't nearly as good as xargs for what I use find for. I tend to use it to do big jobs involving many files. "Move all these files there", "copy all those directories there", "Delete these links.", etc.

This is where -exec breaks down and xargs stands up. Whe you use -exec you run a seperate instance of the called program for each input. With xargs, you build up the input into bundles and run them through the called command as few times as possible, which is usually just once. When dealing with hundreds or thousands of elements this is a big win for xargs.
Don't believe me? Well, let's run some numbers. Below is a listing of 5,310 .jpg files on my OS X system using both -exec and xargs:

time find . -name "*.jpg" -exec ls {} \;


real 0m23.548s
user 0m3.913s
sys 0m15.167s


Hmm, that's not bad. 23 seconds for over five thousand files, right? Let's try it with xargs.

time find . -name "*.jpg" -print0 | xargs -0 ls


real 0m1.526s
user 0m0.667s
sys 0m0.864s


That's 2 seconds vs 24 seconds. Seriously; find and xargs is the combination you want to use.


References
The find man page:
http://www.netadmintools.com/html/find.man.html

The xargs man page:
http://www.research.att.com/~gsf/man/man1/xargs.html
潮汐研究作为海洋科学的关键分支,融合了物理海洋学、地理信息系统及水利工程等多领域知识。TMD2.05.zip是一套基于MATLAB环境开发的潮汐专用分析工具集,为科研人员与工程实践者提供系统化的潮汐建模与计算支持。该工具箱通过模块化设计实现了两大核心功能: 在交互界面设计方面,工具箱构建了图形化操作环境,有效降低了非专业用户的操作门槛。通过预设参数输入模块(涵盖地理坐标、时间序列、测站数据等),用户可自主配置模型运行条件。界面集成数据加载、参数调整、可视化呈现及流程控制等标准化组件,将复杂的数值运算过程转化为可交互的操作流程。 在潮汐预测模块中,工具箱整合了谐波分解法与潮流要素解析法等数学模型。这些算法能够解构潮汐观测数据,识别关键影响要素(包括K1、O1、M2等核心分潮),并生成不同时间尺度的潮汐预报。基于这些模型,研究者可精准推算特定海域的潮位变化周期与振幅特征,为海洋工程建设、港湾规划设计及海洋生态研究提供定量依据。 该工具集在实践中的应用方向包括: - **潮汐动力解析**:通过多站点观测数据比对,揭示区域主导潮汐成分的时空分布规律 - **数值模型构建**:基于历史观测序列建立潮汐动力学模型,实现潮汐现象的数字化重构与预测 - **工程影响量化**:在海岸开发项目中评估人工构筑物对自然潮汐节律的扰动效应 - **极端事件模拟**:建立风暴潮与天文潮耦合模型,提升海洋灾害预警的时空精度 工具箱以"TMD"为主程序包,内含完整的函数库与示例脚本。用户部署后可通过MATLAB平台调用相关模块,参照技术文档完成全流程操作。这套工具集将专业计算能力与人性化操作界面有机结合,形成了从数据输入到成果输出的完整研究链条,显著提升了潮汐研究的工程适用性与科研效率。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值