print out date of files last created??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print out date of files last created??
# 1  
Old 11-10-2010
print out date of files last created??

Hello everyone, I have this script here:
Code:
use Time::Local;

opendir (D, $ARGV[0]) or die "Cant open";

foreach $file (readdir D)
{
 $path = "$ARGV[0]/$file";
 next if ! -T $path;

 $last_mod = (stat $path[9]);
 ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime ($last_mod);
 printf "%-15s: $-10s %3s %2d %4d\n",$file,$day,$mon,$year+1900,"\n";
}

The thing is: I dont want to print $sec,$min and $hour. Although this will print $mday,$mon,$year and $wday ok, but I get warning message: $sec,$min and $hour only used once.
Any suggestion on how to slice out the things I only need? I've tried to slice before converted to localtime but didn't get the dates right.
i.e.
Code:
($mday,$mon,$year,$wday) = (split //, $last_mod)[3,4,5,6]; # this is not working!!!

Any help is greatly appreciated.
Thanks,

Last edited by Scott; 11-10-2010 at 04:20 PM.. Reason: Please use code tags
# 2  
Old 11-11-2010
You could try something like this:
Code:
#!/usr/bin/perl

use warnings;
use strict;

my $dir = shift or die "usage: $0 <dirname>\n";

for my $file ( glob "$dir/*" ) {
    next unless -T $file;
    my ( $mday, $mon, $year, $wday ) =
      ( localtime( ( stat $file )[9] ) )[ 3 .. 6 ];
    printf "%-15s: %-10s %3s %2d %4d\n", $file, $wday, $mday, $mon + 1,
      $year + 1900;
}

# 3  
Old 11-13-2010
thanks for your help Radoulu. However, the script above doesn't print out the date right- different day, month, and year.

new bie,
# 4  
Old 11-14-2010
Strange, could you post the output of the following commands:

Code:
ls -l <dir_name>
./<script_name> <dir_name>

# 5  
Old 11-15-2010
sure,
Code:
ls -l <dir_name>

-rw-r--r-- 1 s98820 unix 18 Nov 12 13:06 a
-rw-r--r-- 1 s98820 unix 137 Nov 12 12:09 command

---------- Post updated at 09:42 PM ---------- Previous update was at 09:30 PM ----------

Sorry Radoulu,

Try the code again and it works. I must have typed it wrong earlier.

Thanks a lot for your help,

new bie

Last edited by Scott; 11-15-2010 at 02:56 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find the count of files by last created date based on the given date range

My unix version is IBM AIX Version 6.1 I tried google my requirement and found the below answer, find . -newermt “2012-06-15 08:13" ! -newermt “2012-06-15 18:20" But newer command is not working in AIX version 6.1 unix I have given my requirement below: Input: atr files: ... (1 Reply)
Discussion started by: yuvaa27
1 Replies

2. Shell Programming and Scripting

Copy files based on last created date

Hi, I have a requirement to copy files from a windows network drive to a Linux server using shell script based on the last created date. Ex: FileName CreatedDate/Time F1 05-01-2012 3:00 PM F2 05-01-2012 3:15 PM F3 05-01-2012 2:00 PM When i run the shell script... (1 Reply)
Discussion started by: Lee_10
1 Replies

3. Shell Programming and Scripting

[Solved] Find Files Created Recently and Print

Hi, I'm looking to create a script which will find all the files created in the last 24h in a directory starting with a few different letters and send them to the printer. This would be run through the cron each morning to print the last 24 hours files. I have started with this to find all... (2 Replies)
Discussion started by: rab
2 Replies

4. Shell Programming and Scripting

Showing files that were created at a certain Date

Guys i am having a bit of a trouble finding the creation date of a file. What i have to do is to redirect the output of a command (which i believed was ls -l but this command shows only the Modification time) into a file, which will contain all the files that were created on a certain date, for... (2 Replies)
Discussion started by: jimas13
2 Replies

5. Shell Programming and Scripting

Delete files created before specific date.

There is a system logging a huge amount of data and we need to delete some of the older logs .I mean the files that are created before one week from today. Here is a listing of files that are sitting there: /usr/WebSphere/AppServer/logs # ls -l -rw-r--r-- 1 root system 3740694 May... (5 Replies)
Discussion started by: moustafashawky
5 Replies

6. Shell Programming and Scripting

Remove files which created date before 10 days on HP-UX

Hi All, Could you please let me know if there is any one can help to create a shell script to remove some files which is the created date for them greate than 10 days (sysdate-10) Please try to email me on email removed Thanks in advance, Murad (1 Reply)
Discussion started by: murad_fayez
1 Replies

7. UNIX for Dummies Questions & Answers

Display files created on particular date

hi , i am trying to display the files created on a particular date. I have tried using find .-mtime +n but these files are created on november 6th 2007 , so i'm not sure of what the 'n' value should be. And the number of files created on that particular day are more than 5000 so i have to make a... (6 Replies)
Discussion started by: amit_kv1983
6 Replies

8. Shell Programming and Scripting

Copying files created after a specified date/time

I need to write a script that copies files from one directory to another that were created after "today 6:30". This script will be NOT be ran at the same time each day. any help is appreciated. (2 Replies)
Discussion started by: jm6601
2 Replies

9. Solaris

command to list files that are created before some date

Can you please let me know the command to list the files that are created before some date, this we want to use for the following Eg: Move all the files that got created before 2006 to new folder in Solaris (3 Replies)
Discussion started by: csreenivas
3 Replies

10. Filesystems, Disks and Memory

How to list files with specific created date

Hi, Would like to ask, which command is used to list all the files for specific date (says 1st May) and its size, for all files (including its subdirectory), in a mounted NFS disk to HP-UX. I would like to check for the total files came into my disk on 1st May. Very much appreciating your... (2 Replies)
Discussion started by: Draculla
2 Replies
Login or Register to Ask a Question