通过交换文本来重命名文件 [英] Rename file by swaping text

查看:64
本文介绍了通过交换文本来重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过交换一些文本来重命名文件.例如:

I need to rename files by swaping some text. I had for example :

CATEGORIE_2017.pdf
CLASSEMENT_2016.pdf
CATEGORIE_2018.pdf
PROPRETE_2015.pdf
...

我想要他们

2017_CATEGORIE.pdf
2016_CLASSEMENT.pdf
2018_CATEGORIE.pdf
2015_PROPRETE.pdf

我想出了这个bash版本:

I came up with this bash version :

ls *.pdf | while read i
do 
    new_name=$(echo $i |sed -e 's/\(.*\)_\(.*\)\.pdf/\2_\1\.pdf/')
    mv $i $new_name
    echo "---"
done

这很有效,但对我来说却很笨拙.有没有人有一个更聪明的解决方案,例如使用 rename ?

It is efficient but seems quite clumsy to me. Does anyone have a smarter solution, for example with rename ?

推荐答案

使用重命名,您可以像这样进行重命名:

Using rename you can do the renaming like this:

rename -n 's/([^_]+)_([^.]+).pdf/$2_$1.pdf/g' *.pdf

选项 -n 不执行任何操作,仅显示将发生的情况.如果满意,请删除 -n 选项.

The option -n does nothing, it just prints what would happen. If you are satisfied, remove the -n option.

我使用 [^ _] + [^.] + 捕获 _ 之前和之后的文件名部分..语法 [^ _] 表示除 _ 之外的所有内容.

I use [^_]+ and [^.]+ to capture the part of the filename before and after the the _. The syntax [^_] means everything but a _.

这篇关于通过交换文本来重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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