递归地向所有文件添加文件扩展名

时间:2021-05-14 07:08:24

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脚本,但不是仅添加一个。它也需要递归,有人可以帮忙吗?

6 个解决方案

#1


186  

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)应用于每个文件。还要注意{}周围的引号,以便正确处理带空格(甚至是换行符......)的文件名。

#2


51  

this will find files without extension and add your .jpg

这将找到没有扩展名的文件并添加你的.jpg

find /path -type f -not -name "*.*" -exec mv "{}" "{}".jpg \;

#3


9  

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/(.)$/$1.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脚本,所以在任何系统上使用上面的命令都很容易。

#4


4  

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 /目录。

#5


0  

rename

改名

not sure that it can rename files without extensions (I'm on windows 7 right now)

不确定它可以重命名没有扩展名的文件(我现在在Windows 7上)

#6


0  

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基础中没有扩展名的所有文件,你可以执行ren * * .jpg由于文件没有扩展名,只需使用*,或者如果你想将png更改为jpg,请使用ren * .png * .jpg

#1


186  

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)应用于每个文件。还要注意{}周围的引号,以便正确处理带空格(甚至是换行符......)的文件名。

#2


51  

this will find files without extension and add your .jpg

这将找到没有扩展名的文件并添加你的.jpg

find /path -type f -not -name "*.*" -exec mv "{}" "{}".jpg \;

#3


9  

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/(.)$/$1.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脚本,所以在任何系统上使用上面的命令都很容易。

#4


4  

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 /目录。

#5


0  

rename

改名

not sure that it can rename files without extensions (I'm on windows 7 right now)

不确定它可以重命名没有扩展名的文件(我现在在Windows 7上)

#6


0  

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基础中没有扩展名的所有文件,你可以执行ren * * .jpg由于文件没有扩展名,只需使用*,或者如果你想将png更改为jpg,请使用ren * .png * .jpg