使用 bash 为文件添加文件扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6114004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add file extension to files with bash
提问by Adrian
What is the good way to add file extension ".jpg" to extension-less files with bash?
使用 bash 将文件扩展名“.jpg”添加到无扩展名文件的好方法是什么?
回答by Seth Robertson
for f in *.jpg; do mv "$f" "${f%.jpg}"; done
for f in *; do mv "$f" "$f.jpg"; done
回答by Ryan Li
find . | while read FILE; do if [ $(file --mime-type -b "$FILE") == "image/jpeg" ]; then mv "$FILE" "$FILE".jpg; fi; done;
回答by Kim Stebel
You can use rename:
您可以使用重命名:
rename 's/(.*)/.jpg/' *
回答by jm666
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
- delete filetype info
- xargs run the "mv" for each file
- 查找所有没有扩展名的文件
- 检查文件类型
- 仅过滤掉 JPG 文件
- 删除文件类型信息
- xargs 为每个文件运行“mv”
the above command is for dry run, after it you should remove the "echo" before mv
上面的命令用于试运行,在它之后你应该删除 mv 之前的“echo”
EDITSome people suggesting that here is needed "Wrap path arguments in quotes; avoids argument splitting on paths with spaces".
编辑有些人建议这里需要“将路径参数用引号括起来;避免在带有空格的路径上拆分参数”。
Usually, this recommendation is true, in this case isn't. Because, here the %
is got replaced notby shell expansion but by the xargs
internally (directly), so the %
will be substituted correctly even with spaces in filenames.
通常,这个建议是正确的,在这种情况下不是。因为,这里的 the不是被 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...
回答by Dillon Beresford
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;
回答by bharath
Simple, cd to the directory where your files are and:
简单, cd 到您的文件所在的目录,然后:
for f in *;do mv $f $f.jpg;done
回答by Roman Rhrn Nesterov
rename --dry-run * -a ".jpg" # test
* -a ".jpg" # rename
回答by Alexey
dry run:
试运行:
rename -n s/$/.jpg/ *
actual renaming:
实际重命名:
rename s/$/.jpg/ *
回答by Paolo
In my case i was not aware of the filetype so i used the mvcommand with the help of the filecommand to examine and possibly find the file type. This solution might not be perfect for all files since the filecommand might not recognize the filetype but it worked mostly good for me.
就我而言,我不知道文件类型,因此我在file命令的帮助下使用mv命令来检查并可能找到文件类型。此解决方案可能并不适用于所有文件,因为file命令可能无法识别文件类型,但它对我来说最有效。
for f in *; do ext=$(file $f | awk '{print ;}'); mv -n "$f" "$f.$ext"; done
The use of awk is to strip the second word of the string returned from the command filethat is actually the extension.
awk 的用途是去除命令文件返回的字符串的第二个字,实际上是扩展名。