用于删除目录中多个文件名末尾的“x"个字符的 Bash 脚本? [英] Bash script to remove 'x' amount of characters the end of multiple filenames in a directory?

查看:9
本文介绍了用于删除目录中多个文件名末尾的“x"个字符的 Bash 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录 (/path/to/local) 中的文件名列表.我想从所有这些文件名中删除一定数量的字符.

I have a list of file names in a directory (/path/to/local). I would like to remove a certain number of characters from all of those filenames.

示例文件名:

iso1111_plane001_00321.moc1
iso1111_plane002_00321.moc1
iso2222_plane001_00123.moc1

在每个文件名中,我希望删除文件扩展名前的最后 5 个字符.

In every filename I wish to remove the last 5 characters before the file extension.

例如:

iso1111_plane001_.moc1
iso1111_plane002_.moc1
iso2222_plane001_.moc1

我相信这可以使用 sed 来完成,但我无法确定确切的编码.像……

I believe this can be done using sed, but I cannot determine the exact coding. Something like...

for filename in /path/to/local/*.moc1; do
    mv $filname $(echo $filename | sed -e 's/.....^//');
done

...但这不起作用.抱歉,如果我删除了 sed 选项,我对此没有太多经验.

...but that does not work. Sorry if I butchered the sed options, I do not have much experience with it.

推荐答案

 mv $filname $(echo $filename | sed -e 's/......moc1$//');

 echo ${filename%%?????.moc1}.moc1

%% 是 bash 内部操作符...

%% is a bash internal operator...

这篇关于用于删除目录中多个文件名末尾的“x"个字符的 Bash 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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