Sponsored Content
Top Forums Shell Programming and Scripting Spaces in filenames screwing things up... Post 302680669 by Binary Buddha on Thursday 2nd of August 2012 07:01:11 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
PRINTF(1)							   User Commands							 PRINTF(1)

NAME
printf - format and print data SYNOPSIS
printf FORMAT [ARGUMENT]... printf OPTION DESCRIPTION
Print ARGUMENT(s) according to FORMAT, or execute according to OPTION: --help display this help and exit --version output version information and exit FORMAT controls the output as in C printf. Interpreted sequences are: " double quote \ backslash a alert (BEL)  backspace c produce no further output e escape f form feed new line carriage return horizontal tab v vertical tab NNN byte with octal value NNN (1 to 3 digits) xHH byte with hexadecimal value HH (1 to 2 digits) uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits) UHHHHHHHH Unicode character with hex value HHHHHHHH (8 digits) %% a single % %b ARGUMENT as a string with '' escapes interpreted, except that octal escapes are of the form or NNN %q ARGUMENT is printed in a format that can be reused as shell input, escaping non-printable characters with the proposed POSIX $'' syntax. and all C format specifications ending with one of diouxXfeEgGcs, with ARGUMENTs converted to proper type first. Variable widths are han- dled. NOTE: your shell may have its own version of printf, which usually supersedes the version described here. Please refer to your shell's documentation for details about the options it supports. AUTHOR
Written by David MacKenzie. REPORTING BUGS
GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report printf translation bugs to <http://translationproject.org/team/> COPYRIGHT
Copyright (C) 2017 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO
printf(3) Full documentation at: <http://www.gnu.org/software/coreutils/printf> or available locally via: info '(coreutils) printf invocation' GNU coreutils 8.28 January 2018 PRINTF(1)
All times are GMT -4. The time now is 01:34 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy