•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 427,779 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,772 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 28525 | Replies: 2
![]() |
•
•
Join Date: May 2004
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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
#!/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
•
•
Join Date: Apr 2004
Location: Virginia Beach
Posts: 113
Reputation:
Rep Power: 5
Solved Threads: 2
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:
That will recurusivly (sp?) search through directories finding all occurances of the file called filename.
HTH
Ben
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:
find . -name filename -print
That will recurusivly (sp?) search through directories finding all occurances of the file called filename.
HTH
Ben
Application development, webhosting, and much more: www.webcentric-hosting.com
•
•
Join Date: Jul 2004
Location: Oklahoma City, OK
Posts: 15
Reputation:
Rep Power: 5
Solved Threads: 0
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
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
:mrgreen:Tim Cummings
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
adsense adult advertising blogging bomb book business coding copyright development earth engine failure flash forum google interactivity. internet legal malware marketing mcafee microsoft monetization msn multimedia news operating pagerank phishing privacy publishing revenue scripting search security seo sex silverlight software spyware support system technical video web web development webmaster wiki yahoo
- Bash Mail script help (Shell Scripting)
- HELP! Who can solve this bash script? (Shell Scripting)
- script to call google search? (Perl)
- Bash shell needs tweeking (Shell Scripting)
- Linux Shell Script needs help writing program (Shell Scripting)
- web page statistics - my first bash script (Shell Scripting)
- Bash script prob (Kernels and Modules)
Other Threads in the Shell Scripting Forum
- Previous Thread: writing a script
- Next Thread: Bash shell screipt help


Linear Mode