macOS 终端在目录中找到最新的文件

macOS 终端在目录中找到最新的文件

我正在编写(我认为是)一个快速的小功能,用于识别从 DSLRbash导入到 Apple 标准中的最新照片。Photos.app

到目前为止它看起来像这样:

find ~/Pictures/Photos\ Library.photoslibrary/Masters -type f

非常简单。它列出了导入照片的整个库。我的第一反应是简单地按| tail -n 1,但文件没有按任何可辨别的顺序列出。

我考虑过,但文件名(即)乱序的情况| sort -V并不罕见。IMG_0123.CR2

仅供参考:此 BSD 版本find不支持-printfGNU 版本中的选项。

答案1

我找到了类似问题的答案,按修改日期排序查找结果

在你的情况下它应该是这样的:

find ~/Pictures/Photos\ Library.photoslibrary/Masters -type f -print0 | xargs -0 ls -tl

在上面链接中可以找到更多关于其如何交互的信息。

答案2

将此 Perl 脚本保存在搜索 $PATH 中的某个位置,chmod使其可执行,然后运行它。它会提示您几个选项,包括按名称或日期排序。这是我几年前编写的一个旧脚本,不要把它扔得太远;它确实有效!

#!/usr/bin/perl -w

# This script finds files or folders that have been modified "recently"
# (as determined by the timeframe specified in the arguments).
# Just run the script to get the full usage instructions.

use strict;
use File::Find;
use Config;

my $time;
my $units;
my $dir;
my $order = "time";
my @validUnits = qw(sec min hours days);
my @results;
my %results;

if ($#ARGV < 1) {
  print "\nUSAGE: $0 [-f] <time> <units> [dir]\n\n";
  print "Search for recently modified files or folders located\nin (or under) dir (which is \".\" if not specified).\n";
  print "\nResults are sorted by timestamp, in ascending order, unless the '-f' option is used\n";
  print "to specify sorting by filename.\n";
  print "\nPlease enter the time (just a number): ";
  $time = <>;
  chomp $time;
  print "Please enter the units (@validUnits): ";
  $units = <>;
  chomp $units;
  print "Please enter the directory: [.] ";
  $dir = <>;
  chomp $dir;
  if ($dir eq "") { $dir = "." }
  print "Sort order (file or time): [time] ";
  $order = <>;
  chomp $order;
  if ($order eq "") { $order = "time" }
  print "\n";
}
else {
  if ($ARGV[0] eq "-f") {
    $order = "filename";
    shift;
  }
  $time = shift;
  $units = shift;
  if ($#ARGV > -1) {
    $dir = shift;
  }
  else {
    $dir = ".";
  }
}

# Check the time entered.
if (!($time) || !($time =~ m/^[0-9]*$/)) {
  print "You must enter an integer for the length of time.\n";
  exit 1;
}

# Check the units entered.
my $validUnits = grep m/$units/, @validUnits;
if (!($units) || !($validUnits)) {
  print "You must use one of the valid units: @validUnits\n";
  exit 1;
}

if ("min" eq $units) { $time *= 60 }
if ("hours" =~ m/$units/) { $time *= 60 * 60 }
if ("days" =~ m/$units/) { $time *= 60 * 60 * 24 }
my $now = time();
my $then = $now - $time;

find
  (
     sub {
       # If you can't get the mtime for a file, it's probably a dead link.
       # Set $mtime back to "0", to remove this dead link from consideration.
       # NOTE: all links are resolved, so $mtime reflects the mtime of the
       # link target, not the link itself.
       my $mtime = (stat)[9] || "0";
       if ($mtime > $then) {
     # If the $mtime is more recent than the target ($then),
     # add the mtime to the %results hash,
     # keyed by filename (w/ relative path)
     $results{$File::Find::name} = $mtime;
       }
     }
     , $dir
  );

# Get all the keys (filenames) of the %results hash.
# If we're sorting by "time", re-sort @results by the timestamp.
@results = sort keys %results;
if ($order eq "time") {
  @results = sort { $results{$a} <=> $results{$b} } @results;
}

foreach my $key (@results) {
  # If we're sorting by "time", print the time first, followed by filename.
  # Else, print filename first, followed by time.
  if ($order eq "time") {
    print localtime($results{$key}) . ": $key\n";
  }
  else {
    print "$key: " . localtime($results{$key}) . "\n";
  }
}

if ($Config{'osname'} eq "MSWin32") {
  print "\nPress RETURN to exit.\n";
  <>;
}

相关内容