bash 如何更改目录中多个文件的扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12120326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I change the extension of many files in a directory?
提问by Root
Suppose I have a large number of files in a directory with .txt
extension.
假设我在一个带有.txt
扩展名的目录中有大量文件。
How can I change the extension of all these files to .c
using the following command line environments:
如何将所有这些文件的扩展名更改为.c
使用以下命令行环境:
- Powershell in Windows
- cmd/DOS in Windows
- The terminal in bash
- Windows 中的 Powershell
- Windows 中的 cmd/DOS
- bash中的终端
回答by Smi
On Windows, go to the desired directory, and type:
在 Windows 上,转到所需目录,然后键入:
ren *.txt *.c
In PowerShell, it is better to use the Path.ChangeExtension
method instead of -replace
(thanks to Ohad Schneiderfor the remark):
在 PowerShell 中,最好使用该Path.ChangeExtension
方法而不是-replace
(感谢Ohad Schneider的评论):
Dir *.txt | rename-item -newname { [io.path]::ChangeExtension($_.name, "c") }
For Linux (Bash):
对于 Linux (Bash):
for file in *.txt
do
mv "$file" "${file%.txt}.c"
done