Directories with spaces in name - looking for help with questions in that issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Directories with spaces in name - looking for help with questions in that issue
# 1  
Old 02-28-2008
Directories with spaces in name - looking for help with questions in that issue

I need to work with 'nice' directory names which have some spaces in name, and that brings some questions and not-understanding.

Would some expert help me out how to deal with that?!

My task is to document some processing, running from some script. I do need to have spaces in directories names, and those directory dynamic - in term of depend on processing way.

My actions are:
1. check if directory exist
2. if not - create it
3. use it to copy file to it.

I make a function to check a directory. Generaly speacking, it's doing next:
Code:
make_sure_dir_exist()  # checks if directory (first parameter) presented and, if not, create it
{
     id1="=== $0.make_sure_dir_exist()==\n"
   if [ -z $1 ]; then test_return 1 "$id1 ERR: called without the directory name (first argum)";
   fi
   dir="$*"
   
   if [ -d ./"${dir}" ] ; then return 0;
   else mkdir ./"${dir}";    test_return $? "$id1 ERR: Most likely 'dir' ( = $dir ) not appropriate for 'mkdir'"
   fi;
     ## to remove dir with all contents : rm -R <dir_name>
}  # end of the make_sure_dir_exist()

That works fine until the received directory is in one level. For 2 level unexistend dir it does not work.
I mean if received dir is "lev_1/lev_2/" and ./lev_1 does not exist, the "mkdir" has error.
I see a solution to parce the received dir by the '/' and check for every level and create it if nessesary.

I've used 'nawk' for that:
Code:
dir="test/lev1/-3-lev2/-2-lev3 -"
echo $dir|nawk -F/ '{curr=""; for (i=1;i<=NF;i++) {curr=curr$i"/"; print curr;}}'

It result as expected:
Code:
test/
test/lev1/
test/lev1/-3-lev2/
test/lev1/-3-lev2/-2-lev3 -/

Now I need to check those dirs for existance and mkdir if nessesary.

And here problems start!

First attempt is to use shell command inside the nawk block. So, instead of 'print curr' I would do some shell 'if..then..fi'

1. But how to pass the nawk variable to shell command? (this is first question)

Well, can't do from nawk, let nawk pass dirs out to shell, and process it outside of nawk.
I could use 'for .. in ..'
This way works fine untill spaces in dir-name.
Code:
dir="test/lev1/-3-lev2/-2-lev3 -"
for cdir in `echo $dir|nawk -F/ '{curr="";for (i=1;i<=NF;i++) { curr=curr$i"/"; print curr;}}'` ; do echo $cdir; done

Now the last dir is broken on space:
Code:
test/
test/lev1/
test/lev1/-3-lev2/
test/lev1/-3-lev2/-2-lev3
-/

Ok, need to put quotations around:
Code:
src> for cdir in `ec $dir|nawk -F/ '{curr="";for (i=1;i<=NF;i++) { curr=curr$i"/"; print "\""curr"\"";}}'` ; do ec $cdir; done
"test/"
"test/lev1/"
"test/lev1/-3-lev2/"
"test/lev1/-3-lev2/-2-lev3
-/"

2. Here I do not understand why it is happend this way? Why double quotation treated as a part of the string, but not as an indicator of string beginning and end?,
This way it is working fine:
Code:
src> for cdir in "test/" "test/lev1/" "test/lev1/-3-lev2/" "test/lev1/-3-lev2/-2-lev3 -/" ; do ec $cdir; done
test/
test/lev1/
test/lev1/-3-lev2/
test/lev1/-3-lev2/-2-lev3 -/

To print it as one string does not halped also:
Code:
src> for cdir in `ec $dir|nawk -F/ '{curr="";for (i=1;i<=NF;i++) { curr=curr$i"/"; all=all" \""curr"\"";}} END{ print all; }'` ; do ec $cdir; done 
"test/"
"test/lev1/"
"test/lev1/-3-lev2/"
"test/lev1/-3-lev2/-2-lev3
-/"

I've tryed to set array with dirs. Again the same result:
Code:
src> arr=(`ec $dir|nawk -F/ '{curr="";for (i=1;i<=NF;i++) { curr=curr$i"/"; all=all" \""curr"\"";}} END{ print all; }'`)
src> ec ${arr[3]}
"test/lev1/-3-lev2/-2-lev3

.. but
Code:
 src> arr=("test/" "test/lev1/" "test/lev1/-3-lev2/" "test/lev1/-3-lev2/-2-lev3 -/")
src> ec ${arr[3]}
test/lev1/-3-lev2/-2-lev3 -/

Also I've tried to add '\' to space - does not work:
Code:
 
src> arr=(`ec $dir|nawk -F/ '{curr=""; print $0; for (i=1;i<=NF;i++) { gsub(" ","\\\\ ",$i); print $i}}'`)
src> ec ${arr[5]}; ec ${arr[6]}
-2-lev3\
-

Could anyone give me some highlight why I could not get space processed as a part of string?
Also, I am out of idea how to be with the spaces in dir name.
I would appreciate any suggestions on this mater, also.

Last edited by alex_5161; 02-29-2008 at 04:16 PM..
# 2  
Old 02-28-2008
I must confess, I stopped reading at the part where you talk about the need to make each level directory as you go.

Why not just use the -p flag to mkdir? Might save some effort...
# 3  
Old 02-29-2008
Quote:
Originally Posted by Smiling Dragon
...
Why not just use the -p flag to mkdir? Might save some effort...
Well, just because I did not know that! Smilie
Definitely, it is solution to that task! Thank you! Smilie

But raised questions still do not let me slip well Smilie (at work, at least )
- How to pass 'nawk' variables to a shell command, called inside of nawk?
- Why the double-quoted strings, produced by nawk are treated differently than the same string, typed in command line? (a space in double quoted string from nawk still processed as a word separator, and double-quotes are treated as a part of string, instead of as string boundaries.)
Quote:
Originally Posted by Smiling Dragon
I must confess, I stopped reading at the part where you talk about the need to make each level directory as you go.
...
I am agree, it is comes out as a big and uncomfortable to read post.
Later I will edit it to make shorter and more obvious in questions.

Anyway, thank you for replay and hint.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Directories having spaces

Hi I have a list file with complete path of files and there are directories with spaces. Now when i read the list file, read the line and read the file it says no file found because of the space. How can I get through this issues for file in $(<list_file.txt) do first_line=`head -1... (8 Replies)
Discussion started by: lijjumathew
8 Replies

2. Shell Programming and Scripting

Directories with spaces

My code path=`find /root/folder/ -maxdepth 100 -type d | sort --random-sort | head -1` if this returns a directory with spaces in for instance: /root/folder/this is a folder is there a way to replace the value of path with the same value but with the spaces replaced so its... (2 Replies)
Discussion started by: digitalviking
2 Replies

3. Shell Programming and Scripting

White spaces issue with shell variables

Hi all, I've a requirement as below Source file src.txt sample data: A<10 white spaces>B12<5 white spaces>C<17 white spaces> A1<5 white spaces>B22<5 white spaces>C13<17 white spaces> when I'm fetching a record from this file into a shell variable like below: vRec=`head -1 src.txt... (2 Replies)
Discussion started by: madhu_1126
2 Replies

4. Shell Programming and Scripting

Issue with spaces in Java command line options

Hi, I am writing a shell script to build Java options dynamically in a variable array and pass them to java.exe. If an option value contains a space, I cannot find a way to get it interpreted correctly. Here is my example: #!/bin/bash JAVA_HOME=/opt/jvm/jre1.5.0_18 JAVA_OPTS=("-Xms256m... (4 Replies)
Discussion started by: Romain
4 Replies

5. Shell Programming and Scripting

find -exec directories with spaces

All, I have a cleanup script that removes directories and all contents underneath, but I am having issues with directories with spaces. This is the command I am currently running, how can I get it to work with directories with spaces? find /path -mindepth 3 -type d -exec rm -rf {} \; (29 Replies)
Discussion started by: markdjones82
29 Replies

6. Emergency UNIX and Linux Support

Spaces in File Name issue

Hi, I have a small issue while using variables for a file name in UNIX. I have file name like "abc def 123.txt" Folder will be in /zzz/xxx/yyy I created 2 variables Folder = '/zzz/xxx/yyy' file = 'abc def 123.txt' While using this in grep command i am... (6 Replies)
Discussion started by: pssandeep
6 Replies

7. Shell Programming and Scripting

Script to find folders with spaces and end of files and directories

Hi I need a script that can search through a set of directories and can locate any file or directory that has a space at the end Filename(space) Foldername(space) I then need to remove that space within the script Hope someone can help thanks in advance Treds (8 Replies)
Discussion started by: treds
8 Replies

8. Shell Programming and Scripting

find & dirname:problems with white spaces in Directories

Hi all, my problem: (little extract from my bash-script) I want to move each file (.mov) from one directory (and many Subdirectories) to another directory (only one); after moving i want to create hardlinks to the old directories. Thatīs no problem, but now: source-directories... (4 Replies)
Discussion started by: tubian
4 Replies

9. UNIX for Advanced & Expert Users

Access permisions issue for directories

I am getting the different accessing permissions for the directories ,for some directories it is drwxr-sr-x, for some other directories it is like drwxr-xr-x. what is the difference between these 2 access permisions of the directories? (1 Reply)
Discussion started by: bishnu.bhatta
1 Replies
Login or Register to Ask a Question