The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Want to Get List of All directories niceboykunal123 Shell Programming and Scripting 6 05-06-2008 11:58 AM
how to list directories only yodadbl07 UNIX for Dummies Questions & Answers 3 04-26-2006 01:48 AM
How to list only directories? videsh77 UNIX for Dummies Questions & Answers 4 01-09-2006 06:10 AM
List directories malaymaru UNIX for Dummies Questions & Answers 3 09-25-2005 06:54 AM
Script to list changes in Directories aojmoj Shell Programming and Scripting 2 11-22-2003 06:51 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-13-2006
Registered User
 

Join Date: Sep 2006
Posts: 8
Question help with script to list directories

Hi All I have two scripts which i used to try and list all the directories one using 'function', which only lists the first directory and does not show directories within directories.

function ListDir ()
{
for arg in $(ls $HOME)
do
if [ -d $1/$arg ]
then
echo $arg
ListDir $arg
fi
done
}

Result

home/<username>/
backup
bin
computer
unix
oracal

There are driectories within 'computer' and 'unix' (prob with loop i think)

The other lists, as i want it, but also includes the complete path name which i dont want. i tried using 'for'

for arg in `find $HOME -type d -exec ls -d {} \;`

do
echo $arg >> outputfile
done
rm outputfile

Result
/home/<username>/
/home/<username>/backups
/home/<username>/bin
/home/<usermane>/computer
/home/<username>/computer/dept
/home/<username/unix
/home/<username>/unix/accounts
/home/<username>/oracal

This goes through a funny loop repeating its self and addidn the new found dir to the end

As you can see i just need the last directory

I know you can do it so come on help me out thank you

Chassis
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 09-13-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
for arg in `find $HOME -type d -exec ls -d {} \;`
do
echo ${arg##/*/}
done
Reply With Quote
  #3 (permalink)  
Old 09-13-2006
Registered User
 

Join Date: Sep 2006
Posts: 8
Your the dude

Thanks Andu23, it worked so well my script couldn't even belive it.
Your a star
Cheers
Chassis
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:59 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0