交换文件名中的第一个和最后四个字符(Swap first and last four characters in a filename)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
交换文件名中的第一个和最后四个字符(Swap first and last four characters in a filename)

堆栈溢出。

我收集了一些工作笔记。 我把它们作为markdown文件保存,并且用日期和年份格式化它们 - 例如,今天的标题是06132017.md

我工作了一年,所以我有很多这样的文件。 我希望将命名约定从第一个月/第一年改为第一年,这样我就可以按字母顺序对它们进行排序,并轻松找到我需要的日期。

因此06132017.md将成为20170613.md - 这将使2016年和2017年不再按照aplha顺序混合。 是否有可以在文件夹上运行的命令来执行此操作?

StackOverflow.

I have a colleciton of notes from work. I keep them as markdown files, and had been formatting them with the date and year - for example, today's is titled 06132017.md

I am coming up on a year at work, so I have quite a few of these files. I wish to change the naming convention from month/day first to year first, so that I can sort them alphabetically and easily find dates I need.

So 06132017.md would become 20170613.md - this would keep 2016 and 2017 from mixing in aplha order. Is there a command I can run on a folder to do this?

最满意答案

如果您有Perl重命名实用程序,则执行起来相当简单:

$ prename 's/^(....)(....)(\.md)$/$2$1$3/' *.md 06132017.md renamed as 20170613.md

点匹配任何字符,括号组,替换侧的$N插入组中捕获的字符。

或者只是在Bash:

$ for x in ????????.md ; do mv -v "$x" "${x:4:4}${x:0:4}.md" ; done '06132017.md' -> '20170613.md'

${var:n:m}取一个长度为m的子字符串,从变量var位置n开始。

If you have the Perl rename utility, it's rather simple to do:

$ prename 's/^(....)(....)(\.md)$/$2$1$3/' *.md 06132017.md renamed as 20170613.md

The dots match any character, the parenthesis group, and $N on the replacement side inserts the characters captured in the groups.

Or just in Bash:

$ for x in ????????.md ; do mv -v "$x" "${x:4:4}${x:0:4}.md" ; done '06132017.md' -> '20170613.md'

${var:n:m} takes a substring of length m, starting at position n from variable var.

更多推荐

本文发布于:2023-04-12 20:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/7a4464457c77ccd3b4d39d4436f9a46d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一个   文件名   字符   filename   characters

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!