简体   繁体   English

使用 bash 为文件添加文件扩展名

[英]Add file extension to files with bash

What is the good way to add file extension ".jpg" to extension-less files with bash?使用 bash 将文件扩展名“.jpg”添加到无扩展名文件的好方法是什么?

for f in *.jpg; do mv "$f" "${f%.jpg}"; done
for f in *; do mv "$f" "$f.jpg"; done

You can use rename:您可以使用重命名:

rename 's/(.*)/$1.jpg/' *

Another way - without loops另一种方式 - 没有循环

find . -type f -not -name "*.*" -print0 |\
xargs -0 file |\
grep  'JPEG image data' |\
sed 's/:.*//' |\
xargs -I % echo mv % %.jpg

Breakdown:分解:

  • find all files without extension查找所有没有扩展名的文件
  • check the file type检查文件类型
  • filter out only JPG files仅过滤掉 JPG 文件
  • delete filetype info删除文件类型信息
  • xargs run the "mv" for each file xargs 为每个文件运行“mv”

the above command is for dry run , after it you should remove the "echo" before mv上面的命令是空运行的,在它之后你应该删除 mv 之前的“echo”

EDIT Some people suggesting that here is needed "Wrap path arguments in quotes; avoids argument splitting on paths with spaces" .编辑有些人建议这里需要“将路径 arguments 用引号括起来;避免在带空格的路径上拆分参数”

Usually, this recommendation is true, in this case isn't.通常,这个建议是正确的,在这种情况下不是。 Because, here the % is got replaced not by shell expansion but by the xargs internally (directly), so the % will be substituted correctly even with spaces in filenames.因为,这里的%不是被 shell 扩展替换,而是被内部的xargs (直接)替换,所以即使文件名中有空格, %也会被正确替换。

Simple demo:简单演示:

$ mkdir xargstest
$ cd xargstest

# create two files with spaces in names
$ touch 'a b' 'c d'

$ find . -type f -print
./c d
./a b
# notice, here are spaces in the above paths

#the actual xargs mv WITHOUT quotes
$ find . -type f -print | xargs -I % mv % %.ext

$ find . -type f -print
./a b.ext
./c d.ext
# the result is correct even in case with spaces in the filenames...

Simple, cd to the directory where your files are and:很简单,cd 到你的文件所在的目录,然后:

for f in *;do mv $f $f.jpg;done

dry run:空跑:

 rename -n s/$/.jpg/ *

actual renaming:实际重命名:

 rename s/$/.jpg/ *
find . | while read FILE; do if [ $(file --mime-type -b "$FILE") == "image/jpeg" ]; then mv "$FILE" "$FILE".jpg; fi; done;

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.在我的情况下,我不知道文件类型,所以我在文件命令的帮助下使用mv命令来检查并可能找到文件类型。 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. awk 的用途是剥离从命令文件返回的字符串的第二个字,实际上是扩展名。

rename --dry-run * -a ".jpg" # test  
* -a ".jpg" # rename

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它将使用所有文件名打开您的 $EDITOR,或默认情况下为 vim,您只需在 vim 中使用 Ctrl+v+G 突出显示所有文件名的末尾,保存文件,退出,然后您的所有文件都被重命名

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;

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

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