bash 如何使用bash脚本更改多个文件的扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15829702/
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 to change the extension of multiple files using bash script?
提问by unique_programmer
I am very new with linux usage maybe this is my first time so i hope some detailed help please. I have more than 500 files in multiple directories on my server (Linux) I want to change their extensions to .xml using bash script I used a lot of codes but none of them work some codes i used :
我对 linux 的使用很陌生,也许这是我第一次,所以我希望得到一些详细的帮助。我的服务器 (Linux) 上的多个目录中有 500 多个文件我想使用 bash 脚本将它们的扩展名更改为 .xml 我使用了很多代码,但它们都没有使用我使用的某些代码:
for file in *.txt
do
mv ${file} ${file/.txt}/.xml
done
or
或者
for file in *.*
do
mv ${file} ${file/.*}/.xml
done
i do not know even if the second one is valid code or not i tried to change the txt extension beacuse the prompt said no such file '.txt'
我不知道第二个是否是有效代码我试图更改 txt 扩展名,因为提示说没有这样的文件“.txt”
I hope some good help for that thank you
我希望有一些好的帮助,谢谢
回答by édouard Lopez
Explanation
解释
- For recursivity you need Bash
>=4and to enable**(i.e.globstar) ; - First, I use parameter expansionto remove the string
.txt, which must be anchored at the end of the filename (%) : - the
#anchors the pattern(plain word or glob) to the beginning, - and the
%anchors it to the end. - Then I append the new extension
.xml - Be extra cautious with filename, you should always quote parameters expansion.
- 对于递归,您需要 Bash
>=4并启用**(即globstar); - 首先,我使用参数扩展来删除字符串
.txt,它必须锚定在文件名 (%)的末尾: - 将
#模式(普通词或全局)锚定到开头, - 并将其
%锚定到最后。 - 然后我附加新的扩展名
.xml - 对文件名要格外小心,你应该总是引用参数扩展。
Code
代码
This should do it in Bash(note that I only echothe old/new filename, to actually rename the files, use mvinstead of echo) :
这应该在Bash(请注意,我只有echo旧/新文件名,实际重命名文件,使用mv而不是echo):
shopt -s globstar # enable ** globstar/recursivity
for i in **/*.txt; do
[[ -d "$i" ]] && continue; # skip directories
echo "$i" "${i/%.txt}.xml";
done
回答by Guru
If its a matter of a one or two sub-directories, you can use the rename command:
如果是一两个子目录的问题,您可以使用重命名命令:
rename .txt .xml *.txt
This will rename all the .txt to .xml files in the directory from which the command is executed.
这会将执行命令的目录中的所有 .txt 文件重命名为 .xml 文件。
回答by unique_programmer
I wanted to rename "file.txt" to "file.jpg.txt", used rename easy peezy:
我想将“file.txt”重命名为“file.jpg.txt”,使用rename easy peezy:
rename 's/.txt$/.jpg.txt/' *.txt
重命名 's/.txt$/.jpg.txt/' *.txt
man rename will tell you everything you need to know.
man rename 会告诉你你需要知道的一切。
Got to love Linux, there's a tool for everything :-)
爱上 Linux,一切都有一个工具:-)
回答by Amol Manthalkar
If all the files are in same directory, it can be done using a single command. For example you want to convert all jpg files to png, go to the related directory location and then use command
如果所有文件都在同一目录中,则可以使用单个命令完成。比如你想把所有的jpg文件都转成png,到相关目录位置,然后使用命令
rename .jpg .png *
重命名 .jpg .png *
回答by siz
passing command line argument for dir path
传递目录路径的命令行参数
#!/bin/sh
cd
names_1=`ls`
for file in ${names_1}
do
mv ${file} ${file}.jpg
done

