LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 11-06-2004, 03:57 PM   #1
scattaneo
LQ Newbie
 
Registered: Nov 2004
Posts: 3

Rep: Reputation: 0
problem creating BASH script with files that have whitespace


hi I'm attempting to write a bash script to evaluate a large number of files that have whitespace in their name. I believe my problem lies in how i read the files into the array. not tottaly sure though.
agood example of the current file naming is:
artist - songname.mp3

heres my code:
------BEGIN-----
#!/bin/bash
for OLDNAME in `find . -name "* *mp3"`
do
NEWNAME=`echo "$OLDNAME" | tr -s " " "_"`
mv "$OLDNAME" $NEWNAME
done
------END-----

any help would be appreciated.

steve

Last edited by scattaneo; 11-06-2004 at 03:58 PM.
 
Old 11-06-2004, 04:18 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 53
tr uses [:blank:] for horizontal spaces ( man tr ) and I just happen to have a diddy for such a rename project.
Make it executable and run like this: ./test /home/myimages

Note: the mv statement is protected by " echo " so you can test it with no harm done. Just remove the echo when ready to roll.

Note: this site changed my [[ : alnum : ]] , I haven't tested it with just one [ ]

Also, you can use sed to make the replacement:
j=`echo -n "$i" | sed -e 's/ /_/'`

Code:
#!/bin/sh
#
usage()
{
        echo "Usage: $0 [directory]"
        exit 1;
}

test -d "$1" || usage

dir="$1"

ls $dir | grep -e "[:alnum:]" | \
while read i; do
j=`echo -n "$i" | tr "[:blank:]" "_"`
echo mv "$dir/$i" "$dir/$j"
done

Last edited by homey; 11-06-2004 at 04:39 PM.
 
Old 11-06-2004, 05:40 PM   #3
scattaneo
LQ Newbie
 
Registered: Nov 2004
Posts: 3

Original Poster
Rep: Reputation: 0
worked beautifully thanks a lot

steve
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash backup script - If multiple files starting with a exist problem demoncheese Programming 2 07-29-2004 10:47 PM
Creating switches in a bash script Moto_Jon Programming 2 07-28-2004 06:06 PM
Using sed in bash to remove whitespace jimieee Programming 3 01-28-2004 10:33 AM
Help creating a Bash script gothicreader Linux - Newbie 4 10-10-2003 01:36 PM
need help creating a bash script zibertron Linux - General 3 01-31-2003 09:23 AM


All times are GMT -5. The time now is 11:41 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration