Handling directory with spaces in for loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handling directory with spaces in for loops
# 1  
Old 06-21-2010
CPU & Memory 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 Smilie

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

Code:
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 Smilie

Code:
| |____forlder     foldergrp
| | |____subfolder     subfolderg
| | | |____subfolder     subfolderg
| | | |____subfolder     subfolderg

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

Code:
| | | | | | |____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 Smilie )

Code:
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 Smilie

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

Regards,

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

Last edited by Scott; 06-21-2010 at 01:02 PM.. Reason: Please use code tags
# 2  
Old 06-21-2010
Welcome to the forum, plmachiavel (as a member). Try using read, like so:
Code:
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

Quote:
Originally Posted by plmachiavel
I based my code on something I found somewhere (sorry, no originale credits )
Perderabo, perhaps?
https://www.unix.com/302170164-post2.html

Last edited by Scrutinizer; 06-21-2010 at 11:36 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-21-2010
Big thanks! I will have to re-code a big part of my stuff to include that but it works smoothly ! SmilieSmilie

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

And YES the original credits goes to Perderabo Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Handling filenames with spaces

I'm trying to handle some files with spaces in their name using "" or \ . Like "file 1" or file\ 1. My current confusion can be expressed by the following shell script: #!/bin/bash touch "file 1" "file 2" echo -n "ls: " ; ls echo --- for file in "file 1" "file 2" ; do echo $file... (9 Replies)
Discussion started by: Ralph
9 Replies

2. Shell Programming and Scripting

Proper way to use a path with spaces in the directory name

I am using the below bash loop: or f in /media/cmccabe/My Book Western Digital/10_29and30_2015/*.bam ; do bname=`basename $f` pref=${bname%%.bam} samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > /home/cmccabe/Desktop/NGS/${pref}_newheader.bam done is the... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. UNIX for Advanced & Expert Users

File Processing: Handling spaces in a line

Hi All, Iam trying to get a file processed and some lines have spaces...the below is not working Want to remove empty line Want to remove lines that start with # Avoid line with substring WHOA When trying to get the substring from the var also Iam having trouble file is like VAR=VALUE,... (13 Replies)
Discussion started by: baanprog
13 Replies

4. UNIX for Dummies Questions & Answers

handling white spaces with getopt

Hi I'm trying to ensure that I have catered for all situations with my getopt cases. One other situation I want to cover is should the user enter the script without any preceding arguments eg: ./script_eg I need the script to the direct the user to the helpfile I have tried... (3 Replies)
Discussion started by: ladyAnne
3 Replies

5. Shell Programming and Scripting

Handling blank spaces

Hi, I am trying to replace a specific column values in a csv file with double quotes when I am find embedded spaces with in the fields. Example: SNO,NAME,ZIPCODE,RANK,SEX,ADDRESS 1,Robert,74538,12,34, M,Robert Street, NY 2,Sam,07564,13,M,12 Main Ave, CA 3,Kim, Ed,12345,14,M,123D ,... (1 Reply)
Discussion started by: techmoris
1 Replies

6. UNIX for Dummies Questions & Answers

How to access an directory with spaces

I am getting an error while trying to access a directory created with spaces in between, i couldn't able to login into that directory could you please suggest me what should i do to access that directory or if i want to rename that directory what should i do. Directory Name: MH PLR 2005... (1 Reply)
Discussion started by: Ariean
1 Replies

7. Shell Programming and Scripting

Mutiple For loops - moving files to another directory

I need to clean out some application subdirectories from backup scripts we used to rename to various backup extensions just in case the script failed in production and we need to rollback. I will be moving these old scripts to a staging directory and then removing them after 30 days (I have the... (9 Replies)
Discussion started by: tekster757
9 Replies

8. Shell Programming and Scripting

help in handling loops

Hi, I am using bash script I want some help in handling loops in my script... I need to check for a specific interval of time whether a file in a paritcular directory exists. If exists, i need to do some action and continue with the next iteration.. //my script attempt=0... (1 Reply)
Discussion started by: borncrazy
1 Replies

9. Shell Programming and Scripting

Directory names that contain spaces and other suggestions?

The script below was written to select files and convert a particular string to something other and replace that file. However, I came across some issues with filenames that contain spaces, any suggestions to get around this? Any other suggestions that may apply to this code would also be... (5 Replies)
Discussion started by: Shakey21
5 Replies

10. Shell Programming and Scripting

handling spaces in unix

I am testing a ksh script for email. In the script I receive several parameters. One of them is a subject. The subject may contain spaces. Ex. Test this. When I am running the script on telnet to test, how should the syntax at the command line be written. I have this: ksh ResendE.sh '001111'... (2 Replies)
Discussion started by: supercbw
2 Replies
Login or Register to Ask a Question