getting files between specific date ranges in solaris


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getting files between specific date ranges in solaris
# 8  
Old 02-01-2012
Filenames are for example only. You will probably want to place them somewhere outside of the tree you will be searching!
Not tested on Solaris 10, but "touch" is pretty consistent across unix platforms.

Code:
touch -t 201201232359.59 start_file
touch -t 201201310000.00 end_file

ls -la *file
-rw-r--r--   1 root       sys              0 Jan 31 00:00 end_file
-rw-r--r--   1 root       sys              0 Jan 23 23:59 start_file

These file dates match you most recent post, not post #1.
You can of course create test files with assorted dates to enable you to test your search script.
# 9  
Old 02-01-2012
I found this on web simliar for what you guys are telling me and it worked pefect
Code:
#!/usr/bin/bash
printf "Enter start date( YYYYMMDD ):" 
read startdate 
printf "Enter end date( YYYYMMDD ):" 
read enddate 
touch -t "${startdate}0000.00" sdummy 
touch -t "${enddate}0000.00" edummy 
for fi in * 
do     
   if [ $fi -nt "sdummy" -a ! $fi -nt "edummy" ] ;then         
     echo ls -al $fi         
   fi 
done


Last edited by Franklin52; 02-02-2012 at 03:48 AM.. Reason: Please use code tags for code and data samples, thank you
# 10  
Old 02-01-2012
Code:
#! /bin/bash

st_dt=$1
nd_dt=$2

for file in `ls | grep -v "^d"`
do
    f_mod_tm=`stat -c%y $file | cut -d. -f1 | sed 's/[ -:]//g'`
    if [ $f_mod_tm -ge $st_dt ]  && [ $f_mod_tm -le $nd_dt ]
    then
        echo $file
    fi
done

./test.sh <start_dt_ddmmyyhhmmss> <end_dt_ddmmyyhhmmss>

Code:
$ ls -lrt
total 40
drwxrwxr-x 2 linux linux 4096 2012-01-30 05:28 test
-rw-rw-r-- 1 linux linux   49 2012-01-31 06:23 file1~
-rw-rw-r-- 1 linux linux   20 2012-02-01 06:13 file1
-rw-rw-r-- 1 linux linux   45 2012-02-01 06:13 file2
-rwxr--r-- 1 linux linux   67 2012-02-01 21:21 pinger.sh
-rw-rw-r-- 1 linux linux  299 2012-02-01 22:25 input
-rwxr--r-- 1 linux linux  330 2012-02-01 22:27 test.pl~
-rwxr--r-- 1 linux linux  335 2012-02-01 22:30 test.pl
-rwxr--r-- 1 linux linux  239 2012-02-02 05:55 test.sh~
-rwxr--r-- 1 linux linux  240 2012-02-02 05:55 test.sh
$
$ ./test.sh 20120130000000 20120201000000
file1~
test

# 11  
Old 02-02-2012
@aliyesami
The script you found on the web contains basic scripting errors.
Quote:
#!/usr/bin/bash
printf "Enter start date( YYYYMMDD ):"
read startdate
printf "Enter end date( YYYYMMDD ):"
read enddate
touch -t "${startdate}0000.00" sdummy
touch -t "${enddate}0000.00" edummy
for fi in *
do
if [ $fi -nt "sdummy" -a ! $fi -nt "edummy" ] ;then
echo ls -al $fi
fi
done
Quote:
for fi in *
Never use this construct for open-ended lists of files. It breaks if any filename contains a space character and it breaks the Shell if there are too many files.
The shebang line is for "bash" when you have "ksh".
The script does not check the type of inode. If it comes across a directory it will break.
The script logic will ignore anything created on the end date.
The script creates the reference files in the directory you are searching.
The line starting "echo" stops the "ls" executing.


The "find" approach is best.


@balajesuri
The script you post is interesting but the O/P has "ksh" not "bash" and probably won't have a Linux "stat" command.
The script contains similar errors to that posted by the O/P.
Quote:
for file in `ls | grep -v "^d"`
This will ignore every file with a name starting with a lower case "d". It will not ignore directories. Never use "for" to process a directory list.


I'm still searching for any course, book or manual which advocates using "for" with an open-ended list of filenames. This construct must have come from somewhere!
This User Gave Thanks to methyl 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

Grepping the data between 2 date ranges

Hi There, Good Day !! I have txt file containing data in the below format. There are many lines, here i have mentioned for example. cat remo.txt 2/3/2017 file1 3/4/2016 file2 6/6/2015 file5 1/1/2018 file3 4/3/2014 file4 - - - I need to grep the file names for given date rage... (11 Replies)
Discussion started by: kumar85shiv
11 Replies

2. UNIX for Beginners Questions & Answers

Search files between date ranges - Ctime usage

Hello, I am a noob and need some help. I am trying to find files created between a date range. For Example: These are files in directory. -rw-r--r-- 1 user staff 6 May 8 09:43 file1.txt -rw-r--r-- 1 user staff 6 May 8 09:43 file2.txt -rw-r--r-- 1 user... (8 Replies)
Discussion started by: r@v!7*7@
8 Replies

3. Programming

Derivation of values falling on date ranges

Hi Guys, I am having below tables used in oracle bal ID BALANCE BAL_DATE 1 -11.71 01-JAN-05 00.00.00 1 -405.71 02-JAN-05 00.00.00 1 -760.71 03-JAN-05 00.00.00 ref_table PRODUCT EFF_FROM_DATE EFF_TO_DATE TYPE MIN_AMT MAX_AMT CHARGE 12 01-JAN-05 00.00.00 01-JAN-06... (6 Replies)
Discussion started by: rohit_shinez
6 Replies

4. Shell Programming and Scripting

Sum values of specific column in multiple files, considering ranges defined in another file

I have a file (let say file B) like this: File B: A1 3 5 A1 7 9 A2 2 5 A3 1 3 The first column defines a filename and the other two define a range in that specific file. In the same directory, I have also three more files (File A1, A2 and A3). Here is 10 sample lines... (3 Replies)
Discussion started by: Bastami
3 Replies

5. Shell Programming and Scripting

awk working inside specific pattern ranges

Hi, I have a text file, which I am trying to parse. File contents: BEG Id Job1 Id Stage1 1 EN Id Job2 Id Stage2 BEG Id2 Job3 Id Stage4 2 EN I have to process the data in this between every BEG and EN. so I am trying to restrict the range and inside every... (1 Reply)
Discussion started by: Kulasekar
1 Replies

6. Shell Programming and Scripting

Generate Regex numeric range with specific sub-ranges

hi all, Say i have a range like 0 - 1000 and i need to split into diffrent files the lines which are within a specific fixed sub-range. I can achieve this manually but is not scalable if the range increase. E.g cat file1.txt Response time 2 ms Response time 15 ms Response time 101... (12 Replies)
Discussion started by: varu0612
12 Replies

7. Shell Programming and Scripting

extracting columns falling within specific ranges for multiple files

Hi, I need to create weekly files from daily records stored in individual monthly filenames from 1999-2010. my sample file structure is like the ones below: daily record stored per month: 199901.xyz, 199902.xyz, 199903.xyz, 199904.xyz ...199912.xyz records inside 199901.xyz (original data... (4 Replies)
Discussion started by: ida1215
4 Replies

8. Shell Programming and Scripting

date ranges

Hi, Please anyone help to achive this using perl or unix scripting . This is date in my table 20090224,based on the date need to check the files,If file exist for that date then increment by 1 for that date and check till max date 'i.e.20090301 and push those files . files1_20090224... (2 Replies)
Discussion started by: akil
2 Replies

9. Shell Programming and Scripting

find command: various date ranges

Hi, I have writtena script that will recursivly go into subdirecotries and report out what files there are in there that have not been accessed over various date ranges. I do this using a number of find commands: find . -path './.snapshot' -prune -o -type f -atime -8 find... (4 Replies)
Discussion started by: littleIdiot
4 Replies

10. Shell Programming and Scripting

[csh] checking for specific character ranges in a variable

I want to check if a zip code is valid, using a variable that stores the zipcode. I am not sure how I would do this in a script. I know that simply checking for the numerical range of the number will not work, because '1' would be '00001' in zip code format. I know when I am in shell, I can use... (5 Replies)
Discussion started by: userix
5 Replies
Login or Register to Ask a Question