从猛砸名删除连字符。 [英] Remove hyphens from filename with Bash

查看:152
本文介绍了从猛砸名删除连字符。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个小的Bash脚本从文件名删除连字符。例如,我要重命名:

I am trying to create a small Bash script to remove hyphens from a filename. For example, I want to rename:

CropDamageVO-041412.mpg

CropDamageVO-041412.mpg

CropDamageVO041412.mpg

CropDamageVO041412.mpg

我是新来的Bash,所以,请温柔:]谢谢您的帮助。

I'm new to Bash, so be gentle :] Thank you for any help

推荐答案

试试这个:

for file in $(find dirWithDashedFiles -type f -iname '*-*'); do
  mv $file ${file//-/}
done

这是假设你的目录没有在名称中破折号。这将打破这一点。

That's assuming that your directories don't have dashes in the name. That would break this.

$ {// VARNAME正则表达式/ replacementText} 语法解释的此处。只要搜索串更换。

The ${varname//regex/replacementText} syntax is explained here. Just search for substring replacement.

此外,这将打破,如果你的目录或文件名有空格的。如果您在文件名中有空格,你应该使用这样的:

Also, this would break if your directories or filenames have spaces in them. If you have spaces in your filenames, you should use this:

for file in *-*; do
  mv $file "${file//-/}"
done

这具有在包含要更改文件的每个目录中运行的缺点,但是,就像我说的,这是一个有点更加强劲。

This has the disadvantage of having to be run in every directory that contains files you want to change, but, like I said, it's a little more robust.

这篇关于从猛砸名删除连字符。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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