如何删除文件名中的特殊字符? [英] How to remove special characters in file names?

查看:98
本文介绍了如何删除文件名中的特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建播放列表时,我经常遇到会破坏播放过程的文件.这样的文件将带有空格或撇号.我将使用以下命令对其进行修复

When creating playlists I often came across files that would break the playing process. Such would be files with spaces or apostrophes. I would fix it with the following command

for file in *.; do mv "$file" `echo $file | tr " " '_'` ; done **(for spaces)**

现在,我经常遇到带有逗号,撇号,方括号和其他字符的文件.我将如何修改命令以删除此类字符?

Now I more often come across files with commas, apostrophes, brackets and other characters. How would I modify the command to remove such characters?

还尝试了重命名's/[^ a-zA-Z0-9 _-]//'* .mp4 ,但它似乎并未删除空格或逗号

Also tried rename 's/[^a-zA-Z0-9_-]//' *.mp4 but it doesnt seem to remove spaces or commas

推荐答案

如果您向其中添加 g 修饰符,则您的重命名将起作用,这将执行所有替换而不是只有第一个:

Your rename would work if you add the g modifier to it, this performs all substitutions instead of only the first one:

$ echo "$file"
foo bar,spam.egg

$ rename -n 's/[^a-zA-Z0-9_-]//' "$file"
foo bar,spam.egg renamed as foobar,spam.egg

$ rename -n 's/[^a-zA-Z0-9_-]//g' "$file"
foo bar,spam.egg renamed as foobarspamegg


您可以单独进行 ,并进行参数扩展:

  • 要从文件名中删除除 a-zA-Z0-9 _- 以外的所有内容,请使用字符类 [假定变量 file 包含文件名] [:alnum:] 匹配当前 locale :

  • For removing everything except a-zA-Z0-9_- from file names, assuming variable file contains the filename, using character class [:alnum:] to match all alphabetic characters and digits from current locale:

"${file//[^[:alnum:]_-]/}"

或明确地将 LC_COLLATE 更改为 C :

"${file//[^a-zA-Z0-9_-]/}"

示例:

$ file='foo bar,spam.egg'

$ echo "${file//[^[:alnum:]_-]/}"
foobarspamegg

这篇关于如何删除文件名中的特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆