Computing.Net > Forums > Unix > Bash: rename files with 2 spaces

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Bash: rename files with 2 spaces

Reply to Message Icon

Name: 3Dave
Date: September 20, 2005 at 08:21:32 Pacific
OS: Slackware vcurrent
CPU/Ram: Never enough...!=o
Comment:

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! :)



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: September 20, 2005 at 09:22:14 Pacific
Reply:

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


0

Response Number 2
Name: Luke Chi
Date: September 20, 2005 at 09:38:30 Pacific
Reply:

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


0

Response Number 3
Name: Luke Chi
Date: September 20, 2005 at 09:41:50 Pacific
Reply:

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


0

Response Number 4
Name: 3Dave
Date: September 21, 2005 at 01:35:09 Pacific
Reply:

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.


0

Response Number 5
Name: Dlonra
Date: September 22, 2005 at 17:52:10 Pacific
Reply:

picky, picky
the -Q is not required


0

Related Posts

See More



Response Number 6
Name: 3Dave
Date: September 23, 2005 at 04:09:47 Pacific
Reply:

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"


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Bash: rename files with 2 spaces

Renaming files with awk www.computing.net/answers/unix/renaming-files-with-awk/8036.html

How to remove spaces in filenames www.computing.net/answers/unix/how-to-remove-spaces-in-filenames/8204.html

Rename files older than 2 days www.computing.net/answers/unix/rename-files-older-than-2-days/4185.html