Spaces in filenames screwing things up...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Spaces in filenames screwing things up...
# 1  
Old 08-02-2012
Spaces in filenames screwing things up...

Here's the code... obviously

Code:
#!/bin/bash

SRC=$1  

function main {

        validate 
        run

}

function validate {
        if [[ -z $SRC ]]; then
                printf "You need to supply a source.\n";
                exit 1;
        elif [[ -f $SRC ]]; then
                printf "This is a file! You need to specify a directory.\n";
                exit 1;
        fi
}

function run {

        FILES=`find $SRC -type f |sed 's_ _\ _g' |  head`

        for FILE in $FILES
        do
                printf $FILE"\n";
        done
}

main

Here's the output

Code:
/media/Data_Bucket/Audio/(val)Liam/Vampire_Sunrise_(Disc_1)/04_-_As_Is.mp3_CBR.mp3
/media/Data_Bucket/Audio/)eib(/BC
Recordings
(BCRUK001)/Dogfight.mp3
/media/Data_Bucket/Audio/)eib(/Digital
Nation/08
Infinity.MP3_CBR.mp3
/media/Data_Bucket/Audio/)eib(/mix
1/05
Planet
Dust.mp3
/media/Data_Bucket/Audio/)eib(/Unknown
Album/Bounce.mp3
/media/Data_Bucket/Audio/)eib(/Unknown
Album/Dustball
(Dubplate).mp3
/media/Data_Bucket/Audio/)eib(/Unknown
Album/eib
./sync-indus.bash: line 28: printf: -\: invalid option
printf: usage: printf [-v var] format [arguments]
running
man.mp3
/media/Data_Bucket/Audio/)eib(/Unknown
Album/eib
./sync-indus.bash: line 28: printf: -\: invalid option
printf: usage: printf [-v var] format [arguments]
Speedball.mp3
/media/Data_Bucket/Audio/)eib(/Unknown
Album/eib
./sync-indus.bash: line 28: printf: -\: invalid option
printf: usage: printf [-v var] format [arguments]
start
the
fire.mp3
/media/Data_Bucket/Audio/)eib(/Unknown
Album/Orient
Express.mp3

As you can see, the file name break up in to separate lines due to spaces and special characters. I've tried to use sed on line 24 to replace the spaces with the escape characters. But that didn't work.

Kinda clueless at this point.
# 2  
Old 08-02-2012
Try:
Code:
function run {
        find $SRC -type f |sed 's/ /_/g'
}

# 3  
Old 08-02-2012
See Useless Use of Backticks. Never ever use variables/backticks for open-ended lists. It's generally pointless, likely to truncate your data, and splits where it pleases instead of where you expect.

Since you're putting it in a loop anyway, you might as well save yourself the trouble and make it more direct.

The usual way to do this is

Code:
find ... | while read LINE
do
...
done

If you need to use the same results more than once, you could save find's output into a temporary file and read from that.
Code:
while read LINE
do
...
done < /tmp/filename

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 08-02-2012
Quote:
Originally Posted by zaxxon
Try:
Code:
function run {
        find $SRC -type f |sed 's/ /_/g'
}

That wouldn't work for me. I'm planning on processing the files in the script. So, I need to keep the name as the original.

---------- Post updated at 03:31 PM ---------- Previous update was at 03:14 PM ----------

Quote:
Originally Posted by Corona688
Code:
find ... | while read LINE
do
...
done

So, would LINE be the "per entry" variable that I process? Like an i in:

Code:
for i in $LIST

I was never good at loops.

Quote:
Originally Posted by Corona688
Code:
while read LINE
do
...
done < /tmp/filename

Would this be more "memory efficient"? Since it's in a file that would be one less thing for it stuff into memory? The targets can generate a very large list and the "server" doesn't really have that much ram; 256MB.
# 5  
Old 08-02-2012
Quote:
Originally Posted by Binary Buddha
That wouldn't work for me. I'm planning on processing the files in the script. So, I need to keep the name as the original.

---------- Post updated at 03:31 PM ---------- Previous update was at 03:14 PM ----------



So, would LINE be the "per entry" variable that I process? Like an i in
Yes, exactly, except you don't need to have the entire $LIST in a variable like that (a bad idea for the reasons given above).
Quote:
Would this be more "memory efficient"? Since it's in a file that would be one less thing for it stuff into memory? The targets can generate a very large list and the "server" doesn't really have that much ram; 256MB.
It'd be far more efficient than cramming it into a variable. Depending on your shell, it's quite questionable whether you can cram 256 megs of text into a variable and expect it to even work no matter how much RAM you have.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 08-02-2012
Quote:
Originally Posted by Corona688
It'd be far more efficient than cramming it into a variable. Depending on your shell, it's quite questionable whether you can cram 256 megs of text into a variable and expect it to even work no matter how much RAM you have.
I'm one of those "BASH Punks". 256MB is how much memory I have in the "server". It's an old POS that I use to process things on instead of my workstation.

Yeah, I guess I'll go this route with my script then...
# 7  
Old 08-02-2012
You can do a lot with 256 megs. Smilie

Either use mktemp to generate a name for the temporary file, or use files like /tmp/$$-appname. This will allow several instances of the script to run without stomping over each other's temp files.

Last edited by Corona688; 08-02-2012 at 06:45 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Reading filenames with spaces

Hello I've got a certain no. of files in a directory whose names I'm reading and redirecting into a temporary text file using the command below: ls -l | grep ^- | awk '{print $9}'However, whenever the file names contain spaces the above command considers only the part of the file name up to... (5 Replies)
Discussion started by: S. BASU
5 Replies

2. Shell Programming and Scripting

Remove spaces in filenames

Hi, I have files like below, In files coming as spaces. Before transfering those files into ftp server. I want to remove the spaces and then can transfer the files into unix server. e.g: filenames are 1) SHmail _profile001_20120908.txt 2) SHmail_profile001 _20120908.txt 3) sh... (3 Replies)
Discussion started by: kirankumar
3 Replies

3. Shell Programming and Scripting

removing spaces in filenames

I have a problem mounting images because of the spaces in the filenames. Does anyone know how to rename files by removing the spaces with the find command? find Desktop/$dir -name "*.dmg" -print -exec ??? (4 Replies)
Discussion started by: ianebaj
4 Replies

4. Shell Programming and Scripting

awk and spaces in filenames

Hey there, this is my first post and I'll try to explain my situation as best I can.Here is a sample of the input file: ADO Sample.h,v ADO Sample 2010-05-21 lyonsb /repository/patents/TSCommon/OpenSource/Dundass/ug6mfc/DataSources/Ado/ADO Sample ADO SampleDoc.h,v ADO SampleDoc 2010-05-21... (3 Replies)
Discussion started by: rodan90
3 Replies

5. Shell Programming and Scripting

Looping through filenames with spaces

I need to loop through the files in a directory and process the files. But some of the filenames contain spaces. Here is a little test script I've been using to experiment. (I'm not really going to call 'echo', I'm doing some other processing.) Everything I try fails. How can I do this??... (7 Replies)
Discussion started by: KenJackson
7 Replies

6. Shell Programming and Scripting

spaces in filenames

Hi I hope someone will be able to resolve this little teaser! I am running a script for file in `ls directory` do echo "$file" ...other code here.... done this works fine unless we receive a file with a name which has a space in it ie "filena me" (I know its not good... (8 Replies)
Discussion started by: Bab00shka
8 Replies

7. Shell Programming and Scripting

spaces in filenames, for do

Hi All, I see similar problems in past threads but so far no answers have worked for me. I am trying to write a script which parses a txt file that contains one filename per line, then finds those files on the local disk and copies them to a specified directory. What I have: ... (4 Replies)
Discussion started by: naviztirf
4 Replies

8. Shell Programming and Scripting

Unix filenames and spaces

I have files on my unix boxes that users have created with spaces. Example: /tmp/project plan ls -l "/tmp/project plan" works fine. $/tmp>ls -l "/tmp/project plan" -rw-r--r-- 1 root other 0 Jan 31 12:32 /tmp/project plan I created a file called test and put just the... (2 Replies)
Discussion started by: x96riley3
2 Replies

9. Shell Programming and Scripting

how to handle spaces in filenames

I'm trying to do something like that: for $filename in `ls -1` do some_command $filename done but it doesn't work properly for file names with spaces, for...in splits at spaces. Anyway around? (4 Replies)
Discussion started by: rayne
4 Replies

10. Shell Programming and Scripting

spaces in filenames

I have a problem with the script below #!/bin/sh for vo in `find -maxdepth 1 -type f -regex "^\./*$"` do ls -l "$vo" some other commands done It works fine until `find ...` returns files with spaces. I've tryed to change IFS but haven't succeed Any solutions? (4 Replies)
Discussion started by: Hitori
4 Replies
Login or Register to Ask a Question