bash删除文件名的一部分

前端之家收集整理的这篇文章主要介绍了bash删除文件名的一部分前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下格式的以下文件
$ls CombinedReports_LLL-*'('*.csv
CombinedReports_LLL-20140211144020(Untitled_1).csv
CombinedReports_LLL-20140211144020(Untitled_11).csv
CombinedReports_LLL-20140211144020(Untitled_110).csv
CombinedReports_LLL-20140211144020(Untitled_111).csv
CombinedReports_LLL-20140211144020(Untitled_12).csv
CombinedReports_LLL-20140211144020(Untitled_13).csv
CombinedReports_LLL-20140211144020(Untitled_14).csv
CombinedReports_LLL-20140211144020(Untitled_15).csv
CombinedReports_LLL-20140211144020(Untitled_16).csv
CombinedReports_LLL-20140211144020(Untitled_17).csv
CombinedReports_LLL-20140211144020(Untitled_18).csv
CombinedReports_LLL-20140211144020(Untitled_19).csv

我想删除这部分:
20140211144020(这是报告运行的时间戳,因此会有所不同)

并最终得到类似的东西:

CombinedReports_LLL-(Untitled_1).csv
CombinedReports_LLL-(Untitled_11).csv
CombinedReports_LLL-(Untitled_110).csv
CombinedReports_LLL-(Untitled_111).csv
CombinedReports_LLL-(Untitled_12).csv
CombinedReports_LLL-(Untitled_13).csv
CombinedReports_LLL-(Untitled_14).csv
CombinedReports_LLL-(Untitled_15).csv
CombinedReports_LLL-(Untitled_16).csv
CombinedReports_LLL-(Untitled_17).csv
CombinedReports_LLL-(Untitled_18).csv
CombinedReports_LLL-(Untitled_19).csv

我只是按照mv命令的思路思考,可能是这样的:

$ls CombinedReports_LLL-*'('*.csv

但也许sed命令或其他更好

@H_301_17@
重命名是perl包的一部分.它根据perl样式的正则表达式重命名文件.要从文件名中删除日期:
rename 's/[0-9]{14}//' CombinedReports_LLL-*.csv

如果重命名不可用,可以使用sed shell:

for fname in Combined*.csv ; do mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')" ; done

上面的循环遍历每个文件.对于每个文件,它执行一个mv命令:mv“$fname”“$(echo”$fname“| sed -r’s / [0-9] {14} //’)”where,在这种情况下,sed能够使用与上面的重命名命令相同的正则表达式. s / [0-9] {14} //告诉sed连续查找14位数字并用空字符串替换它们.

猜你在找的Bash相关文章