简体   繁体   English

用bash更改文件扩展名的问题

[英]issue with changing file extensions with bash

find /path/to/files -type f -not -name "M*'.jpg" -exec mv "{}" "{}".mxg \;

I fear I made two mistakes. 我担心我犯了两个错误。

Files are stored in a directory structure. 文件存储在目录结构中。 Goal is to keep the filenames and change the file extension from .jpg to .mxg. 目标是保留文件名并将文件扩展名从.jpg更改为.mxg。 But only for files that have 'M' as the first character of there filename. 但仅适用于以“ M”作为文件名首字符的文件。

The above line has this result: 上一行具有以下结果:

  • all files have .mxg added. 所有文件都添加了.mxg。 So the .jpg isn't and all files are changed. 因此,.jpg不是,所有文件都已更改。

This should do it: 应该这样做:

find /path/to/files -type f -name 'M*.jpg' -exec bash -c 'echo mv "$1" "${1/jpg/mxg}"' -- {} \;

A somewhat cleaner solution is if you have the rename command. 一个更干净的解决方案是,如果您具有rename命令。 However, there are different implementations out there, so read your man page first to check you have the same as mine. 但是,那里有不同的实现,因此请先阅读man页,以检查您与我的man是否相同。 The version I have in Debian is Larry Wall's implementation in perl . 我在Debian中使用的版本是Larry Wall在perl的实现。 You can recognize this by the example rename 's/\\.bak$//' *.bak near the top, or the AUTHOR section near the bottom. 您可以通过示例在顶部附近rename 's/\\.bak$//' *.bak或在底部附近rename 's/\\.bak$//' *.bak AUTHOR部分来识别这一点。 With this implementation you can rename your files like this: 通过此实现,您可以像这样重命名文件:

find /path/to/files -type f -name 'M*.jpg' -exec rename 's/jpg$/mxg/' {} \;

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

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