help with script to list directories | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

help with script to list directories

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-13-2006
chassis chassis is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 19 September 2006, 10:01 AM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 09-13-2006
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 8 October 2014, 1:35 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts

Code:
for arg in `find $HOME -type d -exec ls -d {} \;`
do
echo ${arg##/*/}
done

Sponsored Links
    #3  
Old 09-13-2006
chassis chassis is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 19 September 2006, 10:01 AM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Your the dude

Thanks Andu23, it worked so well my script couldn't even belive it.
Your a star
Cheers
Chassis
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
List directories and sub directories recursively excluding files pointers UNIX for Dummies Questions & Answers 3 02-23-2011 08:39 PM
How to list all the directories, sub directories in a mount along with size in ascending order? nmakkena Shell Programming and Scripting 4 10-27-2010 02:56 AM
Backup script: Copying and removing directories based on list dotancohen Shell Programming and Scripting 4 02-04-2010 04:30 PM
a remove script taken in input a file which contain a list of directories yeclota Shell Programming and Scripting 2 09-09-2008 10:56 AM
Script to list changes in Directories aojmoj Shell Programming and Scripting 2 11-22-2003 09:51 PM



All times are GMT -4. The time now is 04:12 AM.