简体   繁体   English

Linux命令为目录中的所有文件提供文件扩展名

[英]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/Quoteshttp://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.

相关问题 Linux命令在目录中查找具有相同名称但扩展名不同的所有文件? - Linux command to find all the files with same name but different extension in a directory? 为目录中的所有文件修剪一部分文件扩展名 - Linux - Trimming a part of file extension for all the files in directory - Linux Linux 命令行:在包含特定文本的目录树中查找具有特定扩展名的所有文件 - Linux command line: Find all files with a certain extension in a directory tree containing specific text 是否有 linux 命令循环遍历目录中的文件,然后将所有文件名和内容写入 csv 文件? - Is there a linux command loop through files in a directory then write all files names and content into csv file? 在linux或windows中更改目录中的所有文件名和扩展名 - Change all file name and extension in directory in linux or windows 将Linux命令递归应用于单一(.sh)类型目录中的所有文件 - Apply Linux command recursively to all files in directory of single (.sh) type Python-提取并修改Linux目录中所有文件中的文件路径 - Python - extract and modify a file path in all files in a directory in linux 生成包含Linux目录中所有文件列表的文件 - Generate file that contains a list of all files in a directory on linux Linux watch命令“没有这样的文件或目录” - Linux watch command “no such file or directory” Linux命令删除所有〜文件 - Linux command for removing all ~ files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM