To get the files in a directory for the given date (User entered date)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To get the files in a directory for the given date (User entered date)
# 1  
Old 10-09-2013
Lightbulb To get the files in a directory for the given date (User entered date)

Need a ksh script to get the files that were created or modified in a directory on a particular date entered by the user.

For example if a directory contains files as below :

Code:
> ll
total 41
-rw-rw-r--    1 psn      psn           199 Aug 23 07:06 psn_roll.sh
-rw-rw-r--    1 psn      psn           451 Aug 24 09:24 x.log
-rw-rw-r--    1 psn      psn           451 Aug 23 05:27 y.log
-rwxrwxr-x    1 psn      psn           451 Aug 24 09:29 z.log
-rw-rw-r--    1 psn      psn           233 Aug 23 09:28 Items1.txt
-rw-rw-r--    1 psn      psn           233 Aug 24 12:00 Items2.txt
drwxrwxr-x    2 psn      psn          2119 Aug 23 08:44 final
-rwxrwxr-x    1 psn      psn             6 Aug 25 08:36 sample.sh
-rw-rw-r--    1 psn      psn            85 Aug 23 06:17 best.log

When a user enters a date 2013/08/23

I have to get the files that were created or modified on that date.

Desired output:

Code:
Enther the date: 2013/08/23

psn_roll.sh
y.log
Items1.txt
best.log

# 2  
Old 10-09-2013
Date manipulation is very system specific what Operating system is this to be run on (e.g. output from uname -a)?
# 3  
Old 10-09-2013
Lightbulb

Quote:
Originally Posted by Chubler_XL
Date manipulation is very system specific what Operating system is this to be run on (e.g. output from uname -a)?
Not accessible to machine now. But I use AIX 6.1 OS machine with ksh88 version.

Hope this will be helpful.
# 4  
Old 10-09-2013
Unfortunately your options for date manulipitation under AIX 6.3 are limited.

I'd suggest using perl to convert entered date to epoch time and work with that.

Code:
#!/bin/ksh

printf "Enter date: "
read date

year=${date%%/*}
month=${date#*/}
month=${month#0}
month=${month%%/*}
let month=month-1
day=${date##*/}

epoch=`perl -e "use Time::Local; print timelocal(0,0,0,$day,$month,$year);"`
now=`date +%s`
let age=now-epoch
let age=age/86400

find . -mtime $age -print

---------- Post updated at 08:18 AM ---------- Previous update was at 07:38 AM ----------

Some testing of this solution reveals that find will use the current time when matching so files listed will not always be on correct date (uses 24 hours before current time on the target date)

Also the output of ls changes when files are older than 1 year so greping directly form ls is not reliable.

Best bet is to grep the output of the AIX istatus command that lists files actual timestamps something like this should work for you (I don't have AIX available to test it fully).

Code:
#!/bin/ksh

printf "Enter date: "
read date

year=${date%%/*}
month=${date#*/}
month=${month#0}
month=${month%%/*}
let month=month-1
day=${date##*/}

TIMEMATCH=`perl -e 'use Time::Local; 
use POSIX qw(strftime);
print strftime("%a %b %e .* %Y", localtime(timelocal(0,0,0,'"$day,$month,$year)));"`

for file in *
do
   istatus "$file" | grep -q "$TIMEMATCH" && echo $file
done


Last edited by Chubler_XL; 10-09-2013 at 07:23 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 10-10-2013
Lightbulb

Quote:
Originally Posted by Chubler_XL
Unfortunately your options for date manulipitation under AIX 6.3 are limited.

I'd suggest using perl to convert entered date to epoch time and work with that.

Code:
#!/bin/ksh

printf "Enter date: "
read date

year=${date%%/*}
month=${date#*/}
month=${month#0}
month=${month%%/*}
let month=month-1
day=${date##*/}

epoch=`perl -e "use Time::Local; print timelocal(0,0,0,$day,$month,$year);"`
now=`date +%s`
let age=now-epoch
let age=age/86400

find . -mtime $age -print

---------- Post updated at 08:18 AM ---------- Previous update was at 07:38 AM ----------

Some testing of this solution reveals that find will use the current time when matching so files listed will not always be on correct date (uses 24 hours before current time on the target date)

Also the output of ls changes when files are older than 1 year so greping directly form ls is not reliable.

Best bet is to grep the output of the AIX istatus command that lists files actual timestamps something like this should work for you (I don't have AIX available to test it fully).

Code:
#!/bin/ksh

printf "Enter date: "
read date

year=${date%%/*}
month=${date#*/}
month=${month#0}
month=${month%%/*}
let month=month-1
day=${date##*/}

TIMEMATCH=`perl -e 'use Time::Local; 
use POSIX qw(strftime);
print strftime("%a %b %e .* %Y", localtime(timelocal(0,0,0,'"$day,$month,$year)));"`

for file in *
do
   istatus "$file" | grep -q "$TIMEMATCH" && echo $file
done

Thanks for your solution.

Unfortunately I dont have the required perl modules installed in my machine neither I do not have admin rights for that machine to install the required modules.

Kindly help me with some other solution.
# 6  
Old 10-10-2013
Do you have a C compiler available in your environment?
# 7  
Old 10-10-2013
This script I wrote may help you. I tested it on one of our AIX boxes and it seems to work pretty well. Might require some tweaking but should get you going down the right path.

Code:
#!/bin/bash
#
# showFiles.sh
#
# -- script to list files based on given date
#

# check command-line for argument
if [ $# -ne 1 ]
then
    echo "Usage: ${0##*/} <date in format YYYY/MM/DD>"
    exit 1
fi

# store the given date
dt=$1

# setup our hash table of months
#declare -a months
months=(
    key01='Jan'
    key02='Feb'
    key03='Mar'
    key04='Apr'
    key05='May'
    key06='Jun'
    key07='Jul'
    key08='Aug'
    key09='Sep'
    key10='Oct'
    key11='Nov'
    key12='Dec'
)

# function to retrieve the matching month
# given the numerical version
function getMonth()
{
    local _hashtable="$1";
    local _hashkey="$2";
    local _hashvalue="${_hashtable}[@]";
    local "${!_hashvalue}";
    eval echo "\$${_hashkey}";
}

# let's split up the given date into variables
read yy mo dy <<<$(echo $dt | sed 's/\// /g')

# access our hash table and retrieve the correct
# month name and create our date string
date_string=$(echo $(getMonth months $(echo "key${mo}")) $dy $yy)

# get the current directory and switch to it
cdir=$(echo $PWD)
cd $cdir

# run the ls command and grep for files with our date
# string
ls -l | tr -s ' ' | grep "$date_string" | awk '{print $9}'

# done
exit 0

./showFiles.sh 2013/08/23

Also, with files that are within that 6 month modication window that do not show the year, entering the date string like so seems to work (just omitting the year):

./showFiles.sh /08/23

Good luck.

39798d2bb39bd147cd2e7bec2fd0b130

Last edited by in2nix4life; 11-12-2013 at 01:48 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

2. Shell Programming and Scripting

Detect DST from a date entered by user in bash

I am trying to write a bash script that takes in a users input of a specific date in a format such as MM/DD/YYYY and returns whether or not that specific date was during daylight savings time or not. Is there a specific function that checks this? Does the date command have a way to do this? I am... (1 Reply)
Discussion started by: conman1985
1 Replies

3. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

4. AIX

Need to get the next day's date of the user entered date

I need to get the next day's date of the user entered date for example: Enter date (yyyy/mm/yy): 2013/10/08I need to get the next day's date of the user entered date Desired Output: 2013/10/09Though there are ways to achieve this is Linux or Unix environment (date command) ,I need to... (1 Reply)
Discussion started by: rpm120
1 Replies

5. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

6. Shell Programming and Scripting

find files for next day of the date entered

i have few files generated everyday with a date stamp. Sometimes it happens that if the files are generated late i.e after 00:00 hrs the date stamp will be of the next day. example: 110123_file1 110123_file2 110123_file3 110124_file4 in the above example file4 is also for the previous... (2 Replies)
Discussion started by: gpk_newbie
2 Replies

7. Shell Programming and Scripting

Number of days between the current date and user defined date

I am trying to find out the number of days between the current date and user defined date. I took reference from here for the date2jd() function. Modified the function according to my requirement. But its not working properly. Original code from here is working fine. #!/bin/sh... (1 Reply)
Discussion started by: hiten.r.chauhan
1 Replies

8. Shell Programming and Scripting

How to increment a user defined date value in the DATE format itself using shell script?

I need to increment a date value through shell script. Input value consist of start date and end date in DATE format of unix. For eg. I need increment a date value of 1/1/09 to 31/12/09 i.e for a whole yr. The output must look like 1/1/09 2/2/09 . . . 31/1/09 . . 1/2/09 . 28/2/09... (1 Reply)
Discussion started by: sunil087
1 Replies

9. Shell Programming and Scripting

validating user entered date

I need the date validation. I searched in the google but i didn't find my requirements. requirements: 1) user has to enter the date in YYYY/MM/DD format 2) MM validations 3) DD validations. and if the month is april it should allow 30 days only and for May month it should allow 31 days like... (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

10. Shell Programming and Scripting

Creating date directory and moving files into that directory

I have list of files named file_username_051208_025233.log. Here 051208 is the date and 025233 is the time.I have to run thousands of files daily.I want to put all the files depending on the date of running into a date directory.Suppose if we run files today they should put into 05:Dec:08... (3 Replies)
Discussion started by: ravi030
3 Replies
Login or Register to Ask a Question