Check if file exists or not

Hi,

I want to check if the file exists or not in the directory.
i am trying below code but not working.

 
 
File="/home/va59657/Account_20090213*.dat"
echo "$File"
if [[ -e "$File" ]]; then
echo "file found"
else
echo "file not found"
fi

However i am getting file not found even if file exits as below:

below file exists at path :

 
-rw-r--r-- 1 abcd abc         0 Feb 23 00:45 Account_20090213_abc.dat

OSX 10.7.5 default bash terminal.

You are using a wildcard, see here...

Last login: Sun Feb 23 16:29:03 on ttys000
AMIGA:barrywalker~> file="/Users/barrywalker/AudioScope*.sh"
AMIGA:barrywalker~> if [ -e "$file" ] ; then echo "$file" ; else echo "File does not exist..." ; fi
File does not exist...
AMIGA:barrywalker~> file="/Users/barrywalker/AudioScope.sh"
AMIGA:barrywalker~> if [ -e "$file" ] ; then echo "$file" ; else echo "File does not exist..." ; fi
/Users/barrywalker/AudioScope.sh
AMIGA:barrywalker~> _

i want to search the file in this way only as sometimetimes the file name is

Account_20090213_abc.dat 

and sometimes

Account_20090213_bcd.dat

and various other combinations..
so how can i do this.pls advie.

Try:

exists() {
  [ -e "$1" ]
}

if exists $File; then
  echo "found files with pattern $File"
fi

--
@Barry, what if there are multiple files?

Longhand using OSX 10.7.5 default shell.
This can be simplified considerably.

#!/bin/sh
if [ "$(ls ~/AudioScope*.sh 2>/dev/null)" == "" ]
then
	echo "File(s) do(es) not exist..."
else
	ls ~/AudioScope*.sh
fi
exit 0

Results...

Last login: Sun Feb 23 16:53:36 on ttys000
AMIGA:barrywalker~> chmod 755 filelist.sh
AMIGA:barrywalker~> ./filelist.sh
/Users/barrywalker/AudioScope.sh
/Users/barrywalker/AudioScope_02-01-2014.sh
/Users/barrywalker/AudioScope_03-01-2014.sh
AMIGA:barrywalker~> _

@wisecracker, you could skip the test and the extra subshell:

if ! ls ~/AudioScope*.sh > /dev/null 2>&1; then
  echo "File(s) do(es) not exist..."
fi
1 Like