LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   spaces in filenames (http://www.linuxquestions.org/questions/programming-9/spaces-in-filenames-415980/)

dotancohen 02-16-2006 01:39 AM

spaces in filenames
 
Another LinuxQuestions user 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.

Code:

#!/bin/bash
PWD=`pwd`
for file in `find $PWD -name "*.mp3"`
do
  eyeD3 --force-update --set-encoding=utf8  $file
done

I had considered converting the spaces to underscores, but that would upset the delicate wife/linux balance in our household.

Thank you.

ilikejam 02-16-2006 02:41 AM

Hi.

Replace $file with "$file"

Dave

dotancohen 02-16-2006 02:44 AM

Thank you!

spooon 02-16-2006 02:51 AM

Quote:

Originally Posted by dotancohen
Code:

#!/bin/bash
PWD=`pwd`
for file in `find $PWD -name "*.mp3"`
do
  eyeD3 --force-update --set-encoding=utf8  "$file"
done


You could also do this with something like
Code:

find . -name "*.mp3" -exec eyeD3 --force-update --set-encoding=utf8 '{}' \;

dotancohen 02-16-2006 03:07 AM

Er, didn't quite work. It still returns "file not found" for each file with spaces in the name.

In your second suggestion, is this the script that you suggest?:
Code:

#!/bin/bash
find . -name "*.mp3" -exec eyeD3 --force-update --set-encoding=utf8 '{}' \;
done

I just want to be sure before I ruin something.

spooon 02-16-2006 03:54 AM

Yes, or just run it on the command line.

Hmm, perhaps the quoting the filename thing will work here:
Code:

find . -name "*.mp3" -exec eyeD3 --force-update --set-encoding=utf8 "'{}'" \;

bigearsbilly 02-16-2006 04:30 AM

You shouldn't need all those quotes for find -exec, just the {}
should do it. It works OK for me with spaces.

FYI
PWD=`pwd`??? this is pointless. PWD is already set.

dotancohen 02-16-2006 05:52 AM

Thanks. As bigearsbilly had mentioned, the quotation marks around {} were redundant. It now works!
1) there was plenty of disk space
2) permissions were never a problem

Thank you.

bigearsbilly 02-16-2006 05:54 AM

1) 2)
no that's just my signature ;)

cigarstub 02-16-2006 07:41 AM

please post the result of this:

#!/bin/bash
for i in 'find --name "*.txt"'
do
echo $i
done
Just to text wat's the output of $i in ur box.

kshkid 02-16-2006 07:54 AM

Code:

find --name "*.txt"
this would result in error in any shell

bigearsbilly 02-16-2006 07:59 AM

if no subdirectories

Code:

for f in *.txt ;do
  echo $f
done


dotancohen 02-17-2006 02:42 PM

In any case, I got it working with the code that spooon had suggested.


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