Handling directory with spaces in for loops

Hi everyone,

I have been a big fan here since a couple years (since I started being an admin ...) and finally decided to become a member and help ppl and perhaps being helped

Now I have a problem that might interest some of the gurus.

I am abig fan of what I call "one liners". I am trying to obtain a "tree view" kind of display with the name of the group that owns the folder. I got it except for folders with spaces :confused:

Here is the first part of my "code" arranged in a more "human readable maner:

auditpath="/path/audited"
auditoutput="/export/home/XX/server_path_audited.txt"
for I in `find $auditpath -type d | grep -v .snapshot`
do
  tree=`echo $I | sed -e 's;[^/]*/;|____;g;s;____|; |;g'`
  group=`ls -ld $I | awk '{print "  "$4}'`
  echo $tree"    "$group
done

Output looks cool :o

| |____forlder     foldergrp
| | |____subfolder     subfolderg
| | | |____subfolder     subfolderg
| | | |____subfolder     subfolderg

But when it comes to folder with spaces :mad: it can't handle it :

| | | | | | |____subfolder     subfolderg
| | | | | | | |____subfolder     subfolderg
| | | | | | | |____subfolder     subfolderg
| | | | | | | |____sub
folder
spaced

The actual folder is supposed to be "sub folder spaced" or whatever ...

I based my code on something I found somewhere (sorry, no originale credits :frowning: )

find . -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'

Which handles spaces perfectly.

Any idea on the way I can handle this?

BTW this is Solaris 8 9 and 10 :smiley:

Big thanks to those who can take time to check this out.

Regards,

---
Spelling warning: English is NOT my primary langage :slight_smile:

Welcome to the forum, plmachiavel (as a member). Try using read, like so:

auditpath="/path/audited"
auditoutput="/export/home/XX/server_path_audited.txt"
find $auditpath -type d | grep -v .snapshot |
while read I 
do 
  tree=`echo "$I" | sed -e 's;[^/]*/;|____;g;s;____|; |;g'`
  group=`ls -ld "$I" | awk '{print " "$4}'`
  echo $tree" "$group
done

Perderabo, perhaps?

1 Like

Big thanks! I will have to re-code a big part of my stuff to include that but it works smoothly ! :D:D

It was apparently sooo easy to figure out that I missed it ...

And YES the original credits goes to Perderabo :smiley: