Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove whitespaces from filenames in Linux [closed]

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.

like image 633
Sam Timalsina Avatar asked Mar 11 '13 20:03

Sam Timalsina


People also ask

How do I remove whitespace from text file in Linux?

s/[[:space:]]//g; – as before, the s command removes all whitespace from the text in the current pattern space.

How do I remove leading spaces in Linux?

Use sed 's/^ *//g', to remove the leading white spaces. There is another way to remove whitespaces using `sed` command. The following commands removed the spaces from the variable, $Var by using `sed` command and [[:space:]].


2 Answers

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

like image 122
Anirudh Ramanathan Avatar answered Oct 19 '22 23:10

Anirudh Ramanathan


You could do something like this:

IFS="\n" for file in *.jpg; do     mv "$file" "${file//[[:space:]]}" done 
like image 28
Blake Avatar answered Oct 20 '22 00:10

Blake