使用终端/bash 更改目录中多个文件的文件扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8551993/
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
Change file extensions of multiple files in a directory with terminal/bash?
提问by CokePokes
I'm developing a simple launchdaemon that copies files from one directory to another. I've gotten the files to transfer over fine.
我正在开发一个简单的启动守护程序,它将文件从一个目录复制到另一个目录。我已经把文件转移过来了。
I just want the files in the directory to be .mp3's instead of .dat's
我只希望目录中的文件是 .mp3 而不是 .dat
Some of the files look like this:
一些文件如下所示:
6546785.8786.dat
3678685.9834.dat
4658679.4375.dat
6546785.8786.dat
3678685.9834.dat
4658679.4375.dat
I want them to look like this:
我希望它们看起来像这样:
6546785.8786.mp3
3678685.9834.mp3
4658679.4375.mp3
6546785.8786.mp3
3678685.9834.mp3
4658679.4375.mp3
This is what I have at the end of the bash script to rename the file extensions.
这是我在 bash 脚本末尾重命名文件扩展名的内容。
cd $mp3_dir
mv *.dat *.mp3
exit 0
Problem is the file comes out as *.mp3 instead of 6546785.8786.mp3
问题是文件作为 *.mp3 而不是 6546785.8786.mp3
and when another 6546785.8786.dat file is imported to $mp3_dir, the *.mp3 is overwritten with the new .mp3
当另一个 6546785.8786.dat 文件被导入到 $mp3_dir 时,*.mp3 被新的 .mp3 覆盖
I need to rename justthe .dat file extensions to .mp3 and keep the filename.
我需要重命名只是中的.dat文件扩展名.MP3并保持文件名。
Ideas? Suggestions?
想法?建议?
回答by fge
Try:
尝试:
for file in *.dat; do mv "$file" "${file%dat}mp3"; done
Or, if your shell has it:
或者,如果您的外壳有它:
rename .dat .mp3 *.dat
Now, why your command didn't work: first of all, it is more than certain that you only had one file in your directory when it was renamed to *.mp3, otherwise mvwould have failed with *.mp3: not a directory.
现在,为什么您的命令不起作用:首先,可以肯定的是,当您的目录重命名为 时,您的目录中只有一个文件*.mp3,否则mv将失败*.mp3: not a directory.
And mv does NOT do any magic with file globs, it is the shellwhich expands globs. Which means, if you had this file in the directory:
并且 mv 对文件 glob 没有任何作用,它是扩展 glob的外壳。这意味着,如果您在目录中有此文件:
t.dat
and you typed:
你输入:
mv *.dat *.mp3
the shell would have expanded *.datto t.dat. However, as nothing would match *.mp3, the shell would have left it as is, meaning the fully expanded command is:
外壳会扩展*.dat到t.dat. 但是,由于没有匹配*.mp3,shell 将保持原样,这意味着完全展开的命令是:
mv t.dat *.mp3
Which will create a file named, literally, *.mp3.
这将创建一个名为*.mp3.
If, on the other hand, you had severalfiles named *.dat, as in:
另一方面,如果您有多个名为 的文件*.dat,例如:
t1.dat t2.dat
the command would have expanded to:
该命令将扩展为:
mv t1.dat t2.dat *.mp3
But this will fail: if there are more than two arguments to mv, it expects the last argument (ie, *.mp3) to be a directory.
但这会失败:如果 有两个以上的参数mv,它期望最后一个参数(即*.mp3)是一个目录。
回答by Matt
For anyone on a mac, this is quite easy if you have BREW, if you don't have brew then my advice is get it. then when installed just simply do this
对于 Mac 上的任何人来说,如果你有 BREW,这很容易,如果你没有 brew,那么我的建议是得到它。然后安装时只需简单地执行此操作
$ brew install rename
then once rename is installed just type (in the directory where the files are)
然后一旦安装了重命名,只需键入(在文件所在的目录中)
$ rename -s dat mp3 *

