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

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

问题描述

我正在尝试通过截断文件名中第一个空格出现的文件来批量重命名某些文件.我编写了一个简单的脚本来重命名:

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

不幸的是,我必须对很多文件执行此操作,大约 670 万个.这给了我以下错误:

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

我已经尝试了所有我能想到的技巧,即使我使用 find 它似乎也无法获取文件.

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

任何帮助将不胜感激.

推荐答案

你有这个错误是因为你使用 *.fa 不仅作为 glob 来迭代,而且还将它扩展到命令单个 rename 命令的行,其中名称列表超出了操作系统的最大参数向量长度.您不会遇到以下问题:

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.*//' {} +

<小时>

也就是说,这个用例根本不需要外部 rename 命令.

# 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天全站免登陆