Visit Our UNIX and Linux User Community


find files from the past 7 days


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find files from the past 7 days
# 1  
Old 10-07-2008
Error find files from the past 7 days

Hi All,

I have a file which contains the listing of another directory:


>cat list.dat
-rwxr-xr-x 1 test staff 10240 Oct 02 06:53 test.txtdd
-rwxrwxrwx 1 test staff 0 Oct 04 07:22 test.txx
-rwxrwxrwx 1 test staff 132 Sep 16 2007 test_tt.sh
-rwxrwxrwx 1 test staff 193 Aug19 2007 test_ttt.sh
-rwxrwxrwx 1 test staff 45 Nov 21 2007 testfile.sh
-rw-r--r-- 1 test staff 10240 Apr 18 06:49 testtrar.tar
>


I need to extract the names of the files which are created in the last 7 days. So the output of the script will be lilke below from the above file:

-rwxr-xr-x 1 test staff 10240 Oct 02 06:53 test.txtdd
-rwxrwxrwx 1 test staff 0 Oct 04 07:22 test.txx

Is there any way to achieve this?

Thanks,
D
# 2  
Old 10-07-2008
The "easy" way is to re-generate the list using
Code:
find $DIR -mtime -7 -type f

The "hard" way involves creating a set of regular expressions based on the current date/time and range. The "logwatch" utility has a perlmodule for doing exactly this sort of thing. Respond and I'll post more info.

The really hard way is to calculate the timestamp of the file in seconds based on the ls output and compare that to the current time minus 7 days of seconds (7*24*3600). It might not be as hard as I think, but it involves some straight coding (in Perl or awk) and probably lots of debugging.
# 3  
Old 10-07-2008
I did this recently in perl this way.

Code:
$days = 7;
$dir = "/opt/application";
opendir(BIN, $dir) or die "Can't open $dir: $!";
while(defined($file = readdir BIN) ){
    next if $file =~ /^\.\.?$/;  #skip . and ..
    if(int(-M $file) < $days){
        # do something
    }
}

This only difference is you would read from file.
# 4  
Old 10-07-2008
thanks for that.

I cant use the find command as i dont have the acces to the directory. The directory is accessed by ftp. I ftp to the remote machine and get the listing from the directory into this particular file. And then from this file I need to filter out files older than 7 days. So I think only way is to use the file.

In that case the timestamp method might be the way left I guess...

Hi Photon - I have little knowlegde about perl so I cant understand what your script does. I think its opening the directory and taking each file and checking for conditions. But can that be done with the contents of a file?

Thanks again for your help
D
# 5  
Old 10-07-2008
or simply can i do the below check:
I have the timestamp of a file in a variable

ts="29 Sep 09:19"

How can I find out if this falls in last 7 days?
# 6  
Old 10-07-2008
I don't see why not.

Code:
$days = 7;
open FILE, "/opt/application/file.txt";
while(my $line = <FILE>){
    if(int(-M $line) < $days){
        #do something
    }
}

# 7  
Old 10-07-2008
There may be other wazs, but here's what I'd do. Install "logwatch". Let's say it installs into /usr/share/logwatch. Then make a little perl-script:

Code:
#!/usr/bin/perl
use lib "/usr/share/logwatch/lib";
use Logwatch ':dates';

$ENV{"LOGWATCH_DATE_RANGE"}="since -7 days";
my $SearchDate = TimeFilter('%b %d %H:%M');

while (<>) {
   print if m/\b$SearchDate\b/o;
}

Then do perl <scriptname> <filename>

You should have what you are looking for.

Previous Thread | Next Thread
Test Your Knowledge in Computers #832
Difficulty: Medium
JSON is a data serialization format.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Should pick latest file within past 3 days using UNIX script and perform steps in message below.

Hi , Can anyone help me how do perform below requirement in unix. Step1:we will receive multiple files weekly with same name(as below) in a folder(In folder we will have other files also def.dat,ghf.dat) Filenames: 1) abc_20171204_052389.dat 2)abc_20171204_052428.dat DON'T modify... (23 Replies)
Discussion started by: sunnykamal59
23 Replies

2. UNIX for Dummies Questions & Answers

Find command to get the modified files past 2 hours

Hello, How to get the modified/created files past 2 hours in Solaris with find command? Thank you. (7 Replies)
Discussion started by: balareddy
7 Replies

3. UNIX and Linux Applications

From past 10 days my one job is taking lots of time

One of my job is taking long running time. I need to identify from the unix log file can you please help how to troubleshoot. (1 Reply)
Discussion started by: Nsharma3006
1 Replies

4. Shell Programming and Scripting

Listing files of PAST 7 days and concatenate it...

Hi All, I want to list file of LAST 7 days acc. to its modified date and then concatinate. I have following piece of code.. For concatenate cat file1 file2 >> Output (For concatinating) find . -mtime -7 -exec basename {} \; (list past files but it is including . file also) Plz... (4 Replies)
Discussion started by: vivek1489
4 Replies

5. Shell Programming and Scripting

what is the find to command to find the files created last 30 days

what is the find to command to find the files created last 30 days (5 Replies)
Discussion started by: rajkumar_g
5 Replies

6. Shell Programming and Scripting

Find last 2 days files.

Need to cpy those files which are created or modified in last 2 days. bash$ ll -lrt total 184 drwxr-xr-x 2 ons dce 256 Oct 12 06:58 files -rw-r--r-- 1 ons dce 4313 Oct 14 06:06 cab.ksh -rw-r--r-- 1 ons dce 6 Oct 14 07:03 Code.txt... (2 Replies)
Discussion started by: saluja.deepak
2 Replies

7. Shell Programming and Scripting

How to find a date which is 7 days past when given current date

hii all. I have to get the date of the 7th day past from the current date. if i give the current date as sep 3 then i must get the date as 27th of august. can we get the values from the "cal" command. cal | awk '{print $2}' will this type of command work. actually my need is if today is... (17 Replies)
Discussion started by: ladtony
17 Replies

8. UNIX for Dummies Questions & Answers

creating a CSV file for past 7 days

I have a requirement which will select the files with a specific naming convention which got created in past 7 days in a specific directory.Lets say the directory is /data/XYZ and the file names follow the below nomenclature like Daily_File*.txt I just need to create one CSV file which will... (12 Replies)
Discussion started by: dr46014
12 Replies

9. Shell Programming and Scripting

Find files older than 20 days & not use find

I need to find files that have the ending of .out and that are older than 20 days. However, I cannot use find as I do not want to search in the directories that are underneath the directory that I am searching in. How can this be done?? Find returns files that I do not want. (2 Replies)
Discussion started by: halo98
2 Replies

Featured Tech Videos