简体   繁体   中英

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. But only for files that have 'M' as the first character of there filename.

The above line has this result:

  • all files have .mxg added. So the .jpg isn't and all files are changed.

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. However, there are different implementations out there, so read your man page first to check you have the same as mine. The version I have in Debian is Larry Wall's implementation in perl . You can recognize this by the example rename 's/\\.bak$//' *.bak near the top, or the AUTHOR section near the bottom. With this implementation you can rename your files like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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