Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

script to view files based on date

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-03-2005
krahuliyer krahuliyer is offline
Registered User
 
Join Date: Oct 2005
Last Activity: 1 December 2005, 4:47 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 10-03-2005
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 8 February 2016, 5:12 PM EST
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 Posts
Here is a script for you.


Code:
#! /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
Code:
./krah.sh YYYY-MM-DD

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

Vino

Last edited by vino; 10-03-2005 at 08:44 AM..
Sponsored Links
    #3  
Old Unix and Linux 10-04-2005
cskumar cskumar is offline
Registered User
 
Join Date: Oct 2005
Last Activity: 17 March 2016, 3:55 AM EDT
Posts: 91
Thanks: 0
Thanked 1 Time in 1 Post
Need explanation

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
    #4  
Old Unix and Linux 10-04-2005
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 8 February 2016, 5:12 PM EST
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 Posts
In short RTFM.

From man touch


Code:
       -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,


Code:
       -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
Sponsored Links
    #5  
Old Unix and Linux 10-04-2005
cskumar cskumar is offline
Registered User
 
Join Date: Oct 2005
Last Activity: 17 March 2016, 3:55 AM EDT
Posts: 91
Thanks: 0
Thanked 1 Time in 1 Post
thanks a lot

hi vino,,

thanks a lot !!!!!!

regards,
cskumar
Sponsored Links
    #6  
Old Unix and Linux 10-05-2005
krahuliyer krahuliyer is offline
Registered User
 
Join Date: Oct 2005
Last Activity: 1 December 2005, 4:47 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Help with the script of day before yesterday

Quote:
Originally Posted by vino
Here is a script for you.


Code:
#! /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
Code:
./krah.sh YYYY-MM-DD

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

Vino


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
Sponsored Links
    #7  
Old Unix and Linux 10-05-2005
monsanbu monsanbu is offline
Registered User
 
Join Date: Sep 2005
Last Activity: 3 November 2005, 10:42 PM EST
Location: Chennai,India
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to view files from a specific date/day mady135 Shell Programming and Scripting 1 05-03-2011 07:11 AM
view gzipped files with name file.gz.$DATE on a Solaris box (without unzipping first) slashdotweenie UNIX for Dummies Questions & Answers 4 04-12-2011 03:37 PM
Need script to select multiple files from archive directory based on the date range bbc17484 Shell Programming and Scripting 3 01-22-2010 12:45 AM
script to search context from 2 files based on date and name ezmethod Shell Programming and Scripting 1 08-24-2009 06:15 PM
Count of files based on date? sbasetty Shell Programming and Scripting 6 01-11-2007 02:02 PM



All times are GMT -4. The time now is 03:01 PM.