27

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

  • 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.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Sam Timalsina
  • 457
  • 1
  • 4
  • 9

2 Answers2

42

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

$ rename "s/ //g" *

Try

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

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

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
25

You could do something like this:

IFS="\n"
for file in *.jpg;
do
    mv "$file" "${file//[[:space:]]}"
done
Blake
  • 2,047
  • 2
  • 12
  • 11