Hi, I'm trying to write a script that search file in folder.
I got problems with selecting specific file from the list of the files.
when I runing the script I get a list of files instead of one specific file.
I'm new with linux
#!/bin/bash
FILE=false
ISFOUND=false
while getopts f:d: OPTION
do
case $OPTION in
f) FILE=$OPTARG ;;
d) dirVar=$OPTARG
esac
done
IFS=$'\n'
for file in "$(ls -1 $dirVar)" ; do
if [ "$file" = "$FILE" ] ; then
ISFOUND=true
echo "found file : $FILE"
fi
done
unset IFS
if [ "$ISFOUND" = "false" ] ; then
echo "file does not exists"
fi
#!/bin/bash
while getopts f:d: OPTION
do
case $OPTION in
f) FILE=$OPTARG ;;
d) dirVar=$OPTARG
esac
done
ls "$dirVar/$FILE" && echo "found file : $FILE"
If it gives you a list of files it's because your directory/file specifications corresponds to more tyhan one file. With wildcards like '*' it's not surprisingly.
Ok I succeed to find a file in a folder
and to declare if the "file" that I found is a folder or not.
now I'm trying to get in the subdirectories and to search there recursive,
I tried to write the scrip but I'm getting bugs.
Don't know where's my problem...need help with that please!
thanx!
here is my script.
#!/bin/bash
FILE=false
ISFOUND=false
while getopts f:d: OPTION
do
case $OPTION in
f) FILE=$OPTARG ;;
d) diyrVar=$OPTARG
esac
done
function search ()
{
IFS=$'\n'
for file in $(ls -1 $dirVar) ; do
if [ "$file" = "$FILE" ] ; then
if [ -d $file ] ; then #is a folder
#search $dirVar/$file #go recurcive to the folder
echo "$file is a folder"
else #is not a folder
ISFOUND=true
echo "found file : $FILE"
fi
fi
done
unset IFS
if [ "$ISFOUND" = "false" ] ; then
echo "file does not exists"
fi
}
search