简体   繁体   English

使用terminal / bash更改目录中多个文件的文件扩展名?

[英]Change file extensions of multiple files in a directory with terminal/bash?

I'm developing a simple launchdaemon that copies files from one directory to another. 我正在开发一个简单的launchdaemon,它将文件从一个目录复制到另一个目录。 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 6546785.8786.dat
3678685.9834.dat 3678685.9834.dat
4658679.4375.dat 4658679.4375.dat

I want them to look like this: 我希望它们看起来像这样:

6546785.8786.mp3 6546785.8786.mp3
3678685.9834.mp3 3678685.9834.mp3
4658679.4375.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 just the .dat file extensions to .mp3 and keep the filename. 我需要重命名只是中的.dat文件扩展名.MP3并保持文件名。

Ideas? 想法? Suggestions? 建议?

Try: 尝试:

for file in *.dat; do mv "$file" "${file%dat}mp3"; done

Or, if your shell has it: 或者,如果你的shell有它:

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 mv would have failed with *.mp3: not a directory . 现在,为什么你的命令不起作用:首先,当你将命令重命名为*.mp3时,你的目录中只有一个文件,否则mv会因*.mp3: not a directory而失败*.mp3: not a directory

And mv does NOT do any magic with file globs, it is the shell which expands globs. 并且mv不会对文件globs做任何魔术,它是扩展globs的shell Which means, if you had this file in the directory: 这意味着,如果您在目录中有此文件:

t.dat

and you typed: 然后你输入:

mv *.dat *.mp3

the shell would have expanded *.dat to t.dat . shell会将*.dat扩展为t.dat However, as nothing would match *.mp3 , the shell would have left it as is, meaning the fully expanded command is: 但是,因为没有任何东西可以匹配*.mp3 ,所以shell会保持原样,这意味着完全展开的命令是:

mv t.dat *.mp3

Which will create a file named, literally, *.mp3 . 这将创建一个名为*.mp3的文件。

If, on the other hand, you had several files 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 )是一个目录。

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. 对于Mac上的任何人来说,如果你有BREW,这很容易,如果你没有酿造,那么我的建议是得到它。 then when installed just simply do this 然后安装时只需这样做

$ brew install rename

then once rename is installed just type (in the directory where the files are) 然后一旦重命名只安装类型(在文件所在的目录中)

$ rename -s dat mp3 *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM