Open directory and sort files by date

I need to open directories and sort files by the time they are created. I can find some discussion using tags for Perl, sorting and files, when sorting files based on date modified. I guess this is a more common need than sorting by creation date. I am using Perl. There are several previous posts about sorting by creation date in 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... 

CPAN has a page in the sort command, but it’s not very accessible to me, and I don’t find the words “date” or “create” on the page.

In response to editing, I have to say that I am using Mac, OS 10.7. I know that Finder has the option of sorting by creation date, so there should be some indication of the creation date, somehow attached to the files in this system.

In response to the answer, here is another version of the script that tries 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"; } 
+6
source share
4 answers

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

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

Thus, the default sorting of sort @numbers equivalent to sort {$a <=> $b} @numbers .

If sorted by creation time, you can use the stat function to get this file information. It returns an array of file information, some of which may not apply to your platform. The time of the last file modification is usually safe, but there is no time to create. ctime (the 11th value it returns) is as close as you can get (it represents the time the inode changed to * nix, the creation time to win32), which is expressed as the number of seconds from the moment, which is convenient because it means that you can do simple digital sorting.

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

I'm not sure if you want to filter directories as well. If so, you probably also want to use grep .

+10
source

OS X stores the creation date in metadata for Mac, so the standard Perl file system features are not aware of this. You can use MacOSX :: File to access this information.

+2
source

I need to open directories and sort the files by the time they were created .

You can not. Creation time simply does not exist. There are three time elements tracked * nix as operating systems:

  • mtime . This is the time the file was last modified.
  • atime : this is the time the file was last accessed.
  • ctime . This is the time the last inode change.

On Unix, certain file information is stored in the inode. This includes various things that you see when you take a perl stat file. This is the username, file size, its device, the number of links and, paradoxically, the timestamps mtime, atime and ctime.

Why is there no time to create? Because how would you define it? What if i move the file? If there is a new creation time (by the way, ctime will not change with the move). What if I copy a file? Should a new copy have a new creation time? What if I made a copy and then deleted the original? What if I edited the file? How about if I changed everything in the file with my editing? Or did I edit the file and then rename it to a completely new name?

Even on Windows, which has a file creation time, file creation is not actually tracked. It just keeps track of when a directory entry was created, which is something like ctime . And you can even change this creation time through the Windows API. I suspect that the Mac file creation time is a relic of the HFS file system and does not actually indicate the time the file was created as much as the time the directory entry was created.


As others have pointed out. You can add a block of code to the sorting procedure that says how you want something to be sorted. Here is a quick example. Note. I use File :: stat , which gives me a nice interface named old stat . If I used the old stat command, I would get an array, and then have to figure out which array the item I want is in. Here, the stat command gives me a stat object, and I can use the mtime , atime or ctime to extend the desired time.

I also use <=> , which is a comparison operator specially created for the sort command block.

The sort command gives you two elements $a and $b . You use these two elements to figure out what you want, and then use either <=> or cmp to tell if $a larger, $b larger, or both are the same size.

 #! /usr/bin/env perl use 5.12.0; use warnings; use File::stat; my $dir_name = shift; if ( not defined $dir_name ) { die qq(Usage: $0 <directory>); } opendir(my $dir_fh, $dir_name); my @file_list; while ( my $file = readdir $dir_fh) { if ( $file !~ /^\./ ) { push @file_list, "$dir_name/$file" } } closedir $dir_fh; say scalar @file_list; for my $file (sort { my $a_stat = stat($a); my $b_stat = stat($b); $a_stat->ctime <=> $b_stat->ctime; } @file_list ) { say "$file"; } 
+2
source
 #!/usr/bin/env perl use strict; use warnings; opendir(DIR, $ARGV[0]); chdir($ARGV[0]); my @files = sort { (stat($a))[10] <=> (stat($b))[10] } (readdir(DIR)); closedir(DIR); print join("\n",@files); 

stat provides you with all kinds of file status information. field 10, which is ctime (on file systems that support it), which is the inode change time (and not the creation time).

0
source

All Articles