大家好,我是欧盆索思(opensource),每天为你带来优秀的开源项目!

F2 是一个快捷键,进行文件重命名。今天推荐一个开源工具,它的名字就叫做 f2,是一个跨平台、快速、安全的批量重命名工具。

项目地址:https://github.com/ayoisaiah/f2,刚开源,使用 Go 语言实现的。

它核心的特色是安全和快速。在速度方面,对比了几个工具:

  • rnm[1] (C++) — v4.0.9
  • rnr[2] (Rust) — v0.3.0
  • brename[3] (Go) — v2.11.0

f2 的结论是它比以上工具都快 2~3 倍。

具体其他特色有:

  • 跨平台,支持 Linux、MacOS 和 Windows;
  • 支持正则表达式过滤文件,并支持分组;
  • 安全,体现在默认执行改名操作,而是预览,只有告诉它改名它才执行;
  • 支持递归重命名;
  • 支持撤回;
  • 。。。

安装该工具,可以通过 go get 安装,也可以通过 npm 安装,因为发布到了 npm 上:

$ npm i @ayoisaiah/f2 -g

以下是 f2 的命名帮助文档:

DESCRIPTION:
  F2 is a command-line tool for batch renaming multiple files and directories quickly and safely

USAGE:
   f2 FLAGS [OPTIONS] [PATHS...]

AUTHOR:
   Ayooluwa Isaiah <ayo@freshman.tech>

VERSION:
   v1.1.0

FLAGS:
   --find string, -f string       Search string or regular expression.
   --replace string, -r string    Replacement string. If omitted, defaults to an empty string.
   --start-num value, -n value    Starting number when using numbering scheme in replacement string such as %03d (default: 1)
   --output-file value, -o value  Output a map file for the current operation
   --exec, -x                     Execute the batch renaming operation (default: false)
   --recursive, -R                Rename files recursively (default: false)
   --undo value, -u value         Undo a successful operation using a previously created map file
   --ignore-case, -i              Ignore case (default: false)
   --ignore-ext, -e               Ignore extension (default: false)
   --include-dir, -d              Include directories (default: false)
   --only-dir, -D                 Rename only directories (implies include-dir) (default: false)
   --hidden, -H                   Include hidden files and directories (default: false)
   --fix-conflicts, -F            Fix any detected conflicts with auto indexing (default: false)
   --help, -h                     show help (default: false)
   --version, -v                  print the version (default: false)

WEBSITE:
  https://github.com/ayoisaiah/f2

看一个具体简单的例子:

$ f2 -f 'Screenshot' -r 'Image'
+--------------------+---------------+--------+
|       INPUT        |    OUTPUT     | STATUS |
+--------------------+---------------+--------+
| Screenshot (1).png | Image (1).png | ok     |
| Screenshot (2).png | Image (2).png | ok     |
| Screenshot (3).png | Image (3).png | ok     |
+--------------------+---------------+--------+

当不提供 -x Flag 时,只是展示改名后的样子,只有加上了 -x,才会真正的执行改名操作。
在项目主页提供了其他例子供参考。

此外,如果你是一个 Go 爱好者,可以学习下它的源码,它使用了 github.com/urfave/cli 这个库,我也是很喜欢这个库。学习开源项目源码,对自己能力会很有帮助。

参考资料

[1] rnm: https://github.com/neurobin/rnm
[2] rnr: https://github.com/ChuckDaniels87/rnr
[3] brename: https://github.com/shenwei356/brename

最后编辑: kuteng  文档更新时间: 2021-03-31 19:14   作者:kuteng