[英]Linux command to give all files in a directory a file extension
I came up with this: 我想出了这个:
for i in *;
do input=`echo $i|sed -e 's/[ ]/\\\ /g'`;
output=`echo $input".mp3"`;
mv $input $output; done
Its very close, but it mv complains: 它非常接近,但它抱怨:
mv: invalid option -- '\\'
mv:无效选项 - '\\'
I don't quite get why it won't work if I try echoing $input or $output they both seem to properly escape all the spaces in the file names and nothing more. 我不太明白为什么它不起作用如果我尝试回显$ input或$ output他们似乎都正确地逃避了文件名中的所有空格而已。
find . -type f -exec mv "{}" "{}.mp3" \\;
It looks like you are massively over-complicating this with your use of sed
(and echo
)... 看起来你使用
sed
(和echo
)会大大过度复杂化......
You are trying to escape space characters in file names, but you really just need to enclose the filename in quotation marks. 您正试图在文件名中转义空格字符,但实际上您只需要将文件名括在引号中。 You can simply do...
你可以简单地做......
for i in *; do
mv "${i}" "${i}.mp3"
done
Shorter and safer : 更短更安全:
for i; do
mv -- "${i}" "${i}.mp3"
done
=) =)
--
takes care of files starting with a dash --
处理以破折号开头的文件
and for i in *
is shortened by for i
for i in *
来说, for i
缩短了
Use more quotes : they are vital. 使用更多报价 :它们至关重要。 Also, learn the difference between
'
, "
and ` . See http://mywiki.wooledge.org/Quotes and http://wiki.bash-hackers.org/syntax/words . ( sentence stolen from IRC freenode #bash ) 另外,了解
'
, "
和`之间的区别。请参阅http://mywiki.wooledge.org/Quotes和http://wiki.bash-hackers.org/syntax/words 。(从IRC freenode #bash中窃取的句子)
使用重命名 ,它使这些事情变得非常简单:
rename 's/$/.mp3/' *
for i in *; do mv -- ${i}{,.mp3};done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.