LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
Search this Thread
Old 10-04-2007, 02:41 PM   #1
sir_woland
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Rep: Reputation: 0
find command and file names with whitespace


Hi everyone,

I'm looking for a way to iterate through a list of files that have blank spaces in their names. I haven't had much luck with the find command so far:

Code:
for i in `find . -iname "*.mp3"`
do
  echo $i
done
since it splits the filenames by whitespaces. I found a successful workaround which involves renaming the files by replacing spaces with underscores, then reverting the operation, but this is far from elegant. Could someone provide a better solution?
 
Old 10-04-2007, 03:27 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
Try putting a pair of quotes around the whole find command, and another around the output variable, to contain the whole expression:

Code:
for i in "`find . -iname "*.mp3"`";
do echo "$i";
done
 
Old 10-04-2007, 03:30 PM   #3
Arwkin
LQ Newbie
 
Registered: Oct 2007
Distribution: MEPIS
Posts: 15

Rep: Reputation: 0
Using your example of echo, this will do what you want.

Code:
find . -iname "*.mp3" -exec echo \"{}\" \;
//edit - You probably want to use David's solution. With my solution, I had something like this in mind:

Code:
find . -iname "*.mp3" -exec mp3toogg -c \"{}\" output_dir \;

Last edited by Arwkin; 10-04-2007 at 04:01 PM. Reason: After-thought
 
Old 10-04-2007, 04:22 PM   #4
sir_woland
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you both -- unfortunately neither solution was quite what I'm looking for.

Trying the quoting trick gives me one long string with all the file names separated with line breaks, which echoes to the console quite nicely but still does not iterate correctly -- it goes through the for loop exactly once, regardless of the amount of files found. For X matches in the find command I need exactly X iterations of the for loop.

What I'm looking for is a way to iterate through all mp3 files in a directory, then perform some tagging on each one individually. This gets quite complex as I have to formulate the tag data from the file name itself, then call id3v2 with the appropriate switches and tag strings. So far haven't managed to get find working the way I want it to, and -exec {} hasn't really helped me either yet.

Last edited by sir_woland; 10-04-2007 at 04:23 PM. Reason: typo
 
Old 10-04-2007, 05:00 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by sir_woland View Post
So far haven't managed to get find working the way I want it to, and -exec {} hasn't really helped me either yet.
Whatís the problem with exec? Maybe I donít correctly understand you, but does this not do what you want?:
Code:
find . -iname "*.mp3" -exec do_tagging \"{}\" \;
 
Old 10-04-2007, 05:04 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,355

Rep: Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042
Change the default field separator to newline - then your original should work.
Pays to save and restore the IFS value, but that's just personal preference.
 
Old 10-04-2007, 05:26 PM   #7
sir_woland
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Yes! Changing the IFS was precisely the correct thing to do, and running the script in a subshell lets me use the new IFS value only for the duration of the script. Works perfectly, thank you!

The reason -exec didn't fly is that the tagging command I need to run for each and every file is of the form:

Code:
id3v2 -T tracknum -t "trackname" file_to_process
where tracknum and trackname are strings cut from the file name and piped through tr and sed. Too much for my inept skills!
 
Old 10-04-2007, 05:33 PM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
I will do it like this:
You can use the -print0 option to use a null instead of a carriage return. The list can be fed to xargs with the -0 option.

If the source or filenames is another command you can pipe the output through "| tr '\n' '\000' |"
example:
find . -iname "*.mp3" -print0 | xargs -0 mpg2ogg

When I use K3B to make a backup of a directory, I'll save the .k3b file and unzip it and use sed to obtain the filenames backed up. Then I can pipe the output through "| tr '\n' '\000' | xargs -0 rm -v" to remove the files backed up.
 
Old 10-04-2007, 07:10 PM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Iím glad you found a solution that suits you.
Quote:
Originally Posted by sir_woland View Post
The reason -exec didn't fly is that the tagging command I need to run for each and every file is of the form:
Code:
id3v2 -T tracknum -t "trackname" file_to_process
where tracknum and trackname are strings cut from the file name and piped through tr and sed. Too much for my inept skills!
If you wanted to do it cleanly, this would work:
Code:
find . -iname "*.mp3" -exec ./do_tagging \"{}\" \;
Where the file do_tagging is something like this:
Code:
#!/bin/sh

tracknum=$(echo "$1" | tr '1' '2' | sed 's/foo/bar')
trackname=$(echo "$1" | tr '3' '4' | sed 's/foo/bar')

id3v2 -T $tracknum -t "$trackname" $1
I guess that is a little too roundabout.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making find do search in a directory that has whitespace in its pathname xode Linux - Software 2 04-18-2006 10:42 AM
Command to find similar file names in a directory Ottoguy Linux - Newbie 4 02-02-2006 05:42 AM
copying files and give new unique names to each file by using xargs command gnim66 Programming 6 06-22-2005 08:29 PM
Find and special file names CritterZ Linux - Newbie 2 12-23-2004 09:49 PM
remove whitespace at end of file FunkyRes Programming 2 10-05-2004 12:31 AM


All times are GMT -5. The time now is 05:05 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