简体   繁体   English

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

[英]Rename millions of files on Linux

I need to rename about 2 million images. 我需要重命名大约200万张图片。 The files look like this image.jpg?arg=value and need to be renamed to image.jpg without the arguments. 这些文件看起来像image.jpg?arg=value ,需要重命名为image.jpg不带参数。

Here is what I'm currently doing: 这是我目前正在做的事情:

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

This gets the job done but seems to take forever. 这可以完成工作,但似乎要花很多时间。 Does anyone have a suggestion on how to speed this up? 有人对如何加快速度有建议吗?

Can you try 你能试一下吗

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

From the man page of xargs 来自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.

Here I am limiting the max child process to 4. If you want more then mark -P0 which will take max possible child, but remember, your CPU will be heavily overloaded. 在这里,我将最大子进程数限制为4。如果您想要更多子进程,则将-P0标记为可能的最大子进程数,但是请记住,您的CPU将严重过载。

OR 要么

use gnu parallel 使用gnu 并行

parallelize the renaming. 并行化重命名。 Start two (or three, four) shells and run the command. 启动两个(或三个,四个)shell并运行命令。 Be sure that you seperate somehow the images for the commands, so that not 2 commands are run on the same images. 确保以某种方式分隔命令的映像,以便在同一映像上不能运行2个命令。

I tried this on Ubuntu 14.04 but it does not work. 我在Ubuntu 14.04上尝试了此操作,但它不起作用。 The command executed successfully but nothing happened. 该命令成功执行,但没有任何反应。 I figured that the rename regex part is not right. 我发现重命名正则表达式部分不正确。 To check this: 要检查此:

$ echo Screenshot_from_2015-08-17_122834.png.de4Mzv2 | sed 's/(\?.*)//'
Screenshot_from_2015-08-17_122834.png.de4Mzv2

But changing the regex to the following works. 但是将正则表达式更改为以下工作。 $ echo Screenshot_from_2015-08-17_122834.png.de4Mzv2 | $ echo Screenshot_from_2015-08-17_122834.png.de4Mzv2 | sed 's/.[^.]*$//' Screenshot_from_2015-08-17_122834.png sed's /.[^.]*$//'Screenshot_from_2015-08-17_122834.png

Using that in the command suggested by @realspirituals, I have the following files: 在@realspirituals建议的命令中使用该命令,我有以下文件:

$ ls -ltr
Screenshot_from_2015-08-19_114601.png.somegthingy 
Screenshot_from_2015-08-17_122834.png.de4Mzv2 
Screenshot_from_2015-08-17_122455.png.ac84Lk1
Screenshot_from_2015-08-13_154012.png.uNl34sH 
Screenshot_from_2015-08-13_101459.png.53rv1ce 
Screenshot_from_2015-08-13_101437.png.l4Pt0pz 
Screenshot_from_2015-08-13_101230.png.p31Ic4n

$ sudo find . -name "*.png*" -type f -print0 | xargs -0 -I {} -P4 -n1 rename 's/\.[^\.]*$//' {} \;
Screenshot_from_2015-08-19_114601.png 
Screenshot_from_2015-08-17_122834.png 
Screenshot_from_2015-08-17_122455.png
Screenshot_from_2015-08-13_154012.png 
Screenshot_from_2015-08-13_101459.png 
Screenshot_from_2015-08-13_101437.png 
Screenshot_from_2015-08-13_101230.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM