[英]Renaming files in bash script using mv
I recovered many MOV files from a faulty hard drive however software named these files as per block location: 我从有故障的硬盘驱动器中恢复了许多MOV文件,但是软件按块位置命名了这些文件:
263505816.mov etc... 263505816.mov等...
I wrote a small script that uses mediainfo application so i can read date and time created and rename files accordingly: 我编写了一个使用mediainfo应用程序的小脚本,因此我可以读取创建的日期和时间,并相应地重命名文件:
for f in *.mov
do
MODIFIED=$(mediainfo -f $f |grep -m 1 "Encoded date" |sort -u |awk -F "UTC " '{print $2}')
DATECREATED=$(echo $MODIFIED |cut -d' ' -f 1)
TIMECREATED=$(echo $MODIFIED |cut -d' ' -f 2 |tr -s ':' '-')
mv $f "$DATECREATED $TIMECREATED.mov"
done
Which works fine but when i modify mv statement by adding 2 words at the end: 效果很好,但是当我通过在末尾添加2个单词来修改mv语句时:
mv $f "$DATECREATED $TIMECREATED Holidays 2011.mov"
i get the following: 我得到以下内容:
mv: target â Holidays 2011.movâ is not a directory
I know i have to mark white spaces is some way because mv is mislead that it's a directory. 我知道我必须以某种方式标记空格,因为mv误导了它是目录。 Other articles do not mention using multiple variable in conjunction with mv that is why im asking for guidance.
其他文章没有提到将多变量与mv结合使用,这就是为什么im要求指导的原因。
Many thanks, 非常感谢,
I think it worked the first time because *.mov
expanded only to numeric files like the one in your example, but after the first execution of your script, it renamed the files introducing spaces in the form $DATECREATED $TIMECREATED.mov
. 我认为这是第一次成功,因为
*.mov
仅扩展到示例文件中的数字文件,但是在脚本第一次执行后,它以$DATECREATED $TIMECREATED.mov
的形式重命名了文件,并引入了空格。 Basically, the second time around, mv
is attempting to move $DATECREATED
and $TIMECREATED.mov
to your target file, and since it's not a directory, it fails. 基本上,
mv
第二次尝试将$DATECREATED
和$TIMECREATED.mov
移至目标文件,并且由于它不是目录,因此失败。
You can solve this by quoting $f
. 您可以通过引用
$f
解决此问题。 Try it like this: 像这样尝试:
mv "$f" "$DATECREATED $TIMECREATED Holidays 2011.mov"
In fact, it's recommended that you always quote variables unless you're sure they won't contain special characters. 实际上,建议您始终加引号,除非您确定变量不包含特殊字符。
Before the $DATECREATED
and after the .mov
you have some characters that look like double quotes but are not so, my guess. 在
$DATECREATED
之前和.mov
之后,您可能会有一些看起来像双引号的字符,但事实并非如此。 Some typographic quotes. 一些印刷报价。 Try replacing with normal double quotes
"
. - in addition to the suggestion to also quote "$f" (with the right double quotes ;-) 尝试用普通的双引号
"
。-代替。除了建议也引用” $ f“(使用正确的双引号;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.