943,712 Members | Top Members by Rank

Ad:
May 18th, 2004
0

how to recursive search of subdiretories in BASH script

Expand Post »
can anyone modify my code to make it recursive search of subdiretories, currently this code only can search of particular directory.


#!/bin/bash
if test $# -eq 0 ; then
echo usage: "${0##/*} [Dir name]"
exit
fi
for arg in $* ; do
if test -e $arg ; then
maxLen=0 # <= !
files=$(ls $arg) # <=
for file in $files ; do
newLen=${#file}
if [ $newLen -gt $maxLen ] ; then
maxLen=$newLen
fileName=$file
fi
done
echo "[ Direcroty ] : $arg"
echo "[ File name ] : $fileName"
echo "[ Max length ] : $maxLen"
else
echo Cannot find $arg
fi
done
#End


botherguy
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
botherguy is offline Offline
1 posts
since May 2004
Jul 12th, 2004
0

Re: how to recursive search of subdiretories in BASH script

Hi there

Perhaps this thread may give you pointers on what you are trying to do, it includes code examples for a similar question a few weeks ago:
Thread on bash searching

From what you have shown wouldn't the find command be more appropriate for you though?

ie:

Shell Scripting Syntax (Toggle Plain Text)
  1. find . -name filename -print

That will recurusivly (sp?) search through directories finding all occurances of the file called filename.

HTH

Ben
Reputation Points: 66
Solved Threads: 3
Junior Poster
liliafan is offline Offline
117 posts
since Apr 2004
Jul 18th, 2004
0

Re: how to recursive search of subdiretories in BASH script

I just had to creat a script that would recursively list files that woould equal or exceed X number of bytes with 2 arguments: the first one woould be where to start looking and the second the size to equal or exceed. For example ./test.sh /etc 5000

This is the code I used:

#!/bin/bash
#validate there are 2 arguments passed
#validate argument 1 exists and display an error if it does not
#validate argument 2 is a number and display error if not
directory=$1
size=$2
find $1 -size +$2c
if test -d $1; then directory=$1; else echo "Directory invalid"
fi
if test $size -ge 0; then size=$2; else echo "Specify a file size"
fi
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tccummings is offline Offline
15 posts
since Jul 2004
Oct 19th, 2009
0

recursive list of files in a given directory with length > 30

#!/bin/bash
if test $# -eq 0 ; then
echo usage: "${0##/*} [Dir name]"
exit
fi
echo 'Search in directory $* for the flienames with length > 30'
for arg in $* ; do
if test -e $arg ; then
maxLen=30 # <= !
files=$(ls -lR $arg | grep '[^d]' | awk '{print $9}') # <=
for file in $files ; do
newLen=${#file}
#newLen=`expr length $file`
#echo "Length of the $file : $newLen"
if [ $newLen -ge $maxLen ] ; then
#maxLen=$newLen
fileName=$file
#echo $fileName
rootdir=$arg
echo `find $rootdir -name $fileName`
echo $dir
fi
done
else
echo Cannot find $arg
fi
done
#End



Click to Expand / Collapse  Quote originally posted by botherguy ...
can anyone modify my code to make it recursive search of subdiretories, currently this code only can search of particular directory.


#!/bin/bash
if test $# -eq 0 ; then
echo usage: "${0##/*} [Dir name]"
exit
fi
for arg in $* ; do
if test -e $arg ; then
maxLen=0 # <= !
files=$(ls $arg) # <=
for file in $files ; do
newLen=${#file}
if [ $newLen -gt $maxLen ] ; then
maxLen=$newLen
fileName=$file
fi
done
echo "[ Direcroty ] : $arg"
echo "[ File name ] : $fileName"
echo "[ Max length ] : $maxLen"
else
echo Cannot find $arg
fi
done
#End


botherguy
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ramkmaddela is offline Offline
1 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Changing a string in 1500 documents
Next Thread in Shell Scripting Forum Timeline: perform file actions located in another direcory





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC