Add file extension to files with bash

后端 未结 10 1397
猫巷女王i
猫巷女王i 2020-11-29 16:24

What is the good way to add file extension \".jpg\" to extension-less files with bash?

10条回答
  •  借酒劲吻你
    2020-11-29 16:56

    In my case i was not aware of the filetype so i used the mv command with the help of the file command to examine and possibly find the file type. This solution might not be perfect for all files since the file command might not recognize the filetype but it worked mostly good for me.

    for f in *; do ext=$(file $f | awk '{print $2;}'); mv -n "$f" "$f.$ext"; done
    

    The use of awk is to strip the second word of the string returned from the command file that is actually the extension.

提交回复
热议问题