bash脚本删除“X”字符的多个文件名的目录中到底多少? [英] Bash script to remove 'x' amount of characters the end of multiple filenames in a directory?

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

问题描述

我有一个目录中的文件名的列表( /路径/到/ local )。我想从所有的文件名中删除特定数目的字符。

示例文件名:

  iso1111_plane001_00321.moc1
iso1111_plane002_00321.moc1
iso2222_plane001_00123.moc1

在每一个文件名祝文件扩展名之前删除的最后5个字符。

例如:

  iso1111_plane001_.moc1
iso1111_plane002_.moc1
iso2222_plane001_.moc1

我相信这可以用 SED 做的,但我不能确定确切的编码。喜欢的东西...

 在/path/to/local/*.moc1文件名;做
    MV $ FILNAME $(回声$文件名| sed的-e的/.....^//');
DONE

...但是,这是行不通的。很抱歉,如果我残杀 SED 选项,我没有与它太多的经验。


解决方案

  MV $ FILNAME $(回声$文件名|。SED -e'S / ..... \\ $ MOC1 //');

 回声$ {文件名%% ?????。MOC1} .moc1

%%是一个bash内部运营...

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.

Example filenames:

iso1111_plane001_00321.moc1
iso1111_plane002_00321.moc1
iso2222_plane001_00123.moc1

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

For example:

iso1111_plane001_.moc1
iso1111_plane002_.moc1
iso2222_plane001_.moc1

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

...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$//');

or

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

%% is a bash internal operator...

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

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