我试图通过截断文件名中出现第一个空格的文件来批量重命名某些文件。我编写了一个简单的脚本来通过重命名来执行此操作:
for i in *.fa; do rename 's/\s.*//' *.fa; done
这在测试中效果很好,并根据需要产生以下结果:
$:~/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 万个)执行此操作。这给了我以下错误:
/usr/bin/rename: Argument list too long
我已经尝试了我能想到的各种技巧,即使我使用 find 它似乎也无法拾取文件。
$:~/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
命令,其中名称列表超出了操作系统的最大参数向量长度。您不会遇到以下问题:
# run rename once per *.fa file, with only one name given each run
for i in *.fa; do rename 's/\s.*//' "$i"; done
...或其更高效的表亲:
# 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
如果您想保留扩展名,则可以是:
for i in *[[:space:]]*.fa; do
mv -- "$i" "${i%%[[:space:]]*}.fa"
done
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)