1830238 Members
2446 Online
109999 Solutions
New Discussion

Re: perl ..with date

 
SOLVED
Go to solution
Abdul khadeer_1
Occasional Advisor

perl ..with date

Hi All,(ladies and gentlemen :))
i need help in writing the perl script which checks whether a directory exists or not and read the files in a directory and sort the files according to the date they are created
Thanks in Advance
13 REPLIES 13
Peter Nikitka
Honored Contributor

Re: perl ..with date

Hi,

- creation date is of no means at a UNIX like system and not recorded anyway
- why perl?
- for modification time you can use in a shell something like this, to get the latest m-time first:

d=/path/to/dir
[ -d $d ] && ls -t $d

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Abdul khadeer_1
Occasional Advisor

Re: perl ..with date

it is requirement in my perl program.
spex
Honored Contributor

Re: perl ..with date

Hi Abdul,

#!/usr/contrib/bin/perl
$dir="/path/to/dir/";
if (-d $dir)
{
system("ls -1t $dir");
}

;-)

PCS
H.Merijn Brand (procura
Honored Contributor

Re: perl ..with date

my @files = -d $dir ? sort { -C $a <=> -C $b } glob "$dir/*" : ();

Like that?

BTW You didn't specify
- if you wanted to sort ascending or descending
- if the directory should be descended in
- if files starting with a "." should be included

Alternate way, descending and reversing the sort:

use File::Find;
my @files;
find(sub{-f&&push@files,$File::Find::name},$dir);
@files = sort { -C $b <=> -C $a } @files;

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Abdul khadeer_1
Occasional Advisor

Re: perl ..with date

by calling system we are calling unix utility .what if i have the same code in windows :)
H.Merijn Brand (procura
Honored Contributor

Re: perl ..with date

Then my examples will still work.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Abdul khadeer_1
Occasional Advisor

Re: perl ..with date

i want sort according to the time it is modified
H.Merijn Brand (procura
Honored Contributor

Re: perl ..with date

Well, you said *Change time* in the original question.

For modification time sorting, just use -M

Sort, newest last:

my@files=sort{-M$b<=>-M$a}glob"dir/*"';

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Abdul khadeer_1
Occasional Advisor

Re: perl ..with date

for newest first the tag would be
H.Merijn Brand (procura
Honored Contributor

Re: perl ..with date

my@files=sort{-M$a<=>-M$b}glob"dir/*"';

(swap $a and $b in the sort)

see

# perldoc -f sort

Enjoy, Have FUN! H.Merijn [ don't forget to rate the answers ]
Enjoy, Have FUN! H.Merijn
Ralph Grothe
Honored Contributor

Re: perl ..with date

Though it could hardly be solved much terser than what Procura gave you,
I think the terseness has a few drawbacks.
First afaik Perl is using the csh globbing mechanism when either or glob() is called.
This means that if you have many, or long named files (caveat long paths) in the directory your script may be susceptible to exhaust the No. of chars limited by the OS (e.g. getconf ARG_MAX).
Besides the single asteriks would not expand the "hidden" dot files.
To include them as well (without also catching the current and parent dir) you would have to use some arcane shell globbing pattern like such if you want to stick with glob() (or cast it through a Perl grep()).

$ perl -e 'printf"%d files in /tmp\n",scalar@{[glob"/tmp/* /tmp/.[!.]*"]}'
290 files in /tmp

For large amounts of files or long paths you are probably better off with additional opendir(), readdir(), closedir().
If you need to recurse into subdirs as well combine those in a Find::File callback.

Madness, thy name is system administration
H.Merijn Brand (procura
Honored Contributor
Solution

Re: perl ..with date

Ralph's answer is completely correct, which is why I included a File::Find example earlier in this thread.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Abdul khadeer_1
Occasional Advisor

Re: perl ..with date

thanks all of you ,for helping me out