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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

recursive search of subdirectories

Reply to Message Icon

Name: phgct2004
Date: May 16, 2004 at 21:29:45 Pacific
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



Sponsored Link
Ads by Google

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

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.


0

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

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


0

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

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.


0

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

hi,

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

thank.

chui


0

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

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


0

Related Posts

See More



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

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.



0

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

Hi,

Like to check what does this line do ?

do fqfn=$1/$file

Thankz


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: recursive search of subdirectories

Shell Scripts for Recursive Search www.computing.net/answers/unix/shell-scripts-for-recursive-search/4070.html

Check for new file, exec sql*plus www.computing.net/answers/unix/check-for-new-file-exec-sqlplus/6714.html

recursive link copy www.computing.net/answers/unix/recursive-link-copy/6214.html