script to view files based on date

I have a UNIX box where in files are created every 5 minutes. I need to write a shell script which will take in the date as parameter and return me all files created on that day. Also I will like it to show me only files which have a size greater than 0 i.e. non-empty files. how do I go abt this? I am new to UNIX

Thanks in advance,
Rahul

Here is a script for you.

#! /bin/sh
# krah.sh
[ -z $1 ] && echo "Defaulting to $(date +%F)" && DATE=$(date +%F) || DATE="$1"

touch -d "$1" /tmp/OLD
touch -r /tmp/OLD -F 86400 /tmp/NEW
find . -name '*' -newer /tmp/OLD ! -newer /tmp/NEW ! -size 0

rm -f /tmp/OLD /tmp/NEW

You should run it in the following format

./krah.sh YYYY-MM-DD

If you dont provide anything, then the current date will be picked up.

Vino

hi,

i am a fresher to scripts,
can u explain what these commands will do..

touch -d "$1" /tmp/OLD
touch -r /tmp/OLD -F 86400 /tmp/NEW
find . -name '*' -newer /tmp/OLD ! -newer /tmp/NEW ! -size 0

Thanks in advance
cskumar

In short RTFM.

From man touch

       -d, --date=STRING
              parse STRING and use it instead of current time

       -F, --forward=SECONDS
              Modify  the time by going forward SECONDS seconds.  For example,
              touch -r foo -F 5 bar will make the file  bar  5  seconds  newer
              than file foo.

       -r, --reference=FILE
              use this file's times instead of current time

That means create /tmp/OLD which has a timestamp provided by the user.

And then create /tmp/NEW which is exactly 24 hours newer compared to /tmp/OLD

And from man find,

       -newer file
              File was modified more recently than file.  -newer  is  affected
              by  -follow  only  if -follow comes before -newer on the command
              line.

       -size n[bckw]
              File uses n units of space.  The units are  512-byte  blocks  by
              default  or  if `b' follows n, bytes if `c' follows n, kilobytes
              if `k' follows n, or 2-byte words if `w' follows  n.   The  size
              does  not  count  indirect  blocks,  but it does count blocks in
              sparse files that are not actually allocated.

The find script says -- find all files which are newer than /tmp/OLD but not /tmp/NEW and whose size is not zero.

Vino

hi vino,,

thanks a lot !!!!!!

regards,
cskumar

There is a problem with this.. I am getting an error like "./Script.sh: cannot execute".. I dont know why it is happening.. CAn u help or suggest something?

This is the exact thing thats being shown (my file is called Script.sh):

$ Script.sh
ksh: Script.sh: not found
$ ./Script.sh
ksh: ./Script.sh: cannot execute
$ ls -lt
total 1
-rw-r----- 1 BTBDEV1 dba 227 Oct 5 13:46 Script.sh

$ vi Script.sh
"Script.sh" 5 lines, 227 characters
1 [ -z $1 ] && echo "Defaulting to $(date +%F)" && DATE=$(date +%F) || DAT
E="$1"
2 touch -d "$1" /tmp/OLD
3 touch -r /tmp/OLD -F 86400 /tmp/NEW
4 find . -name '*' -newer /tmp/OLD ! -newer /tmp/NEW ! -size 0
5 rm -f /tmp/OLD /tmp/NEW
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
:q

You dont have execute permission for the file Script.sh

Just run the following command to grant execute permission

chmod u+x Script.sh

-Mons