`
taylorqt
  • 浏览: 14985 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

find

阅读更多
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
分享到:
评论

相关推荐

    findpeaks.zip_findpeaks_波峰_波峰函数

    "findpeaks.zip_findpeaks_波峰_波峰函数" 提供了一个名为 "findpeaks.m" 的 MATLAB 子函数,用于帮助用户在给定的数据序列中识别这些波峰。下面我们将详细探讨波峰检测的重要性、基本概念以及如何使用这个特定的 ...

    N-FINDR解混算法研究

    ### N-FINDR 解混算法研究 #### 摘要 N-FINDR(Near-Infrared Feature Identification and Determination Routine)是一种快速自主光谱端元确定算法,在高光谱数据处理领域具有重要的应用价值。该算法由Michael E....

    VB 查找函数Find

    在Visual Basic (VB)编程中,`Find`函数是一个非常重要的功能,用于在特定范围内查找满足特定条件的数据。这个函数通常被用在处理大量数据,如电子表格、文本文件或数据库记录时,需要快速定位特定信息的情景。下面...

    用WIN32_FIND_DATA+FindFirst读出文件

    `WIN32_FIND_DATA`结构和`FindFirstFile`、`FindNextFile`函数是微软提供的API,用于实现这一功能。在这个场景中,我们将深入理解这些概念以及如何在VC++环境下使用它们。 `WIN32_FIND_DATA`是一个在Windows API中...

    findpeaks.rar_constructionmgz_findpeak_findpeaks_寻峰_寻峰 matlab

    这个压缩包"findpeaks.rar"包含了名为"constructionmgz_findpeak_findpeaks_寻峰_寻峰 matlab"的相关资料,特别是其中的"findpeaks.m"文件,很可能是一个自定义版本或带有注释的findpeaks实现,对于初学者来说是学习...

    Find Reference 2 插件包

    "Find Reference 2 插件包"是一款专为大型项目设计的高效工具,它在Unity游戏开发环境中扮演着至关重要的角色。在处理复杂的项目时,往往会出现资源冗余,这不仅占用了大量的存储空间,还可能导致性能下降。该插件...

    Mysql中FIND_IN_SET()和IN区别简析

    在MySQL数据库中,`FIND_IN_SET()` 和 `IN` 是两种不同的查询方法,它们在处理数据集时有不同的特性和应用场景。本文将对这两种方法进行详细对比,以帮助理解它们之间的差异。 `FIND_IN_SET()` 函数主要用于在一个...

    Linux find 按文件修改时间查找文件

    ### Linux find 按文件修改时间查找文件 在Linux系统中,`find`命令是非常强大的文件搜索工具之一,它能够帮助用户根据不同的条件查找文件,包括文件名、大小、类型以及文件最后修改时间等。本篇文章将重点介绍如何...

    C# list对象FindAll函数的四种写法

    在本篇文章中,我们将详细探讨`List<T>`的`FindAll`方法,并通过四种不同的写法来演示如何使用这个功能来筛选满足特定条件的元素。`FindAll`方法用于在列表中找到符合指定条件的所有元素,返回一个新的`List<T>`实例...

    Could not find the main class

    "解决 MyEclipse 8.5 中的 “Could not find the main class” 问题" "Could not find the main class" 是一个常见的错误提示,出现于 MyEclipse 8.5 中使用外带的 Tomcat 时,因 JDK 版本问题所引起。下面将详细...

    用richTextBox1.Find实现查找功能,C#源代码

    在实际应用中,我们经常需要为用户提供查找(Find)和替换(Replace)的功能,以便他们能快速定位到特定的文本内容。`richtextbox1.Find`方法就是实现这一功能的关键。 `richtextbox1.Find`方法是`richtextbox`控件...

    gethibernatetemplate的find方法

    gethibernatetemplate的find方法,find(String queryString);find(String queryString , Object value);find(String queryString, Object[] values);findByExample(Object exampleEntity);findByExample(Object ...

    Linux Find命令详解---教你认识强大的Linux Find命令

    Linux Find命令是Linux系统中一个极其重要的工具,它允许用户在文件系统中查找符合特定条件的文件和目录。这个命令的灵活性和强大性使得它成为系统管理员和开发者的必备技能。下面将详细介绍Linux Find命令的一些...

    C++利用 _findfirst与_findnext查找文件的方法

    这些函数和结构体在的头文件中,结构体为struct _finddata_t ,函数为_findfirst、_findnext和_fineclose。具体如何使用,下面来一起看看吧 _findfirst与_findnext查找文件 一、这两个函数均在io.h里面。 二、首先...

    finddata_数据恢复工具

    【标题】"finddata_数据恢复工具"是一个专门设计用于帮助用户在遭遇数据丢失问题时进行数据恢复的应用程序。这款工具适用于多种操作系统环境,包括较旧的Windows版本如WIN98、2000、2003以及较新的Windows XP。它...

    使用find命令查找Linux中的隐藏文件的方法.docx

    使用find命令查找Linux中的隐藏文件的方法 find命令是Linux和Unix系统中最强大和灵活的命令之一,能够根据不同的选项和参数来实现各种文件搜索和管理任务。下面将详细介绍如何使用find命令查找Linux中的隐藏文件。 ...

    Find My Network Accessory Specification R2

    "Find My Network Accessory Specification R2" 是一份针对苹果MFi(Made for iPhone/iPod/iPad)认证配件的开发者文档,旨在详细阐述如何使非Apple设备能够与Apple的"Find My"服务集成,以实现设备定位、追踪和保护...

    解决Cannot find Graphviz

    标题中的“解决Cannot find Graphviz”意味着用户在尝试使用某个软件或工具时遇到了与Graphviz相关的错误。Graphviz是一款开源的图形绘制软件,主要用于自动布局图表,如流程图、网络图和UML图等。在Java开发环境中...

    C++find()函数用法

    在C++编程语言中,`find()`函数是一个非常实用的工具,主要应用于标准库中的算法,尤其是`<algorithm>`头文件中。这个函数允许程序员在序列(如数组、向量或列表)中查找特定元素。在本文中,我们将深入探讨`find()`...

Global site tag (gtag.js) - Google Analytics