Bash: rename files with 2 spaces

Score
0
Vote Up
September 20, 2005 at 08:21:32 Pacific
Specs: Slackware vcurrent, Never enough...!=o

Sorry, first posted on the linux forum....then I thought I may get better scripting help here.

I have a couple of folders containing spam and ham on which I cron sa-learn (from spamassassin) for each of my users. This works fine and dandy, users dump emails into these directories and the database learns from them. The trouble arrises when a filename (the email subject) contains double spaces.

No probs I thought, a simple script can be run before sa-learn to rename any files containing double spaces. I normally do things in perl but (don't ask me why!) I want this in bash. So far I have come up with:

#!/bin/bash
for file in $( ls -Q *\ \ * );
do
target=$(echo $file | sed -e "s/\ \ /\ /")
mv $file $target
done

For some reason this does not work as bash still seems to split the filenames at the double space. I have also tried the --quoting-style=shell and --quoting-style=shell-always ls switches to no avail, just seems to use single instead of the double quotes you get with -Q.

HELP! :)


Reply ↓  Report •


#1
Vote Down
Score
0
Vote Up
September 20, 2005 at 09:22:14 Pacific

I'm interpreting that you want to find all the files with two spaces in the name and rename them to one?

There's no need to escape the spaces, and you need to make sure ls returns only one file name at a time:

#!/bin/bash

ls -1 *" "*|while read file
do
target=$(echo $file | sed -e "s/ / /")
mv "$file" "$target"
done


Reply ↓  Report •

#2
Vote Down
Score
0
Vote Up
September 20, 2005 at 09:38:30 Pacific

You need to create a tmp.txt file.

#!/bin/bash
ls *" "* > tmp.txt
cat tmp.txt | while read FILE
do
target=$(echo "$FILE" | sed -e "s/ /_/")
mv "$FILE" $target
done

Original files:

kill 1.sql
kill 2.sql

Renamed to:

kill_1.sql
kill_2.sql


Luke Chi


Reply ↓  Report •

#3
Vote Down
Score
1
Vote Up
September 20, 2005 at 09:41:50 Pacific

I was wrong. You don't need tmp.txt.

ls -1 *" "*|while read file in nails is better than creating a tmp.txt file.

Luke Chi


Reply ↓  Report •

#4
Vote Down
Score
0
Vote Up
September 21, 2005 at 01:35:09 Pacific

Thank you both for your replies. Looks like all I had to do was change the loop.

for file in $( ls -Q *\ \ * );
changed to
ls -Q *\ \ * | while read file

It's working now, thanx again.


Reply ↓  Report •

Related Posts

#5
Vote Down
Score
0
Vote Up
September 22, 2005 at 17:52:10 Pacific

picky, picky
the -Q is not required


Reply ↓  Report •

#6
Vote Down
Score
0
Vote Up
September 23, 2005 at 04:09:47 Pacific

Hi Dlonra. Didn't expect to see you on this forum, wolfbone is probably around as well!

True....in fact the -1 switch is not required either. Just got to make sure you enclose $file and $target with quotes in the mv command, ie:
mv "$file" "$target"


Reply ↓  Report •

Reply to Message Icon Start New Discussion
« How to insert string in a... Idendifying duplicate rec... »

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.

Ask the Community!
Describe your Problem
Example: Hard Drive Not Detected on My PC