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.
The ls command works fine in the script but the find command encounters this error:
If I enclose the path variable with quotes
then this error comes up:
I'm baffled; I added the IFS as suggested in another posting but it didn't help.
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:
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:
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
if you like. But you need quotes.
Last edited by jim mcnamara; 12-27-2012 at 10:46 AM..
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.
But after lots of trial and error I found that this does work:
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).
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)
Hi All,
i had to split one files into 10 equally. For that i have coded below awk.
OUTPUT_FILE=/home/sit/path/Files/file_EXPORT.lst
DIR_NM=`dirname ${OUTPUT_FILE}`
awk -v CURR_DATE="$(date +'%d-%m-%Y-%H-%M')" -v pth=$DIR_NM '{print >> pth/"tgt_file_name"CURR_DATE"_"NR%10 }' ${OUTPUT_FILE}
... (7 Replies)
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)
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)
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)
I'm trying to write my first shell script and got a bit stuck with this:
I've got myscript.sh that executes from /fromhere.
If the script is run with the syntax ./myscript.sh tothere:
I need to make a variable inside the script containing /fromhere/tothere
...and if the script is run with... (10 Replies)
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)
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)
Hello,
I need to execute the following command in a script:
/usr/bin/ssh 205.21.1.1 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)
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)