常用开发脚本工具推荐(按场景分类)
1. 文件与目录处理
工具名称 | 简介 | 语言 |
---|---|---|
fd | 更快更现代的 find 替代品,支持正则、并发搜索 | Rust(可直接脚本调用) |
bat | 代替 cat ,带语法高亮和行号 | Rust |
tree | 目录结构可视化(跨平台) | Shell |
duf | 文件磁盘使用分析,比 du 更直观 | Shell/C |
实用 Shell 脚本举例:
# 查找最近7天修改的文件
find ./ -type f -mtime -7
# 批量压缩文件
for f in *.txt; do gzip "$f"; done
2. Python 实用脚本库
名称 | 作用 |
---|---|
rich | 美观打印、进度条、日志增强 |
argparse | 命令行参数解析 |
fire / click | 快速创建 CLI 工具 |
loguru | 强大的日志记录替代 logging |
watchdog | 文件夹变更监听器(自动构建等) |
pandas | 数据处理神器,适合 CSV/Excel |
open3d | 点云、Mesh 可视化与处理 |
实用脚本示例:
# 自动清理超过7天的临时文件
import os, time
for f in os.listdir('/tmp'):
path = f'/tmp/{f}'
if time.time() - os.path.getmtime(path) > 7*86400:
os.remove(path)
3. Git 脚本工具
工具 | 用法 |
---|---|
pre-commit | 自动执行格式化、lint、检查 |
git-quick-stats | 图表形式分析 git 活动 |
.git-hooks | 自定义脚本(提交检查、自动推送等) |
Git Hook 示例:自动格式化代码后提交
# .git/hooks/pre-commit
black . || exit 1
4. 系统监控 / 网络工具
工具 | 功能 |
---|---|
htop | 高级进程管理工具 |
iotop | 磁盘读写速率监控 |
iftop | 实时流量监控 |
ncdu | 磁盘空间交互式浏览 |
自定义命令行工具模板(Python + Fire)
# tools.py
import fire
class Tools:
def clean(self, path='.', days=7):
'''删除 N 天前文件'''
import os, time
for f in os.listdir(path):
p = os.path.join(path, f)
if os.path.isfile(p) and time.time() - os.path.getmtime(p) > days*86400:
os.remove(p)
print(f"Deleted: {p}")
def info(self):
'''打印系统信息'''
import platform
print(platform.uname())
if __name__ == '__main__':
fire.Fire(Tools)
执行命令示例:
python tools.py clean --days=3
python tools.py info