Find command fails when a space is in the directory path variable

Thread Tools Search this Thread
# 1  
Question Find command fails when a space is in the directory path variable

I have a script like this running under OS X 10.8. The problem arises when the find command encounters a space in the path name. I need the "dir" variable as I'll be extending the script to more general use.

set dir = "/Users/apta/Library/Mail\ Downloads/2012-12-20/Testimonials.mbox/"
ls -l ${dir}
find ${dir} -name '*.emlx' | xargs grep -ih "^From: " | awk -f emlx_addresses.awk | sort -u -t "," -k 2

The ls command works fine in the script but the find command encounters this error:
find: illegal option -- n
usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
       find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]

If I enclose the path variable with quotes
find "${dir}" -name '*.emlx' |  ...

then this error comes up:
find: ftsopen: No such file or directory

I'm baffled; I added the IFS as suggested in another posting but it didn't help.
# 2  
spaces in file names are a pain.
This will force find to "think" ${dir} is a single object i.e., it won't parse out two different directory names:

find "${dir}" -name '*.emlx' ......

Undo IFS. The rest of your script does not work - IFS is the source of your error.
And try not to set IFS globally like you did. It messes up everything else.

To get around problems you must unroll your one liner:

find "${dir}" -name '*.emlx' |
while read fname
    grep -ih '^From: '  "$fname" 
done  | awk -f emlx_addresses.awk | sort -u -t "," -k 2

I left the red part alone because I cannot tell if there are problems in there. Notice the change in single and double quotes.
Any filename with spaces has to be quoted. And you have to perpetuate quoted filenames on down through code until the filenames go away. This is only one way to do this. You can use
xargs command "{}"

if you like. But you need quotes.

Last edited by jim mcnamara; 12-27-2012 at 10:46 AM..
# 3  
This is how you assign value to variable in bash and most other shells:

set var = val

This User Gave Thanks to binlib For This Post:
# 4  

Thanks for your help, Jim. I need all the file names to be piped to awk in a single stream so unrolling the loop would not work for my purposes.

You're very right: spaces in filenames are a royal pain. Smilie

But after lots of trial and error I found that this does work: Smilie
dir="/Users/apta/Library/Mail Downloads/2012-12-20/"${mailbox}".mbox/"

find "${dir}" -name '*.emlx' -print0| xargs -0 awk -f emlx_addresses.awk | sort -u -t "," -k 2 > ${mailbox}.txt

I removed the "\ " in the value for variable dir and added double quotes around the directory in find as you pointed out. I also had to add -print0 to find and -0 to xargs because there were too many (>300) file names.

One of my original errors was misusing the bash syntax for setting variables. I had originally developed the script in tcsh and converted to bash hoping to get better results but failed to change the "set dir =..." statement (thank you binlib).

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Gadgets
Difficulty: Easy
Microphones can be used not only to pick up sound, but also to project sound similar to a speaker.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Space in PATH variable

Hi All, I did a review of some threads to see if someone had come across this problem. My system is an oracle virtual box solaris 10 installed on a windows 10 system. Right now I am learning scripting and am just playing with the env variables. I am trying to update the PATH to... (9 Replies)
Discussion started by: bdby
9 Replies

2. UNIX for Dummies Questions & Answers

How can i assign directory path to a variable in perl?

Hai how can I assign directory path to a variable in perl Thanks&Regards kiran (2 Replies)
Discussion started by: kiran425
2 Replies

3. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

4. OS X (Apple)

Compiling fails due to space in path to home folder

I seem to have issues compiling software and I think I've narrowed it down to something having to do with having a space in the path name to my Home folder (which contains "Macintosh HD"). The reason I think this is shown here: $ echo $HOME /Volumes/Macintosh HD/Users/Tom $ cd $HOME -sh:... (7 Replies)
Discussion started by: tdgrant1
7 Replies

5. Shell Programming and Scripting

Setting path variable with a space.

Hi I am using MKS Toolkit c shell. I am trying to set a path variable something like c:/Program Files/blah/blah so set path=(c:/Program Files/blah/blah) this, however, does not work as it splits this thing up into 'c:/Program' and 'Files/blah/blah'. Does anyone have any ideas on... (9 Replies)
Discussion started by: vas28r13
9 Replies

6. Shell Programming and Scripting

Custom directory path variable

I'm trying to write my first shell script and got a bit stuck with this: I've got that executes from /fromhere. If the script is run with the syntax ./ tothere: I need to make a variable inside the script containing /fromhere/tothere ...and if the script is run with... (10 Replies)
Discussion started by: Chronomaly
10 Replies

7. AIX

Find command fails in crontab

Hi , I imported find command I have on my hp-ux server to clean up the /tmp of my new IBM AIX servers. Though, the commands always fails in the cron but if I past it at the prompt, it works find. I tried with at jobs and regular 'find' . Could anyone tell me what I am doing wrong? Many... (4 Replies)
Discussion started by: cforget2810
4 Replies

8. Shell Programming and Scripting

Variable directory name in path

I need to write a login script for multiple computers, however, one of the directories in question will have a different name from computer to computer. ~/Library/Application\ Support/Firefox/Profiles/<unique filename>.default/myfile For the directory named <unique filename>.default , I... (2 Replies)
Discussion started by: glev2005
2 Replies

9. Shell Programming and Scripting

Need to execute a command on a path with a space in it...

Hello, I need to execute the following command in a script: /usr/bin/ssh vmware-cmd -v /home/virtual machines//Machine.vmx"createsnapshot Weekly_Backup >/dev/null 2>&1 The problem is that there is a space between virtual and machines and when I run the script I get no such... (2 Replies)
Discussion started by: mojoman
2 Replies

10. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

Featured Tech Videos