Help needed with find command

Hi!

hi
I used find command to find some file names as per input from user. I used it for current directory. It was working fine. Now I tried with giving some other directory path. Its giving issues.
Here what I tried. Script will take input from user say 1_abc.txt, find the file and print list. if files are not there it will print No Files with this name message.
Finding files in the current Directory.

echo View Log files;    
echo Enter Log file name;
read -r filename ; find  "$filename"_*.* -type f ! -name ".*" 2>errorlog 
if [ -s errorlog ] ; then echo "No Files with this name"; fi 
echo Press Enter; 
read x;;

Then I tried with some different path, as:

echo View Log files;    
echo Enter Log file name;
read -r filename ; find /backup/test1/LogFiles/ "$filename"_*.* -type f ! -name ".*" 2>errorlog | awk -F/ '{print $NF}'
if [ -s errorlog ] ; then echo "No Files with this name"; fi 
echo Press Enter; 
read x;;

Now what is the problem with above code.

  1. Its printing files names with fullpath i.e., /backup/test1/LogFiles/1_abc.txt
    This is not required. I need to print only file names.
  2. The if statement is always in execution. Its always printing the "No Files with this name" statement.
    Simply its not working. I tried to use find with -P -H options but not of use.

Urgent help is needed.
Please help me to make it work.
Thanks,

remove the file named errorlog before entering to the loop.
try to give the full path of errorlog and check the errorlog in if condition with full path.
check for the existence or non-zero size of errorlog.

for zero file size checking - it will return true

if ! [ -s errorlog ]

for file name printing try -

 print `basename $NF`

change the find cmd

find /backup/test1/LogFiles/ -type f -name "$filename"_*.* 2>errorlog

@donadarsh I didn't get you. I don't know much about scripting. So please tell in details.

@ygemici Its not working. Same result i.e., printing fullname with path and if statement doesn't work with this.
Plz help

echo View Log files;     
echo Enter Log file name;
rm errorlog
read -r filename ; 
find /backup/test1/LogFiles/ "$filename"_*.* -type f ! -name ".*" 2>errorlog | awk -F/ '{print `basename $NF`}' 
if [ -s errorlog ] ; then echo "No Files with this name"; 
fi  
echo Press Enter; 
 read x;;
# read -r filename; find /backup/test1/LogFiles/ -type f -name "$filename_*.*" ! -name ".*" 2>errorlog|awk '{sub(".*/","")}1'

Hi everybody thanks for your help.
I solved it this way.

find /backup/test1/LogFiles/$filename"_"*.* ........

It worked as per my requirements. :slight_smile: