> [!NOTE] > 本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。 > [English](./README.en.md) · [原始项目](https://github.com/jhspetersson/fselect) · [上游 README](https://github.com/jhspetersson/fselect/blob/HEAD/README.md) > 原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。 # fselect 使用类 SQL 查询查找文件 [![Crates.io](https://img.shields.io/crates/v/fselect.svg)](https://crates.io/crates/fselect) [![build](https://github.com/jhspetersson/fselect/actions/workflows/rust.yml/badge.svg)](https://github.com/jhspetersson/fselect/actions/workflows/rust.yml) ### 为何使用 fselect? 虽然它通常不会完全取代传统的 `find` 和 `ls`,但 **fselect** 具备以下优点: * 类 SQL(并非真正的 SQL,但语法非常宽松!)语法,易于人类理解 * 复杂查询,可通过 [子查询](docs/usage.md#subqueries-for-in-and-exists) 在多个目录间比较结果 * 聚合、统计、日期及其他 [函数](docs/usage.md#functions) * 在压缩包内搜索 * 支持 `.gitignore`、`.hgignore` 和 `.dockerignore`(实验性) * 按图片宽高、EXIF 元数据搜索 * 按音频元数据搜索(MP3、FLAC、Ogg Vorbis、Opus、M4A/AAC、WAV、AIFF、APE、WavPack、Musepack、Speex) * 按扩展文件属性、POSIX ACL 及 Linux capabilities 搜索 * 按文件哈希搜索 * 按 MIME 类型搜索 * 常见文件类型快捷方式 * [交互模式](docs/usage.md#interactive-mode) * 支持 `plocate` 和 `Everything` 索引,实现闪电般快速搜索 * 多种输出格式(CSV、JSON 等) 更多功能正在开发中! ### 安装 #### 从源码安装最新版本 * 安装 [Rust 与 Cargo](https://www.rust-lang.org/en-US/install.html) 及其依赖以构建二进制文件 * 运行 `cargo install fselect` 你可以选择构建带快速索引搜索支持的版本:[voidtools *Everything*](https://www.voidtools.com/) 在 Windows 上(`--features everything`,然后传入 `--everything`),或 Linux 上的 [*plocate*](https://plocate.sesse.net/) (`--features plocate`,然后传入 `--plocate`)。详见 [使用指南](docs/usage.md#index-backed-search-everything--plocate)。 #### Debian/Ubuntu [deb 软件包](https://github.com/jhspetersson/fselect/releases/download/0.10.2/fselect_0.10.2-1_amd64.deb) #### Fedora/RHEL/openSUSE [rpm 软件包](https://github.com/jhspetersson/fselect/releases/download/0.10.2/fselect-0.10.2-1.x86_64.rpm) #### Arch Linux [AUR 软件包](https://aur.archlinux.org/packages/fselect/),,感谢 [@asm0dey](https://github.com/asm0dey) [AUR bin 软件包](https://aur.archlinux.org/packages/fselect-bin/),,感谢 [@4censord](https://github.com/4censord) #### NixOS [`fselect` in `nixpkgs`](https://github.com/filalex77/nixpkgs/blob/1eced92263395896c10cea69e5f60e8be5f43aeb/pkgs/tools/misc/fselect/default.nix),,感谢 [@filalex77](https://github.com/filalex77) #### 其他 Linux 发行版 * x86_64:[glibc](https://github.com/jhspetersson/fselect/releases/download/0.10.2/fselect-x86_64-linux.gz) · [musl(静态)](https://github.com/jhspetersson/fselect/releases/download/0.10.2/fselect-x86_64-linux-musl.gz) * aarch64:[glibc](https://github.com/jhspetersson/fselect/releases/download/0.10.2/fselect-aarch64-linux.gz) · [musl(静态)](https://github.com/jhspetersson/fselect/releases/download/0.10.2/fselect-aarch64-linux-musl.gz) #### Windows 64 位 提供预编译 [二进制文件](https://github.com/jhspetersson/fselect/releases/download/0.10.2/fselect-x86_64-win.zip) #### 通过 winget 在 Windows 上安装 * 安装 [winget](https://github.com/microsoft/winget-cli) * 运行 `winget install -e --id fselect.fselect` #### 通过 Chocolatey 在 Windows 上安装 * 安装 [Chocolatey](https://chocolatey.org/install) * 运行 `choco install fselect` #### 通过 Scoop 在 Windows 上安装 * 安装 [Scoop](https://scoop.sh) * 运行 `scoop install fselect` #### macOS 预编译二进制文件可在 GitHub 下载页面获取(`gunzip`,然后 `chmod +x fselect`): * [Intel(x86_64)](https://github.com/jhspetersson/fselect/releases/download/0.10.2/fselect-x86_64-mac.gz) * [Apple Silicon(aarch64)](https://github.com/jhspetersson/fselect/releases/download/0.10.2/fselect-aarch64-mac.gz) #### 通过 Homebrew 在 Mac 上安装 * 安装 [brew](https://brew.sh) * 运行 `brew install fselect` #### 通过 MacPorts 在 Mac 上安装 * 安装 [MacPorts](https://www.macports.org) * 运行: ``` sudo port selfupdate sudo port install fselect ``` ### 用法 fselect [ARGS] COLUMN[, COLUMN...] [from ROOT[, ROOT...]] [where EXPR] [group by COLUMNS] [order by COLUMNS] [limit N] [offset N] [into FORMAT] ### 交互模式 fselect -i ### 文档 [对所有支持功能的详细说明。](docs/usage.md) ### 示例 查找临时文件或配置文件(完整路径和大小): fselect size, path from /home/user where name = '*.cfg' or name = '*.tmp' Windows 用户可以省略引号: fselect size, path from C:\Users\user where name = *.cfg or name = *.tmp 或者将所有参数放在引号中,例如: fselect "name from /home/user/tmp where size > 0" 在带空格的目录名中搜索(也支持反引号): fselect "name from '/home/user/dir with spaces' where size > 0" fselect "name from `/home/user/dir with spaces` where size > 0" 或者简单地转义单引号: fselect name from \'/home/user/dir with spaces\' where size gt 0 指定文件大小,获取绝对路径并加入结果: cd /home/user fselect size, abspath from ./tmp where size gt 2g fselect fsize, abspath from ./tmp where size = 5m fselect hsize, abspath from ./tmp where size lt 8k fselect name, size from ./tmp where size between 5mb and 6mb 更复杂的查询: fselect "name from /tmp where (name = *.tmp and size = 0) or (name = *.cfg and size > 1000000)" 可以使用子查询: fselect "name from /test1 where size > 100 and size in (select size from /test2 where name in (select name from /test3 where modified in (select modified from /test4 where size < 200)))" fselect "name, path, size from /data as data where exists (select * from /backup as backup where backup.name = data.name)" 子查询也可用于 `FROM` 子句,作为外层查询的数据源: fselect "src.name, src.size from (select path from /projects depth 2 where size > 100) as src where src.name like '%.rs'" 聚合函数(可按需使用花括号,甚至可与普通圆括号组合使用): fselect "MIN(size), MAX{size}, AVG(size), SUM{size}, COUNT(*) from /home/user/Downloads" 格式化函数: fselect "LOWER(name), UPPER(name), LENGTH(name), YEAR(modified) from /home/user/Downloads" 获取最旧文件的年份: fselect "MIN(YEAR(modified)) from /home/user" 如需处理带空格的文件名,请使用单引号: fselect "path from '/home/user/Misc stuff' where name != 'Some file'" 支持 [Rust 风格](https://docs.rs/regex/1.1.0/regex/#syntax) 的正则表达式: fselect name from /home/user where path =~ '.*Rust.*' 否定正则表达式: fselect "name from . where path !=~ '^\./config'" 简单 glob 会自动展开,可与 `=` 和 `!=` 运算符配合使用: fselect name from /home/user where path = '*Rust*' 经典 LIKE: fselect "path from /home/user where name like '%report-2018-__-__???'" 精确匹配运算符,可在禁用正则表达式的情况下搜索: fselect "path from /home/user where name === 'some_*_weird_*_name'" 按日期查找文件: fselect path from /home/user where created = 2017-05-01 fselect path from /home/user where modified = today fselect path from /home/user where accessed = yesterday fselect "path from /home/user where modified = 'apr 1'" fselect "path from /home/user where modified = 'last fri'" 更精确地匹配在下午 3 点至 4 点之间创建的所有文件: fselect path from /home/user where created = '2017-05-01 15' 还可以更精确: fselect path from /home/user where created = '2017-05-01 15:10' fselect path from /home/user where created = '2017-05-01 15:10:30' 支持日期和时间区间(查找自 5 月 1 日以来更新的所有内容): fselect path from /home/user where modified gte 2017-05-01 默认为当前目录: fselect path, size where name = '*.jpg' 在多个位置中搜索: fselect path from /home/user/oldstuff, /home/user/newstuff where name = '*.jpg' 可指定最小和/或最大深度(`depth` 是 `maxdepth` 的同义词): fselect path from /home/user/oldstuff depth 5 where name = '*.jpg' fselect path from /home/user/oldstuff mindepth 2 maxdepth 5, /home/user/newstuff depth 10 where name = '*.jpg' 可选择跟随符号链接(symlinks): fselect path, size from /home/user symlinks where name = '*.jpg' 在压缩包内搜索(目前仅支持 zip 压缩包): fselect path, size from /home/user archives where name = '*.jpg' 或组合使用: fselect size, path from /home/user depth 5 archives symlinks where name = '*.jpg' limit 100 启用 `.gitignore` 或 `.hgignore` 支持: fselect size, path from /home/user/projects gitignore where name = '*.cpp' fselect size, path from /home/user/projects git where name = '*.cpp' fselect size, path from /home/user/projects hgignore where name = '*.py' 按图像尺寸搜索: fselect CONCAT(width, 'x', height), path from /home/user/photos where width gte 2000 or height gte 2000 查找正方形图像: fselect path from /home/user/Photos where width = height 查找名称部分已知但扩展名未知的图像: fselect path from /home/user/projects where name = "*RDS*" and width gte 1 查找老派说唱(old-school rap)MP3 文件: fselect duration, path from /home/user/music where genre = Rap and bitrate = 320 and mp3_year lt 2000 常用文件扩展名的快捷方式: fselect path from /home/user where is_archive = true fselect path, mime from /home/user where is_audio = 1 fselect path, mime from /home/user where is_book != false 使用布尔列的更简便方式: fselect path from /home/user where is_doc fselect path from /home/user where is_image fselect path from /home/user where is_video 查找具有危险权限的文件: fselect mode, path from /home/user where other_write or other_exec fselect mode, path from /home/user where other_all 在文件 mode 中可使用简单的类 glob 表达式,甚至正则表达式: fselect mode, path from /home/user where mode = '*rwx' fselect mode, path from /home/user where mode =~ '.*rwx$' 按所有者的 uid 或 gid 查找文件: fselect uid, gid, path from /home/user where uid != 1000 or gid != 1000 或按所有者或组的名称: fselect user, group, path from /home/user where user = mike or group = mike 查找特殊文件: fselect name from /usr/bin where suid fselect path from /tmp where is_sticky fselect path from /tmp where is_pipe fselect path from /tmp where is_socket 查找带有扩展属性(xattrs)的文件,检查特定 xattr 是否存在,或获取其值: fselect "path, has_xattrs, has_xattr(user.test), xattr(user.test) from /home/user" 将任意文本作为列包含: fselect "name, ' has size of ', size, ' bytes'" 对结果分组: fselect "ext, count(*) from /tmp group by ext" fselect "ext, count(*) from /tmp group by 1" fselect "mime, ext, count(*) from /tmp group by 1, 2" 对结果排序: fselect path from /tmp order by size desc, name fselect modified, fsize, path from ~ order by 1 desc, 3 最后,限制结果数量: fselect name from /home/user/samples limit 5 格式化输出: fselect size, path from /home/user limit 5 into json fselect size, path from /home/user limit 5 into csv fselect size, path from /home/user limit 5 into html ### License MIT/Apache-2.0 --- 由 [JetBrains IDEA](https://jb.gg/OpenSourceSupport) open source license