在 Linux 中删除文件名中的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15347843/
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-08-06 19:14:17  来源:igfitidea点击:

Remove whitespaces from filenames in Linux

regexlinuxrename

提问by Sam Timalsina

I have hundreds of jpg files in different folders like this:

我在不同的文件夹中有数百个 jpg 文件,如下所示:

  • 304775 105_01.jpg
  • 304775 105_03.jpg
  • 304775 105_05.jpg
  • 304775 105_07.jpg
  • 304775 105_02.jpg
  • 304775 105_04.jpg
  • 304775 105_06.jpg
  • 304775 105_01.jpg
  • 304775 105_03.jpg
  • 304775 105_05.jpg
  • 304775 105_07.jpg
  • 304775 105_02.jpg
  • 304775 105_04.jpg
  • 304775 105_06.jpg

Basically, I need to remove the SPACES. I already know the command to change the spaces into underscores:

基本上,我需要删除空格。我已经知道将空格更改为下划线的命令:

$ rename "s/ /_/g" *

But I do not need the underscores in this case. I just need to remove the space. I tried the following, but it didn't work:

但在这种情况下我不需要下划线。我只需要删除空间。我尝试了以下方法,但没有奏效:

$ rename "s/ //g" *

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by Blake

You could do something like this:

你可以这样做:

IFS="\n"
for file in *.jpg;
do
    mv "$file" "${file//[[:space:]]}"
done

回答by Anirudh Ramanathan

The following would work in case it was really a space.

如果它真的是一个空间,以下内容将起作用。

$ rename "s/ //g" *

Try

尝试

$ rename "s/\s+//g" *

\sis a whitespace character, belonging to the set of [ \t\r\n].

\s是一个空白字符,属于[ \t\r\n].