Re: How to test the presence of a file and the date of its creation?



On 2006-06-21, NSIMBA Guy wrote:
Hi,

I would like to write a script which will test the existence of a file and
there creation date (file creation to day).

The creation date is not stored in a Unix filesystem. You can get
the last-modified, last-status-change or the last-access date.

- If the file is present and creation to day, the programm continuous (Code
return 0)

- If the file is not present, the program stops (code return 1).

- If the file is present but the current date does not carry, the program
stops (code return 2)

But I have difficulty to transform into code the test of the file and date
of his creation.

Here my script. Thanks for helping me to make it achievable

Thank you to correct my script because it does not good.

#!/bin/sh
# "mapasa" c'est le nom de mon fichier

if [-f file ] and [file (created to day)]"; then echo "file exist and the
programm continuous"
fi
Exit 0

There is no command called 'Exit'; I presume you mean 'exit'.

if [!-f mapasa] then echo "file not exist"

You need a space after '[' and after '!' and before ']'. You also
need a semi-colon or a newline before 'then'.

fi
Exit 1

if [-f mapasa] and [file (created to day] then echo "file is an old file"
fi
Exit 2

touch -t `date +%m%d0000` tempfile
if [ -f "$file" ]
then
if [ -n "$(find "$file" -newer tempfile)" ]
then
exit 0
else # If the file is present but the current date does not carry,
# the program stops (code return 2)
exit 2
fi
else ## If the file is not present, the program stops (code return 1).
exit 1
fi
rm tempfile

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.