欢迎关注我的公众号 spider-learn

fdhttps://github.com/sharkdp/fd)find 命令的一个更现代的替换。

对比一下

查找名字含有某个字符的文件

OLD

-> % find . -name "*hello*"
./courses/hello_world.go
./courses/chapter_01/hello_world.go
./courses/chapter_01/hello_world
./examples/01_hello_world.go

NEW

-> % fd hello
courses/chapter_01/hello_world
courses/chapter_01/hello_world.go
courses/hello_world.go
examples/01_hello_world.go

使用正则表达式查找

比如说查找符合 \d{2}_ti 模式的文件。find 使用的正则表达式非常古老,比如说在这里我们不能使用 \d,也不能使用 {x} 这种语法。因此我们需要对我们的正则表达式做一些改写。关于find支持的正则表达式这里就不展开了。

fd 默认就是使用的正则表达式作为模式,并且默认匹配的是文件名;而 find 默认匹配的是完整路径。

OLD

-> % find . -regex ".*[0-9][0-9]_ti.*"
./examples/33_tickers.go
./examples/48_time.go
./examples/28_timeouts.go
./examples/50_time_format.go
./examples/32_timers.go

NEW

-> % fd '\d{2}_ti'
examples/28_timeouts.go
examples/32_timers.go
examples/33_tickers.go
examples/48_time.go
examples/50_time_format.go

指定目录

find 的语法是 find DIRECTORY OPTIONS;而 fd 的语法是 fd PATTERN [DIRECTORY]。注意其中目录是可选的。这点个人认为非常好,因为大多数情况下,我们是在当前目录查找,每次都要写 . 非常烦。

OLD

-> % find examples -name "*hello*"
examples/01_hello_world.go

NEW

-> % fd hello examples
examples/01_hello_world.go

直接执行命令

find 会打印帮助信息,而 fd 则会显示当前目录的所有文件。

OLD

-> % find
usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]

NEW

-> % fd
courses
courses/chapter_01
courses/chapter_01/chapter_1.md
courses/chapter_01/chapter_1.pdf
courses/chapter_01/hello_world
courses/chapter_01/hello_world.go

按后缀名查找文件

这是一个很常见的需求,find 中需要使用 -name "*.xxx" 来过滤,而 fd 直接提供了 -e 选项。

OLD

-> % find . -name "*.md"
./courses/chapter_01/chapter_1.md
./courses/chapter_1.md

NEW

-> % fd -e md
courses/chapter_01/chapter_1.md
courses/chapter_1.md

查找中过滤掉 .gitignore 中的文件

find 并没有提供对 .gitingnore 文件的原生支持,更好的方法可能是使用 git ls-files。而作为一个现代工具,fd 则默认情况下就会过滤 gitignore 文件,更多情况请查阅文档。

可以使用 -I 来包含这些文件,使用 -H 添加隐藏文件。

OLD

-> % git ls-files | grep xxx

NEW

-> % fd xxx

排除某个文件夹

OLD

-> % find . -path ./examples -prune -o -name '*.go'
./courses/hello_world.go
./courses/chapter_01/hello_world.go
./examples

NEW

-> % fd -E examples '.go$'
courses/chapter_01/hello_world.go
courses/hello_world.go

使用 xargs

一般来说,如果使用管道过滤的话,需要使用 '\0' 来作为字符串结尾,避免一些潜在的空格引起的问题。

find 中需要使用 -print0 来调整输出 '\0' 结尾的字符串,在 xargs 中需要使用 -0 表示接收这种字符串。而在 fd 中,和 xargs 保持了一直,使用 -0 参数就可以了。

OLD

-> % find . -name "*.go" -print0 | xargs -0 wc -l
7 ./courses/hello_world.go
7 ./courses/chapter_01/hello_world.go
50 ./examples/07_switch.go
...

NEW

-> % fd -0 -e go | xargs -0 wc -l
7 courses/chapter_01/hello_world.go
7 courses/hello_world.go
7 examples/01_hello_world.go
...

总之,fd 命令相对于 find 来说相当简单易用了

PS

使用 exec Using exec

OLD

-> % find . -name "*.md" -exec wc -l {} \;
114 ./courses/chapter_01/chapter_1.md
114 ./courses/chapter_1.md

NEW

You could also omit the {}

-> % fd -e md --exec wc -l {}
114 courses/chapter_1.md
114 courses/chapter_01/chapter_1.md

欢迎关注我的公众号 spider-learn

fd - 更好的 find 命令的更多相关文章

  1. 更安全的rm命令,保护重要数据

    更安全的rm命令,保护重要数据 网上流传的安全的rm,几乎都是提供一个rm的"垃圾"回收站,在服务器环境上来说,这实非良方. 我想,提供一个安全的rm去保护一些重要的文件或目录不被 ...

  2. IP地址更改小工具(bat命令)

    为了方便切换IP地址,特编制bat命令代码来实现,将以下代码复制到txt文本中,然后保存为bat文件,双击bat文件运行即可. 通过bat命令运行,自动修改IP地址,代码如下: @echo off c ...

  3. 9 个让 JavaScript 调试更简单的 Console 命令

    一.显示信息的命令 <!DOCTYPE html> <html> <head> <title>常用console命令</title> < ...

  4. linux上的常见命令掌握

    http://coolshell.cn/articles/8883.html 这篇文章来源于Quroa的一个问答<What are some time-saving tips that ever ...

  5. 【Linux 运维】查看网络连接状态信息之netstat和ss命令详解

    一.netstat 常用命令详解 通过man netstat可以查看netstat的帮助信息: netstat 命令:用于显示各种网络相关信息,如网络连接,路由表,接口状态,无效连接,组播成员 等等. ...

  6. [置顶] Linux 常用命令集锦

    出处:http://www.vaikan.com/what-are-the-most-useful-swiss-army-knife-one-liners-on-unix/ Linux命令行里的&qu ...

  7. linux常用命令技巧

    原文地址 这篇文章来源于Quroa的一个问答<What are some time-saving tips that every Linux user should know?>—— Li ...

  8. Git 内部原理 - (1)底层命令和高层命令 (2Git 对象

    文章摘选自git官网,这里复制下来表示我已阅读并学习过一次这些内容: 无论是从之前的章节直接跳到本章,还是读完了其余章节一直到这——你都将在本章见识到 Git 的内部工作原理和实现方式. 我们发现学习 ...

  9. 【读书笔记】Linux命令行与Shell脚本编程大全

    Linux命令行与Shell脚本编程大全 5.2 shell 的父子关系 命令分组 Command Grouping 主要有两种形式: 一种以小括号包括,命令之间以冒号分隔.也被称为 进程列表: 注意 ...

随机推荐

  1. uvalive 7299 Boggle

    Boggle is a game in which 16 dice with letters on each side are placed into a 4 × 4 grid. Players th ...

  2. Flume学习总结

    Flume学习总结 flume是一个用来采集数据的软件,它可以从数据源采集数据到一个集中存放的地方. 最常用flume的数据采集场景是对日志的采集,不过,lume也可以用来采集其他的各种各样的数据,因 ...

  3. python's @property

    [python's @property] 参考:http://docs.python.org/3/library/functions.html?highlight=property#property

  4. 17.Letter Combinations of a Phone Number(Back-Track)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  5. UNITY WWW使用代码

    string detailURL = "https://www.xxx.xxx."; using (var w = new WWW(detailURL)) { yield retu ...

  6. jave 逻辑运算 vs 位运算 + Python 逻辑运算 vs 位运算

    JAVA中&&和&.||和|(短路与和逻辑与.短路或和逻辑或)的区别 博客分类: 面试题目 Java.netBlog  转自 :http://blog.csdn.net/web ...

  7. MVC三者关系

  8. 使用寄存器点亮LED等

    最基本的输入功能是检测外部输入电平,如把 GPIO引脚连接到按键,通过电平高低区分按键是否被按下. 基本结构分析 2. P-MOS管和 N-MOS管 main.c中的main函数

  9. 前端传递对象列表到WebApi

    public Int64 objectPOC(JObject jsonWrapper) { dynamic jsonValues = jsonWrapper; JArray jsonInput = j ...

  10. (并查集 带关系)Find them, Catch them -- poj -- 1703

    链接: http://poj.org/problem?id=1703 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3676 ...