如何截断目录中所有文件的最后两个字符? [英] How do I truncate the last two characters of all files in a directory?

查看:89
本文介绍了如何截断目录中所有文件的最后两个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单的问题。我目录中的所有文件的格式为 6bfefb348d746eca288c6d62f6ebec04_0.jpg 。我希望它们看起来像 6bfefb348d746eca288c6d62f6ebec04.jpg 。本质上,我想在每个文件名的末尾删除 _0 。我该如何使用bash做到这一点?

So pretty simple question. All of the files in my directory are of the form 6bfefb348d746eca288c6d62f6ebec04_0.jpg. I want them to look like 6bfefb348d746eca288c6d62f6ebec04.jpg. Essentially, I want to take off the _0 at the end of every file name. How would I go about doing this with bash?

推荐答案

使用Perl的独立重命名命令:

With Perl's standalone rename command:

rename -n 's/..(\....)$/$1/' *

如果一切正常,请删除 -n

If everything looks fine, remove -n.

可以使用独立的重命名命令,其语法类似于 sed s / regexp / replacement / 命令。在正则表达式中,匹配一个字符。 \。匹配 $ 匹配结尾行(文件名的末尾)。 是正则表达式中的特殊字符,用于标记一个子表达式(此处为一个和文件名末尾的三个字符),然后可以与 $ 1 重用。 sed 使用 \1 进行第一次反向引用,重命名使用 $ 1

It is possible to use this standalone rename command with a syntax similar to sed's s/regexp/replacement/ command. In regex a . matches one character. \. matches a . and $ matches end of line (here end of filename). ( and ) are special characters in regex to mark a subexpression (here one . and three characters at the end of your filename) which then can be reused with $1. sed uses \1 for first back-reference, rename uses $1.

请参阅:使用sed的反向引用和子表达式

这篇关于如何截断目录中所有文件的最后两个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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