bash 递归地为所有文件添加文件扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1108527/
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
recursively add file extension to all files
提问by robjmills
I have a few directories and sub-directories containing files with no file extension. I want to add .jpg
to all the files contained within these directories. I've seen bash scripts for changing the file extension but not for just adding one. It also needs to be recursive, can someone help please?
我有几个目录和子目录包含没有文件扩展名的文件。我想添加.jpg
到这些目录中包含的所有文件中。我见过用于更改文件扩展名的 bash 脚本,但不仅仅是添加一个。它也需要递归,有人可以帮忙吗?
回答by Stephan202
Alternative command without an explicit loop (man find
):
没有显式循环的替代命令 ( man find
):
find . -type f -exec mv '{}' '{}'.jpg \;
Explanation: this recursively finds all files (-type f
) starting from the current directory (.
) and applies the move command (mv
) to each of them. Note also the quotes around {}
, so that filenames with spaces (and even newlines...) are properly handled.
说明:这会递归地查找-type f
从当前目录( ) 开始的所有文件 ( ).
并将移动命令 ( mv
) 应用于每个文件。还要注意 周围的引号{}
,以便正确处理带有空格(甚至换行符...)的文件名。
回答by ghostdog74
this will find files without extension and add your .jpg
这将找到没有扩展名的文件并添加您的 .jpg
find /path -type f -not -name "*.*" -exec mv "{}" "{}".jpg \;
回答by Chad Huneycutt
This is a little late, but I thought I would add that a better solution (although maybe less readable) than the ones so far might be:
这有点晚了,但我想我会补充一个比目前更好的解决方案(虽然可能不太可读)可能是:
find /path -type f -not -name "*.*" -print0 | xargs -0 rename 's/(.)$/.jpg/'
Using the find | xargs
pattern generally results in more efficient execution, as you don't have to fork a new process for each file.
使用该find | xargs
模式通常会提高执行效率,因为您不必为每个文件创建一个新进程。
Note that this requires the version of rename found in Debian-flavored distros (aka prename), rather than the traditional rename. It's just a tiny perl script, though, so it would be easy enough to use the command above on any system.
请注意,这需要在 Debian 风格的发行版(又名 prename)中找到的重命名版本,而不是传统的重命名。不过,它只是一个很小的 perl 脚本,因此在任何系统上使用上面的命令都很容易。
回答by nik
like this,
像这样,
for f in $(find . -type f); do mv $f ${f}.jpg; done
I am not expecting you have space separated file names,
If you do, the names will need to be processed a bit.
我不希望您有空格分隔的文件名,
如果您这样做,则需要对名称进行一些处理。
If you want to execute the command from some other directory,
you can replace the find .
with find /target/directory
.
如果你想从其他目录下执行命令,
你可以代替find .
用find /target/directory
。
回答by Nuno
For renaming all files with no extension in Windows basic you can do ren * *.jpg
Since the file as no extension, just use the *, or if you want to change png to jpg
use ren *.png *.jpg
要在 Windows basic 中重命名所有没有扩展名的文件,您可以执行ren * *.jpg
由于文件没有扩展名,只需使用 *,或者如果您想将 png 更改为 jpg 使用ren *.png *.jpg
回答by dfa
rename
rename
not sure that it can rename files without extensions (I'm on windows 7 right now)
不确定它是否可以重命名没有扩展名的文件(我现在在 Windows 7 上)