Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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
doneFor 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! :)

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

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
doneOriginal files:
kill 1.sql
kill 2.sqlRenamed to:
kill_1.sql
kill_2.sql
Luke Chi

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

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 fileIt's working now, thanx again.

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"

![]() |
![]() |
![]() |

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