bash 如何从文件名中删除单引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40727912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:26:31  来源:igfitidea点击:

How to remove single quotes from file names

linuxbash

提问by Andy

The files that come in have spaces, single quotes, brackets, square brackets etc.

进来的文件有空格、单引号、方括号、方括号等。

I remove spaces and replace with dots with the following command

我删除空格并使用以下命令替换为点

for file in *.mp4; do mv "$file" `echo $file | tr ' ' '.'` ; done

Then I remove special characters with the following command

然后我使用以下命令删除特殊字符

rename -n 's/[^a-zA-Z0-9_-]//g' "$file"

But for some reason single quotes(') are still present in file names. Is there a way to have clean file names in one command?

但是由于某种原因,文件名中仍然存在单引号(')。有没有办法在一个命令中拥有干净的文件名?

回答by nabin-info

In bash:

在 bash 中:

for file in *.mp4; do dest="${file//[[:space:]]/.}" && mv -i "$file" "${dest//[^[:alnum:]._-]/}"; done

回答by learner

sed command is one of the option for your requirement.

sed 命令是满足您要求的选项之一。

for file in *.mp4 ; do echo ${file}|sed -e "s/ /./g" -e "s/[\[{\']//g" ; done

Example:

例子:

$ for file in *.mp4 ; do echo $file; done
as {df [jk \'hj.mp4
as {df[jk \'hj.mp4
as {df[jk\'hj.mp4
as {df [jk \'.mp4
$
$ for file in *.mp4 ; do echo ${file}|sed -e "s/ /./g" -e "s/[\[{\']//g" ; done
as.df.jk.hj.mp4
as.dfjk.hj.mp4
as.dfjkhj.mp4
as.df.jk..mp4
$