LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2010, 06:28 AM   #1
Hypertenzion
LQ Newbie
 
Registered: Oct 2010
Location: Copenhagen, Denmark
Distribution: Debian
Posts: 21

Rep: Reputation: 0
Replace whitespaces with dots "." on all files in folder


I have a lot of .avi,.mkv,.srt files with empty spaces that needs to be filled out with a dot "."

Example:

With space = "my movie file here.avi"
Replaced with dots = "my.movie.file.here.avi"

I need help with a script that does that

Help much appreciated!
 
Old 11-09-2010, 06:39 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Code:
for i in *; do mv "$i" $(echo "$i" | tr " " "."); done
Also, I modified this from a script I wrote to fix file names:

Code:
for i in *
do
  mv "$i" $(echo "$i" | tr [:upper:] [:lower:] | tr [:blank:] _ | sed 's|[^-._a-z0-9]||g')
done
this one makes names lower case, replaces all white space with _, and removes all non-printable or strange characters.

Last edited by H_TeXMeX_H; 11-09-2010 at 06:41 AM.
 
Old 11-09-2010, 06:41 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
What have you tried so far? The rename command should do the trick. Anyway, there are two versions of the command out there. On my current system (CentOS) it acts only on the first occurrence of the pattern.

In alternative you can try shell's parameter substitution, replacing every occurrence of a space with a dot, e.g.
Code:
for file in *" "*
do
  echo mv $file ${file// /.}
done
where the echo statement is only for testing purposes. This does not replace a sequence of two or more spaces (if any) with a single dot, anyway.
 
Old 11-09-2010, 06:42 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Actually, I tried rename, but surprisingly it only replaces the first space with a dot.
 
Old 11-09-2010, 06:42 AM   #5
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
Something like:
Code:
find YOUR_DIR | while read FILE; do
   OFILE=`echo $FILE |*sed "s/ /./g"`
   mv $FILE $OFILE
done
will do the job
 
Old 11-09-2010, 06:45 AM   #6
Hypertenzion
LQ Newbie
 
Registered: Oct 2010
Location: Copenhagen, Denmark
Distribution: Debian
Posts: 21

Original Poster
Rep: Reputation: 0
Thx for the quick replies, people!

I fixed it with the following:

Code:
find -type f -exec rename 'y/\ /\./' {}  \;
 
Old 11-09-2010, 07:09 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by H_TeXMeX_H View Post
Actually, I tried rename, but surprisingly it only replaces the first space with a dot.
I was surprised too, but accordingly to the man page it is the expected behaviour:
Code:
SYNOPSIS
       rename from to file...

DESCRIPTION
       rename will rename the specified files by replacing
       the first occurrence of from in their name by to.
However this applies only on rename as an ELF executable provided by the util-linux package. Instead, on Debian-based systems the rename command is a perl script that accepts a perl expression in place of the two patterns, so that we can use something like:
Code:
rename 's/ +/./g' *" "*
where the g modifier does the trick. Looking at the last post from the OP, it seems the perl version is in action here.
 
Old 11-11-2010, 12:39 PM   #8
Hypertenzion
LQ Newbie
 
Registered: Oct 2010
Location: Copenhagen, Denmark
Distribution: Debian
Posts: 21

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
I was surprised too, but accordingly to the man page it is the expected behaviour:
Code:
SYNOPSIS
       rename from to file...

DESCRIPTION
       rename will rename the specified files by replacing
       the first occurrence of from in their name by to.
However this applies only on rename as an ELF executable provided by the util-linux package. Instead, on Debian-based systems the rename command is a perl script that accepts a perl expression in place of the two patterns, so that we can use something like:
Code:
rename 's/ +/./g' *" "*
where the g modifier does the trick. Looking at the last post from the OP, it seems the perl version is in action here.
Thx!

The code:

Code:
rename 's/ +/./g' *" "*
Works on both files and folders!

The code:

Code:
find -type f -exec rename 'y/\ /\./' {}  \;
Only works on files.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] need to copy all " *.SEED " files and paste them inside a new folder each time rastin_nz Programming 1 11-03-2010 05:02 PM
Replace ":" from multiple files names (even recursively in directories) pepeq Linux - General 3 02-01-2010 11:09 AM
Comparison to "Program Files" Folder in Linux? Ascendancy5 Linux - Newbie 11 01-15-2009 06:45 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
How to "Search & Replace" in html files using Perl? rebel Red Hat 8 04-09-2005 12:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration