/ usr / bin / rename:参数列表太长(批量重命名文件) [英] /usr/bin/rename: Argument list too long (mass renaming files)

查看:1262
本文介绍了/ usr / bin / rename:参数列表太长(批量重命名文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过截断文件名中第一个空格出现的文件来批量重命名某些文件。我已经写了一个简单的脚本来重命名这样做:

  for i in * .fa;重命名's \\ \s。* //'* .fa;完成

这在测试中工作得很好,并根据需要生成以下结果:

  $:〜/ testenv $ ls 
NM_000016.5_LastMex1_4_12 23 0 1 KB882088_3062975-30.fa NM_000016.5_PastMex1_4_12 23 0 1 KB882088_3062975-30 .fa
$:〜/ testenv $ for i in * .fa;重命名's \\ \s。* //'* .fa;完成
$:〜/ testenv $ ls
NM_000016.5_LastMex1_4_12 NM_000016.5_PastMex1_4_12

不幸的是,我必须在大量的文件上做到这一点,大约有670万。这是给我以下错误:

$ p $ / usr / bin / rename:参数列表太长

我已经尝试了所有我能想到的技巧,即使在使用find的时候,似乎也无法拿起文件。

$ $ p $ $ $ $ $ $〜〜〜〜$ / $ $ $ $ $ $ $ NM_000016.5_astMex1_4_12 23 0 1 KB882088_3062975-30.fa NM_000016.5_PastMex1_4_12 23 0 1 KB882088_3062975-30.fa
NM_000016.5_LastMex1_4_12 23 0 1 KB882088_3062975-30.fa
$:〜/ testenv $ find。 -maxdepth 1 -type f -exec sh -c'rename's / \s。* //'* .fa'_ {} \;
find:`./NM_000016.5_PastMex1_4_12 23 0 1 KB882088_3062975-30.fa':没有这样的文件或目录
find:`./NM_000016.5_astMex1_4_12 23 0 1 KB882088_3062975-30.fa':No这样的文件或目录

任何帮助将不胜感激。

*。fa 不只是作为glob迭代,而且还扩展它到个人 rename 命令的命令行中,其中该名称列表超出了操作系统的最大参数向量长度。您将不会遇到以下问题:

$ $ p $ #为每个* .fa文件运行重命名一次,只给出一个名称每个运行
为我in * .fa;重命名's \\ \s。* //'$ i;完成

...或者更高效的堂兄:

 #只会尝试重命名其名称中实际上有空格的文件
#根据需要只运行重命名 - 不是每个文件一次,像上面那样。
找到。 -name'* * [[:space:]] *。fa'-exec rename's / \s。* //'{} +






也就是说,外部的 rename \\ b
$ b

 #将所有文件重命名为空格,然后以.fa结尾,在空格处截断
为我in * [[:space:]] *。fa;做
mv - $ i$ {i %% [[:space:]] *}
完成

如果你想保留扩展名,可以改为:

  for我在* [[:space:]] *。fa; 
mv - $ i$ {i %% [[:space:]] *}。 $ p>

I am trying to mass rename some files by truncating the files where the first space occurs in the file name. I have written a simple script to do this with rename as such:

for i in *.fa; do rename 's/\s.*//' *.fa; done

This works just fine in a test and produces the following results as desired:

$:~/testenv$ ls
NM_000016.5_LastMex1_4_12 23 0 1 KB882088_3062975-30.fa  NM_000016.5_PastMex1_4_12 23 0 1 KB882088_3062975-30.fa
$:~/testenv$ for i in *.fa; do rename 's/\s.*//' *.fa; done
$:~/testenv$ ls
NM_000016.5_LastMex1_4_12  NM_000016.5_PastMex1_4_12

Unfortunately I have to do this on a lot of files, about 6.7 million. This is giving me the following error:

/usr/bin/rename: Argument list too long

I've tried every sort of trick I can think of, even when I use find it can't seem to pick up the files.

$:~/testenv$ ls
NM_000016.5_astMex1_4_12 23 0 1 KB882088_3062975-30.fa   NM_000016.5_PastMex1_4_12 23 0 1 KB882088_3062975-30.fa
NM_000016.5_LastMex1_4_12 23 0 1 KB882088_3062975-30.fa    
$:~/testenv$ find . -maxdepth 1 -type f -exec sh -c 'rename 's/\s.*//' *.fa' _ {} \;
find: `./NM_000016.5_PastMex1_4_12 23 0 1 KB882088_3062975-30.fa': No such file or directory
find: `./NM_000016.5_astMex1_4_12 23 0 1 KB882088_3062975-30.fa': No such file or directory

Any assistance would be greatly appreciated.

解决方案

You have this bug because you use *.fa not just as the glob to iterate over, but also expand it onto the command line of an individual rename command, where that list of names overruns your operating system's maximum argument vector length. You won't have that issue with the following:

# run rename once per *.fa file, with only one name given each run
for i in *.fa; do rename 's/\s.*//' "$i"; done

...or its more-efficient cousin:

# only tries to rename files that actually have a space in their name
# runs "rename" only as many times as necessary -- not once per file, like the above.
find . -name '*[[:space:]]*.fa' -exec rename 's/\s.*//' {} +


That said, the external rename command isn't strictly required for this use case at all.

# Rename all files with a space in their name, then ending in .fa, to truncate at the space
for i in *[[:space:]]*.fa; do
  mv -- "$i" "${i%%[[:space:]]*}"
done

If you want to keep the extension, that may instead be:

for i in *[[:space:]]*.fa; do
  mv -- "$i" "${i%%[[:space:]]*}.fa"
done

这篇关于/ usr / bin / rename:参数列表太长(批量重命名文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆