Add file extension to files with bash

后端 未结 10 1363
猫巷女王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:50
    for f in *.jpg; do mv "$f" "${f%.jpg}"; done
    for f in *; do mv "$f" "$f.jpg"; done
    
    0 讨论(0)
  • 2020-11-29 16:50

    You can use move multiple files. I am a maintainer of this project. The syntax is simple.

    mmf files*
    

    It will open your $EDITOR with all files names, or vim by default and you can simply highlight the end of all file names using Ctrl+v+G in vim , save the file,quit and that it , all your files are renamed

    0 讨论(0)
  • 2020-11-29 16:55

    Ryan Li

    The correct syntax for adding a file extension to multiple files within a directory which do not have a file extension is

    find . | while read FILE; do if [[ -n `file --mime-type "$FILE" | grep 'message/rfc822'` ]]; then  mv "$FILE" "$FILE".eml; fi; done;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题