更新时间:2023-12-01 10:23:16
可能的解决方案之一:
find . -name '*.jpg' -printf "'%p' '%h/thumb_%f'\n" | xargs -n2 echo mv
Principe:找到所有需要的文件,并为标准mv
命令准备参数.
Principe: find all needed files, and prepare arguments for the standard mv
command.
注意:
'
包围,以允许在文件名中使用空格.'
撇号本身的文件名.如果您需要移动更奇怪的文件名,请在下面检查.echo
假装mv
.mv
are surrounded by '
for allowing spaces in filenames.'
apostrophe itself, like many mp3 files. If you need moving more strange filenames check bellow.echo
pretending mv
.任何文件名重命名.在外壳中,您需要一个定界符.问题在于,通常文件名(存储在shell变量中)通常可以包含分隔符本身,因此:
ANY filename renaming. In the shell you need a delimiter. The problem is, than the filename (stored in a shell variable) usually can contain the delimiter itself, so:
mv $file $newfile #will fail, if the filename contains space, TAB or newline
mv "$file" "$newfile" #will fail, if the any of the filenames contains "
正确的解决方案是:
可以使用内部的printf
和%q
格式指令= print quoted
在 bash 中准备正确的转义.但是这种解决方案很长而且很无聊.
Preparing the correct escaping in bash is possible with it's internal printf
and %q
formatting directive = print quoted
. But this solution is long and boring.
恕我直言,最简单的方法是使用perl
并使用零填充的print0
,如下所示.
IMHO, the easiest way is using perl
and zero padded print0
, like next.
find . -name \*.jpg -print0 | perl -MFile::Basename -0nle 'rename $_, dirname($_)."/thumb_".basename($_)'
以上使用perl的功能来整理文件名,并最终重命名文件.
The above using perl's power to mungle the filenames and finally renames the files.