Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a directory and sort files by date created

Tags:

file

sorting

perl

I need to open directories and sort the files by the time they were created. I can find some discussion, using tags for Perl, sorting, and files, on sorting files based on date of modification. I assume this is a more common need than sorting by date of creation. I use Perl. There is some previous postings on sorting by creation date in other languages other than Perl, such as php or java.

For example, I need to do the following:

opendir(DIR, $ARGV[0]);                             
my @files = "sort-by-date-created" (readdir(DIR)); 
closedir(DIR);

do things with @files...

The CPAN has a page on the sort command, but it's not very accessible to me, and I don't find the words "date" or "creation" on the page.

In response to an edit, I should say I use Mac, OS 10.7. I know that in the Finder, there is a sort by creation date option, so there must be some kind of indication for date of creation somehow attached to files in this system.

In response to an answer, here is another version of the script that attempts to sort the files:

#!/usr/bin/perl
use strict; use warnings;

use File::stat; # helps with sorting files by ctime, the inode date that hopefully can serve as creation date

my $usage = "usage: enter name of directory to be scanned for SNP containing lines\n";
die $usage unless @ARGV == 1;

opendir(DIR, $ARGV[0]);                             #open directory for getting file list
#my @files = (readdir(DIR));
my @file_list = grep ! /^\./, readdir DIR; 
closedir(DIR);  

print scalar @file_list."\n";

for my $file (sort {
        my $a_stat = stat($a);
        my $b_stat = stat($b);
        $a_stat->mtime <=> $b_stat->mtime;
    }  @file_list ) {
    say "$file";
}
like image 799
ES55 Avatar asked Apr 19 '13 01:04

ES55


People also ask

How do I sort a directory by date?

The dir command will sort the files in each directory by the date. It will not sort all the files in date order. To do that you would need to output the file date as YYYYMMDD with the file name and sort the file.

How do I organize files in File Explorer by date?

Just click on Date at the top of the column, and then drag it to the left. If you wanted to put the date column last, you'd do the reverse. If you then wanted to sort everything by date, single-click on the date column. Now all the files in the downloads folder will be sorted by date with the oldest items at the top.

Can you search Windows 10 for files by date?

Enter any date after datemodified: using the mm/dd/yyyy format to search for any files modified on the desired date. To find files modified between two dates, you can type the following. In this example, any files modified between January 1, 2022, and February 1, 2022, are displayed in the search results.


1 Answers

You can customize the sorting order by providing a subroutine or code block to the sort function.

  • In this sub or block, you need to use the special variables $a and $b, which represent the values from the @array as they are compared.
  • The sub or block needs to return a value less than, equal to, or greater than 0 to indicate whether $a is less than, equal to, or greater than $b (respectively).
  • You may use the special comparison operators (<=> for numbers, cmp for strings) to do this for you.

So the default sort sort @numbers is equivalent to sort {$a <=> $b} @numbers.

In the case of sorting by creation time, you can use the stat function to get that information about the file. It returns an array of information about the file, some of which may not be applicable to your platform. Last modification time of the file is generally safe, but creation time is not. The ctime (11th value that it returns) is as close as you can get (it represents inode change time on *nix, creation time on win32), which is expressed as the number of seconds since the epoch, which is convenient because it means you can do a simple numeric sort.

my @files = sort {(stat $a)[10] <=> (stat $b)[10]} readdir($dh);

I'm not sure if you want to filter out the directories also. If that is the case, you'll probably also want to use grep.

like image 190
stevenl Avatar answered Sep 21 '22 08:09

stevenl