效率工具2-shell命令-造轮子-find命令增强版-zfind命令

背景

我们经常会使用 find 命令, 奈何 find命令实在不怎么好用, 于是写一个python 脚本来包装 find命令,让它更友好一些,使用它可以极高的提高效率.
它可以让我们以更少的输入来快速完成原来很复杂的查询, 而且会打印出生成的底层语句, 来看几个例子吧

使用

方法1

我将提供下面一段脚本, 只要你:

  1. 将它命令为 zfind, 不要带.py后缀, 使用 chmod a+x zfind成为可执行文件
  2. 把它放到可执行文件的查找路径下
  3. 使用 zfind 关键字 就可以愉快地使用了
    因为我喜欢使用 markdown 来写文档, 所以我默认让 find 命令查找 markdown 文件

方法2

pip install zcommands-zx

安装完成, zfind 命令就可以使用了
接下来可以使用 zfind -h 查看帮助

例子

以前我要使用 find 查找当前目录下的"名称中包含XX的后缀是XX的文档", 而且希望它能忽略大小写, 能查找目录软链接下的内容, 我必须写很长的参数.

case1 简单初体验

比如我之前想查找 当前目录下,名称中含有 make 的markdown 文件, 那么我必须写成:
find -L . -iname "*make*.md" -type f
作为对比, 我现在只用写: zfind make
实际效果如下:

➜  interview zfind make
the command is:  find  -L . -iname "*make*.md" -type f
./writings/cpp_rank/25_0_什么是Cmake_28.md
./writings/cpp-interview/cpp_rank/25_0_什么是Cmake_28.md
./writings/cpp-interview/cpp_basic/18_0_make的用法_11298.md
./writings/cpp-interview/cpp_basic/12_0_make的用法_11291.md
./htmls/cpp-html/make/@makefile写法.htm.md
./htmls/cpp-html/make/@make 命令零基础教程.html.md
./htmls/cpp-html/make/@CMake Tutorial.htm.md
./cpp-interview/cpp_rank/25_0_什么是Cmake_28.md
./cpp-interview/cpp_basic/18_0_make的用法_11298.md
./cpp-interview/cpp_basic/12_0_make的用法_11291.md

case2 指定特定文件后缀查询

再比如, 我想找 当前目录下,名称中含有 make 的 html 文件, 那么我必须写成
find -L . -iname "*make*.html" -type f
作为对比, 我现在只用写: zfind make -s html

实际效果如下:

➜  interview zfind make -s html
the command is:  find  -L . -iname "*make*.html" -type f
./htmls/cpp-html/make/Make 命令零基础教程.html
./htmls/cpp-html/make/makefile - What is the difference between _make_ and _make all__ - Stack Overflow.html

case3 多种文件后缀查询

再比如, 我想找 当前目录下,名称中含有 make 的 html和htm 文件, 那么我必须写成两条语句
find -L . -iname "*make*.html" -type ffind -L . -iname "*make*.htm" -type f
作为对比, 我现在只用写: zfind make -s html+htm

case4 排除特定路径

有的时候,我不想找某个路径, 那么我可以使用 -e 来排除这个路径, e 是 exclude 的首字母. 它强制是模糊查询的.
我写这么一条语句: zfind find -e blog
其实它会生成这么复杂的一条语句.
the command is: find -L . -iname "*find*.md" -type f -print -o -path "*blog*" -prune

脚本展示

#! /Users/zxzx/.conda/envs/scrapy/bin/python  #换成你本机的python解释器路径
#coding=utf-8
import os,sys

help_txt = """
使用 zfind -h 来获取帮助
使用 zfind 关键字 在当前目录查询含有`关键字`的md文档
使用 zfind 关键字 -d 路径 在指定目录查询含有`关键字`的md文档
使用 zfind 关键字 -d 路径 -s 文件后缀 在指定目录查询含有`关键字`的指定后缀文档
"""

######################## 准备变量
search_dir = ''
keyword = ''
suffix = ''
type = ''

args = sys.argv
if len(args) > 1 and args[1] == '-h':
    print(help_txt)
if len(args) >= 2:
    keyword = sys.argv[1]

# 捕捉多余可选参数
opt_args = args[2:]
for i in range(len(opt_args)):
    if(opt_args[i] == '-s'):
        suffix = opt_args[i+1]
    if (opt_args[i] == '-d'):
        search_dir = opt_args[i+1]
    if (opt_args[i] == '-t'):
        type = opt_args[i+1]

######### 执行命令
search_dir = search_dir or '.'
suffix = suffix or 'md'
type = type or 'f'

if type == 'd':
    suffix = ''
else:
    suffix = '.'+ suffix

command = 'find  -L {} -iname "*{}*{}" -type {}'.format(search_dir, keyword, suffix, type)
# example as: find . -iname "*@make*.md"
######### 执行查询软链接命令
print("the command is: ", command)
ret = os.popen(command).readlines()


for line in ret:
    print(line, end='')

缺点

目前还没有发现

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值