Computing.Net > Forums > Unix > recursive search of subdirectories

recursive search of subdirectories

Reply to Message Icon

Original Message
Name: phgct2004
Date: May 16, 2004 at 21:29:45 Pacific
Subject: recursive search of subdirectories
OS: Redhat 9.0
CPU/Ram: 886/128mb
Comment:

hi,

i'm during with writing small bash script program that will perform a recursive search of subdirectories searching for the longes named file, with given a directory name as an argument.

can anybody tell me how can i do it.

thank


chui


Report Offensive Message For Removal


Response Number 1
Name: Wolfbone
Date: May 17, 2004 at 00:13:55 Pacific
Reply: (edit)

longest () { recurse () { for file in $(/bin/ls $1) ; do fqfn=$1/$file ; [[ -d $fqfn ]] && recurse $fqfn ; [[ ${#file} -gt $len ]] && { len=${#file} name=$fqfn; } ; done ; } ; recurse $1 ; echo -e "\033[1;37m$len \033[0;33m$name" ; unset name len recurse ; }

... is one way.


Report Offensive Follow Up For Removal

Response Number 2
Name: phgct2004
Date: May 17, 2004 at 19:33:10 Pacific
Reply: (edit)

hi,

i have try the coding that been post but i still have know idea how it done and i can't get what i want. is there any other coding that is more easy to understand, or can pls make the coding more in the script form.

thank

chui


Report Offensive Follow Up For Removal

Response Number 3
Name: Wolfbone
Date: May 17, 2004 at 22:24:58 Pacific
Reply: (edit)

The code above is a single line shell function: 'longest'. The idea is that you can cut and paste it into your terminal and use it immediately:

$> longest directory_name

You could also add it to your .bashrc file but if you really want it as an independent script file you need only remove the outer function to leave you with a file looking like this:

#!/bin/bash

recurse () {
for file in $(/bin/ls $1)
do fqfn=$1/$file
[[ -d $fqfn ]] && recurse $fqfn
[[ ${#file} -gt $len ]] && { len=${#file} name=$fqfn; }
done ; }

recurse $1
echo -e "\033[1;37m$len \033[0;33m$name"

# end of file

I hope that makes it clearer. You can see that the recurse function, called with the parameter $1, lists the contents of the directory $1 then tests to see if each file is a directory and if it is, it calls itself again. When it has finished, it echoes the result to the terminal (in colour for added clarity).

You can easily change or remove the colours if you dont like them and you could also improve the script in many other ways, for example by testing $1 at the beginning to make sure it is a directory and printing an error message if it is not.


Report Offensive Follow Up For Removal

Response Number 4
Name: phgct2004
Date: May 17, 2004 at 23:07:22 Pacific
Reply: (edit)

hi,

thank for ur help and it work as what i want.

thank.

chui


Report Offensive Follow Up For Removal

Response Number 5
Name: Dlonra
Date: May 18, 2004 at 18:47:13 Pacific
Reply: (edit)

not to detract from Wolfbone and the fact it works, how about:

len=0
for f in $(find $1 -type f -printf '%f\n')
do
[ ${#f} -gt $len ] && len=${#f}
done
echo $len

BTW,
I was not aware of ${#..} - thnx


Report Offensive Follow Up For Removal


Response Number 6
Name: thepubba
Date: May 19, 2004 at 13:51:21 Pacific
Reply: (edit)

I like Dlonra's version. If you want to run the script on Windows systems (where you have the file names with spaces in them), change the IFS to a carriage return.

IFS="
"

Handy when writing scripts where you have Cygwin installed on your Windows system.



Report Offensive Follow Up For Removal

Response Number 7
Name: blurblur
Date: June 27, 2004 at 04:42:01 Pacific
Reply: (edit)

Hi,

Like to check what does this line do ?

do fqfn=$1/$file

Thankz


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: recursive search of subdirectories

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge