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

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

Post by NSIMBA Gu » Thu, 22 Jun 2006 12:15:00



Hi,

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

- 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

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

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

Thank you for your assistance
GUY

 
 
 

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

Post by Chris F.A. Johnso » Thu, 22 Jun 2006 12:51:08



Quote:> 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.

Quote:> - 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'.

Quote:> 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'.

Quote:> 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

 
 
 

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

Post by Stephane Chazela » Thu, 22 Jun 2006 15:54:51


[...]

Quote:> 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).

As you used [ -f ... ], it's if the file is not present or is
not regular (or a symlink to a regular file).

Quote:>    exit 1
> fi
> rm tempfile

--
Stephane
 
 
 

1. Testing for presence of files

Could anyone please help????

I have encountered a difference between how Sco Unix and Red Hat Linux 5.2
interprets metacharacters in script tests.

E.g.

if [ -f file*.txt ]
then
    echo files present
fi

This works in Unix if there is more than one file matching the criteria, but
it doesn't in Linux. I get the error "Too many arguments".

Is there any way around this ??

2. Compaq Business Audio?

3. test for presence of file on remote system / rsh problem

4. How would you measure bandwidth?

5. File Creation Date

6. Quest for personal Enlightenment(?)

7. How do you find out the creation date of a file?

8. HDD very slow ?

9. File Creation Date

10. Determining the file creation date in Korn shell?

11. getting the creation date of a file

12. How can I get the CREATION date of a file?

13. how to get creation date/time of o file in aix4.3.3