LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-16-2006, 01:39 AM   #1
dotancohen
Member
 
Registered: Dec 2004
Location: Haifa
Distribution: Fedora Core 4, Kubuntu
Posts: 235

Rep: Reputation: 32
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.
 
Old 02-16-2006, 02:41 AM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

Replace $file with "$file"

Dave
 
Old 02-16-2006, 02:44 AM   #3
dotancohen
Member
 
Registered: Dec 2004
Location: Haifa
Distribution: Fedora Core 4, Kubuntu
Posts: 235

Original Poster
Rep: Reputation: 32
Thank you!
 
Old 02-16-2006, 02:51 AM   #4
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
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 '{}' \;
 
Old 02-16-2006, 03:07 AM   #5
dotancohen
Member
 
Registered: Dec 2004
Location: Haifa
Distribution: Fedora Core 4, Kubuntu
Posts: 235

Original Poster
Rep: Reputation: 32
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.
 
Old 02-16-2006, 03:54 AM   #6
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
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 "'{}'" \;
 
Old 02-16-2006, 04:30 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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.
 
Old 02-16-2006, 05:52 AM   #8
dotancohen
Member
 
Registered: Dec 2004
Location: Haifa
Distribution: Fedora Core 4, Kubuntu
Posts: 235

Original Poster
Rep: Reputation: 32
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.

Last edited by dotancohen; 02-16-2006 at 05:56 AM.
 
Old 02-16-2006, 05:54 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
1) 2)
no that's just my signature
 
Old 02-16-2006, 07:41 AM   #10
cigarstub
Member
 
Registered: Sep 2005
Posts: 145

Rep: Reputation: 15
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.
 
Old 02-16-2006, 07:54 AM   #11
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Code:
find --name "*.txt"
this would result in error in any shell
 
Old 02-16-2006, 07:59 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
if no subdirectories

Code:
for f in *.txt ;do
  echo $f
done
 
Old 02-17-2006, 02:42 PM   #13
dotancohen
Member
 
Registered: Dec 2004
Location: Haifa
Distribution: Fedora Core 4, Kubuntu
Posts: 235

Original Poster
Rep: Reputation: 32
In any case, I got it working with the code that spooon had suggested.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
filenames with spaces antony.booth Programming 5 11-01-2005 04:49 AM
ls and filenames with spaces rose_bud4201 Programming 10 07-01-2005 08:28 AM
Spaces in filenames with mpg123 Boffy Linux - Software 3 04-26-2004 02:51 PM
filenames with spaces - explain t3___ Linux - Newbie 14 02-17-2004 06:13 PM
spaces in filenames ebone Linux - General 2 11-12-2001 11:56 AM


All times are GMT -5. The time now is 05:59 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 Google+: linuxquestions
Open Source Consulting | Domain Registration