Bash script for files with spaces in the filenames

Jeff Vian jvian10 at charter.net
Thu Feb 16 13:22:05 UTC 2006


On Thu, 2006-02-16 at 11:29 +0200, Dotan Cohen wrote:
> Another friend has provided me with a simple bash script that performs
> an operation on all the files of a directory. However, many of the
> file names contain spaces. How can I modify the following code to work
> on files with spaces? The directory is on a mounted FAT32 partition.
> 
> 
> #!/bin/bash
> PWD=`pwd`
> for file in `find $PWD -name "*.mp3"`
> do
>   eyeD3 --force-update --set-encoding=utf8 "$file"
> done
> 
In the above construct $file gets the first part up to the delimiter
(white space) for each part it reads, so it may not have the full file
name.

I had a similar problem so I built something like this:

name=''
for i in *.mp3
do
   if ["X$name" = "X" ] then
       name=$i
   else
       name="$name $i"
   fi
#  the part above builds the file name from pieces.
   if [ `grep mp3 "$name"` ] then
       <execute command on file>
      name=''
   fi
#  this part executes the command and resets $name 
#  to begin building the next file name.
done


I am not certain of the exact test I used to make sure my file name was
complete with the .mp3 ending but I think it was the grep above.



> 
> I had considered converting the spaces to underscores, but that would
> upset the delicate wife/linux balance in our household.
> 
> Thank you.
> 
> Dotan Cohen
> http://technology-sleuth.com/technical_answer/what_is_a_firewall.html
> 




More information about the fedora-list mailing list