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)
# 8  
Old 10-10-2013
Lightbulb

Quote:
Originally Posted by Chubler_XL
Do you have a C compiler available in your environment?
Unfortunately no.

In between I came across this piece of code in Google which seems to working fine in bash but not in ksh (ksh88)
Code:
find . -type f -mtime $(( ( $(date +%s) - $(date -d '2013-10-08' +%s) ) / 60 / 60 / 24 - 1 ))

In ksh m getting an error like the
Code:
flag -d is not recognized

So is there any flag in ksh to serve this purpose.
# 9  
Old 10-10-2013
Another thought is to use touch to create a file in /tmp with the date stamp and the ls -l the temp file to fetch the ls string required

Of course the column positions (44-50) will need adjusting for your ls output under AIX

Code:
#!/bin/ksh

printf "Enter date: "
read date

year=${date%%/*}
year=${year#..}
month=${date#*/}
month=${month%%/*}
day=${date##*/}

TIMEMATCH=`touch -t ${year}${month}${day}0000 /tmp/df_$$ ; ls -l /tmp/df_$$ | cut -c44-50 ; rm /tmp/df_$$`

echo "DEBUG: Timematch is \"$TIMEMATCH\""

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

---------- Post updated at 05:58 AM ---------- Previous update was at 05:55 AM ----------

Quote:
Originally Posted by ramprabhum
Code:
find . -type f -mtime $(( ( $(date +%s) - $(date -d '2013-10-08' +%s) ) / 60 / 60 / 24 - 1 ))

This the AIX date command - it doesn't support the -d option, this is why I said AIX is tricky.

I also pointed out earlier that the find mtime option will not work correctly, files created the day before the target date but later in the day than the runtime will be listed incorrectly. e.g. if you run your script at 10am for target date 2013/08/23 a file created on 2013/08/22 @ 3pm will also be found.

Last edited by Chubler_XL; 10-10-2013 at 05:03 PM..
# 10  
Old 10-13-2013
Lightbulb

Quote:
Originally Posted by Chubler_XL
Another thought is to use touch to create a file in /tmp with the date stamp and the ls -l the temp file to fetch the ls string required

Of course the column positions (44-50) will need adjusting for your ls output under AIX

Code:
#!/bin/ksh
 
printf "Enter date: "
read date
 
year=${date%%/*}
year=${year#..}
month=${date#*/}
month=${month%%/*}
day=${date##*/}
 
TIMEMATCH=`touch -t ${year}${month}${day}0000 /tmp/df_$$ ; ls -l /tmp/df_$$ | cut -c44-50 ; rm /tmp/df_$$`
 
echo "DEBUG: Timematch is \"$TIMEMATCH\""
 
for file in *
do
   ls -l "$file" | grep -q "$TIMEMATCH" && echo $file
done

---------- Post updated at 05:58 AM ---------- Previous update was at 05:55 AM ----------



This the AIX date command - it doesn't support the -d option, this is why I said AIX is tricky.

I also pointed out earlier that the find mtime option will not work correctly, files created the day before the target date but later in the day than the runtime will be listed incorrectly. e.g. if you run your script at 10am for target date 2013/08/23 a file created on 2013/08/22 @ 3pm will also be found.
Thanks...!!! This was what I was looking for... But one problem here seems to be that it lists directory as well. I need only the filenames so I added an additional grep command with the code provided by you as given below

Code:
 
#!/bin/ksh
printf "Enter date: "
read date
year=${date%%/*}
year=${year#..}
month=${date#*/}
month=${month%%/*}
day=${date##*/}
TIMEMATCH=`touch -t ${year}${month}${day}0000 /tmp/df_$$ ; ls -l /tmp/df_$$ | cut -c
46-51; rm /tmp/df_$$ `
echo "DEBUG: Timematch is \"$TIMEMATCH\""
for file in *
do
ls -l "$file" | grep -q "$TIMEMATCH" | grep -q "^-" && echo $file
done

But this seems to be not working.
# 11  
Old 10-13-2013
Remove -q form first grep in pipe (-q causes no output and sets the return status so should only be used on last grep in chain):

Code:
ls -l "$file" | grep -q "$TIMEMATCH" | grep -q "^-" && echo $file

This User Gave Thanks to Chubler_XL For This Post:
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