LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   search function (bash script) (http://www.linuxquestions.org/questions/programming-9/search-function-bash-script-186473/)

LYK 05-27-2004 09:06 AM

search function (bash script)
 
Here are some questions for all bash scripting experts out there

1. How do I implement a recursive search function in a bash script ? For example, If I type the name of a directory, it would then start searching that specified directory. What does it search for, you ask ? well, how about : the longest named file ?

2. Here's another one, but instead of searching directories, it searches for files. If I type several filenames, it would then perform a search. The final result would then display the newest file from the list.

Hope to hear a reply soon. And Thank you in advance !

LYK

Oliv' 05-27-2004 11:29 AM

Hello,

I think that find command can do all that things...
Code:

man find
for more precisions :)

Oliv'

Hko 05-27-2004 11:51 AM

Code:

#!/bin/bash

echo -n "Enter directory: "
read DIR
cd $DIR >/dev/null

echo "Longest file name in $DIR is:"
ls | while read L ; do
        test -f $L && echo "${#L} $L"
done | sort -nr | head -1 | cut -d' ' -f2
exit 0



All times are GMT -5. The time now is 03:13 PM.