Script to check for the file existence, if file exists it should echo the no of modified days

Hi,

I am looking for a shell script with the following.

  1. It should check whether a particular file exists in a location
#!/bin/sh

if [ -e /Users/Shared/xxx.txt ];
then
echo "xxx.txt File Exists"
else
echo "File Not Found"
fi
  1. If file exists, it should check for the modified date and run a command if the modified date is greater than 45 days

I have somehow managed to do the first point but I have to add the second query in my script.

Thanks!!!

On OSX, Linux, and BSD you can use the man stat (OSX) command to get the modification time. Read it into a variable using $( ... ) and check if that's greater than 45.

Hi,

I am managed to get the script with the help of my friend. It works.

#!/bin/sh


#This script checks if the <filename> esixts in the <fileloc> and also if the modification date is less than 45 days.

filename=filename.txt

fileloc="filepath"

cd $fileloc

if [ -f $filename ]; then

  echo "File Exists"
  filestr=`find . -name $filename -mtime +45 -print`
  if [ "$filestr" = "" ]; then
    echo "File is not older than 45 days"
  else
    echo "File is older than 45 days"
  fi
else

  echo "File does not exist"
fi

exit 0