Remove whitespaces from filenames in Linux

Sam Timalsina picture Sam Timalsina · Mar 11, 2013 · Viewed 52.7k times · Source

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.

Answer

Anirudh Ramanathan picture Anirudh Ramanathan · Mar 11, 2013

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