通过删除最后 n 个字符来重命名文件 [英] Rename file by removing last n characters

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

问题描述

我想通过删除最后 N 个字符来重命名文件

I want to rename files by removing the last N characters

例如我想通过删除最后 7 个字符来重命名这些文件

For example I want to rename these files by removing the last 7 characters

来自:

file.txt.123456

致:

file.txt

这在单个命令中可行吗?

Is this doable in a single command?

推荐答案

您可以使用

mv "$file" "${file%???????}"  # 7 question marks to match 7 characters

这将适用于任何符合 POSIX 标准的 shell.

This will work in any POSIX-compliant shell.

要删除最后一个扩展名(可能多于或少于 7 个字符),请使用

To remove the last extension (which may be more or less than 7 characters), use

mv "$file" "${file%.*}"

要修剪给定扩展名后的所有内容,您可以尝试

To trim everything after a given extension, you can try

EXT=csv
mv "$file" "${file%.$EXT.*}".$EXT

实际上删除了 .$EXT 和之后的所有内容,但随后重新附加了 .$EXT.

which actually removes .$EXT and everything after, but then reattaches .$EXT.

这篇关于通过删除最后 n 个字符来重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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