背景
我们经常会使用 find 命令, 奈何 find命令实在不怎么好用, 于是写一个python 脚本来包装 find命令,让它更友好一些,使用它可以极高的提高效率. 它可以让我们以更少的输入来快速完成原来很复杂的查询, 而且会打印出生成的底层语句, 来看几个例子吧
例子
以前我要使用 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 f 和 find -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
使用
方法1
我将提供下面一段脚本, 只要你:
- 将它命令为 zfind, 不要带
.py后缀, 使用chmod a+x zfind成为可执行文件 - 把它放到可执行文件的查找路径下
- 使用 zfind 关键字 就可以愉快地使用了 因为我喜欢使用markdown 来写文档, 所以我默认让 find 命令查找 markdown 文件
方法2
pip install zcommands-zx
脚本展示
#! /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='')
缺点
目前还没有发现
Metadata
Release files for zcommands-zx 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| zcommands-zx-0.0.3.tar.gz | 14.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| zcommands_zx-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.4 kB
Release files / zcommands-zx-0.0.3.tar.gz
| Download URL | zcommands-zx-0.0.3.tar.gz |
|---|---|
| Size | 14.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0efc189f6194d43bf322afec08a36c0f6c45e1a57e2151d64ec0d811addf2663
|
|
BLAKE2b-256 checksum How to use checksums |
46e713cf0d4a5e6c5e3e257d49c9d779cc7ea03c86b0e3a91288c91b83c2e9e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5
|
Release files / zcommands_zx-0.0.3-py3-none-any.whl
| Download URL | zcommands_zx-0.0.3-py3-none-any.whl |
|---|---|
| Size | 15.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
05f134c4ac5ac3e410cb8ef16b74d00b79d9d6d926d86a21fdd465519e4d3f13
|
|
BLAKE2b-256 checksum How to use checksums |
01abd0f55310d13213fbff91029ee5e1f54292920477c1e70e6df1e242afdd17
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5
|