[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 975
  • Last Modified:

unix Shell script to find file timestamp

Hello, I'm trying to write a small unix shell script to check the date/timestamp of a specific file.  If the file has been updated today, then do nothing.  If the file is anything older, then create a new file.

ie.  file_name = text.txt

if the timestamp of text.txt = today then exit
if the timestamp of text.txt = yesterday, create a new text.txt (empty file)
0
slim7
Asked:
slim7
  • 2
1 Solution
 
ozoCommented:
file_name=text.txt
perl  -e '$f=shift; open F,">$f" or die $! unless -f $f && -M $f < 1' $file_name


0
 
slim7Author Commented:
How would you write it for unix shell script?
0
 
slim7Author Commented:
This is what i came up with.  Is there a better way?
#!/bin/ksh
cd /home/dir
if [[ -f text.txt]]
then
   if [[ "`date +"%b%d"`" = "`ls -l text.txt |  awk '{print $6""$7}'`" ]]
   then
       exit 0
   fi
   cat /dev/null > text.txt
fi
exit
0
 
SuperdaveCommented:
#create an empty file dated this past midnight
touch -t $(date +%m%d0000) reftime.tmp
#copy it over the file if it's newer
cp -fu reftime.tmp text.txt
rm reftime.tmp

0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now