linux中find命令的用法

同grep一样,这是一个shell中用法非常丰富,重要性很大的命令,所以我也用一篇文章总结一下其用法。

先用一张图片展示命令的基本语法格式:

Slice_find-crunch.png

基本、常用命令组合方式

find . -name ‘filename’
基于文件名搜索。把-name换成-iname,可以忽略文件名的大小写

find . -path ‘*/aireason/*’
-name 换成 -path,会搜索整个路径名,而不仅仅是文件名

-regex参数和-path用法类似,只不过前者用正则表达式匹配路径名;-iregex忽略文件名的大小写
find . -iregex “.*\(\.py\|\.sh\)$”
疑问:为什么要加反斜杠?

能用!表达否定
比如find . ! -name “*.txt”会找到所有不以txt为扩展名的文件

find默认对目录进行递归搜索,直到遍历所有制定目录下的文件和文件夹。但可以用-maxdepth 、-mindepth来限定搜索的深度
最好先制定搜索深度,再指定搜索的文件类型,这样搜索效率高

限定搜索的文件类型

-type参数来限定文件类型,有下面几种类型

普通文件 f
符号链接 l
目录 d
字符设备 c
块设备 b
套接字 s
FIFO p

与时间有关的参数 🕙

指定时间

  • -atime: time of last access (ls -lu)
  • -mtime: time of last modification (ls -l)
  • -ctime: time of last status change (ls -lc)

-mmin, -amin,-cmin以分钟为单位,其他含义相同

三者用法类似,后面接一个整数,可以不带正负号,或者带正号、负号。下面是用法图解,间距是24h(-mtime)或一分钟(-mmin)

Group 388-crunch.png

比如 find . -type f -mtime +3代表查找当前目录下,访问时间在距此刻四天(4*24h)之前的所有文件

指定参考文件

另一个和时间有关的参数是-newer,这个参数带上一个参考文件,可用于寻找比参考文件更新的(更近的修改时间)的文件。
find . -newer file.txt,就能找到比file.txt更近时间修改的文件。

限定文件大小 📐

-size [+/-]number[k, b, c, w...]
后面接带正号或负号的整数,正好表示比数字大的文件,数字后面再接上单位,单位如下所示

🌀 b: 512 byte blocks (块,512字节)
🌀 c: Bytes (字节)
🌀 w: Two-byte words (字,两字节)
🌀 k: Kilobyte (1024 bytes)
🌀 M: Megabyte (1024 kilobytes)
🌀 G: Gigabyte (1024 megabytes)

find . -type f -name '*.pdf' -size +50000k -exec ls -l {} \;
这句话可以查找文件大于50兆的pdf文件

-delete

-delete可用来删除所匹配到的文件,比如下面命令可以删除所有以swp为扩展名的文件
find . -type f -name "*.swp" -delete

Match based on the file permissions and ownership

It is possible to match files based on the file permissions. We can list out the files having specified file permissions as follows:
find . -type f -perm 644 -print # Print files having permission 644

-perm specifies that find should only match files with their permission set to a particular value.

As an example usage case, we can consider the case of the Apache web server. The PHP files in the web server require proper permissions to execute. We can find out the PHP files that don’t have proper execute permissions as follows:
find . -type f -name “*.php” ! -perm 644 -print

We can also search files based on ownership of the files. The files owned by a specific user can be found out using the -user USER option.
The USER argument can be a username or UID.

For example, to print the list of all files owned by the user slynux, you can use the following command:
find . -type f -user slynux -print

使用-exec对找到的文件进行进一步操作

用法: ::-exec COMMAND {} ;::

find . -name "*.c" -exec cat {} \; > all_c.txt
解释一下,{}代表每个找到的文件,后面要用;结尾,但是分号在shell中有特殊含义,所以需要转义。
然后,find命令的全部输出只是一个数据流,所以不需要使用>>

-exec后面不支持接多个命令,但是可以把多个命令写到脚本里,然后执行脚本。

我经常这样使用该命令:
find . -name "*.sh" -exec ls -l {} \;

让find跳过某些目录

使用-prune(修剪)参数
find . \( -name ".git" -prune \) -o \( -type f \)
此命令打印出不包括.git目录在内的所有文件,分为两部分,第一部分\( -name ".git" -prune \)用来排除某文件夹,第二部分\( -type f \)说明了要执行的动作

其他搜索文件的命令

Use a simpler command

Generally, source for a project is likely to be in one place, perhaps in a few subdirectories nested no more than two or three deep, so you can use a (possibly) faster command such as
cd /path/to/project; ls *.c */*.c */*/*.c

Speeding up locate

Ensure it is indexing the locations you are interested in. Read the man page and make use of whatever options are appropriate to your task.

   -U <dir>
          Create slocate database starting at path <dir>.

   -d <path>
          --database=<path> Specifies the path of databases to search  in.


   -l <level>
          Security  level.   0  turns  security checks off. This will make
          searchs faster.   1  turns  security  checks  on.  This  is  the
          default.

I used the "speeding up locate" part of RedGrittyBrick's answer. I created a smaller db:

updatedb -o /home/benhsu/ben.db -U /home/benhsu/ -e "uninteresting/directory1 uninteresting/directory2"

then pointed locate at it: locate -d /home/benhsu/ben.db

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335