在 Linux 上重命名数百万个文件

her*_*err 1 linux rename

我需要重命名大约 200 万张图像。这些文件看起来像这样image.jpg?arg=value,需要重命名为image.jpg不带参数。

这是我目前正在做的事情:

sudo find . -name "*.jpg?*" -exec rename 's/(\?.*)//' {} \;
Run Code Online (Sandbox Code Playgroud)

这完成了工作,但似乎需要很长时间。有人对如何加快速度有建议吗?

Sri*_*niV 5

你能试一下吗

sudo find . -name "*.jpg*" -print0 | xargs -0 -I '{}' -P4 -n1 rename 's/(\?.*)//' {} \;
Run Code Online (Sandbox Code Playgroud)

从手册页xargs

   --max-procs=max-procs
   -P max-procs
          Run  up  to max-procs processes at a time; the default is 1.  If
          max-procs is 0, xargs will run as many processes as possible  at
          a  time.   Use the -n option with -P; otherwise chances are that
          only one exec will be done.
Run Code Online (Sandbox Code Playgroud)

在这里,我将最大子进程限制为 4。如果您想要更多,请标记 -P0,这将占用最大可能的子进程,但请记住,您的 CPU 将严重过载。

或者

使用gnu并行