Spaces in filenames screwing things up...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Spaces in filenames screwing things up...
# 8  
Old 08-02-2012
Quote:
Originally Posted by Corona688
You can do a lot with 256 megs. Smilie
Well, I got other stuff going on too. It's also my media server, NFS server, Bittorrent seedbox, and a backup server to copy stuff off of my Dreamhost servers. "That's CRON. He fights for the Sys Ops."
# 9  
Old 08-02-2012
It can do all that plus some reasonably hefty database stuff. Just don't ask it to do Wordpress.
# 10  
Old 08-02-2012
Let's try putting double quotes round string variables and avoiding for because it cannot deal with lists containing space characters. Also remove many extraneous semi-colon characters and ensure that we run Test [ ] not a Conditional Expression [[ ]]. Positive test for Directory rather than assuming (wrongly) that everything which is not a file is a directory.
Not sure what the sed is for, so I left it out.

Code:
#!/bin/bash

SRC="${1}"   # Start Directory  

function main {

        validate 
        run

}

function validate {
        # Is parameter missing?
        if [ -z "${SRC}" ]
        then
                printf "You need to supply a source.\n"
                exit 1
        fi
        # Is parameter a directory?
        if [ ! -d "${SRC}" ]
        then
                printf "This is not a directory! You need to specify a directory.\n"
                exit 1
        fi
}

function run {

        find "${SRC}" -type f | while read filename
        do
                printf "${filename}\n"
        done
}

main


Ps. I must have missed the bit in post #1 which says what the script is meant to do!

Last edited by methyl; 08-02-2012 at 09:42 PM.. Reason: many and various typos
# 11  
Old 08-02-2012
Also, change printf to avoid issues with filenames starting with dash:

Code:
       printf "%s\n" "${filename}"

This User Gave Thanks to Chubler_XL For This Post:
# 12  
Old 08-02-2012
Quote:
Originally Posted by methyl
Ps. I must have missed the bit in post #1 which says what the script is meant to do!
Well, eventually the script is going to go through music library; $SRC, and analyze each file; which are all MP3. All the files that contain ID3V2 tags with a specifc genre will be copied into another directory in preparation for a rsync over a remote server. The new directory structure is going to be created according to the tags; Library => Artist => Album => Track. (I'll probably end up putting up another post on proper ASCII safe filename conversion later. But, that's off-topic.)

---------- Post updated at 09:33 PM ---------- Previous update was at 09:32 PM ----------

Quote:
Originally Posted by Chubler_XL
Also, change printf to avoid issues with filenames starting with dash:

Code:
       printf "%s\n" "${filename}"


None of my filenames start with a dash; I know that for certian. If anything, the dash will likely be in the somewhere in the middle of the filename.
# 13  
Old 08-02-2012
Quote:
Originally Posted by Binary Buddha
None of my filenames start with a dash; I know that for certian. If anything, the dash will likely be in the somewhere in the middle of the filename.
You may as well use printf correctly anyway. Aside from possible issues with a leading dash with GNU coreutils and bash printf implementations, there would be problems if anything in the filename looks like a format specifier or escape sequence.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 14  
Old 08-03-2012
Forgot to mention that you should use double quotes when calling the script if your parameter contains space characters:

Code:
./scriptnname "directory name"

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