Unix 重命名文件 [英] Unix Renaming Files

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

问题描述

我想在 UNIX 上使用脚本重命名文件夹中的文件.
原文件格式为:

I want to rename files in a folder on UNIX using a script.
The format of the original file is:

abc.txt.temp

我想将其重命名为:

abc.txt

许多文件都使用这种格式,我想从原始文件名中删除 .temp.

Many files use this format and I want to remove .temp from the original file name.

推荐答案

Ciprian 给出的答案当然是一个选项,但我觉得它是有限的.下面的解决方案更加灵活,因为您不必实际计算任何内容,并且您可以从任何位置移除文本,而不仅仅是从末尾移除.

The answer Ciprian gave is certainly an option but I feel it's limiting. The solution below is much more flexible as you don't have to actually count anything and you can remove text from any position rather than just the end.

以下命令(1 行)将删除所有文件中对 .temp 的任何提及:

The following command (1 line) will remove any mention of .temp in all the files:

for filename in *; do mv "$filename" "${filename//.temp/}"; done

注意*"表示当前文件夹中的所有文件.您可以使用 *.temp 获得与 Ciprian 方法完全相同的结果.(也就是说,只从以 .temp 结尾的文件中删除 .temp)

Note The "*" means all files in current folder. You can use *.temp to achieve exactly the same result as Ciprian's method. (that is, only removing .temp from files ending with .temp)

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

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