macOS Terminal sucht die aktuellste Datei im Verzeichnis

1162
tjt263

Ich schreibe (was ich dachte) eine schnelle kleine bashFunktion, um das letzte Foto zu identifizieren, das Photos.appvon einer DSLR in den Apple-Standard importiert wurde .

Bisher sieht es ungefähr so ​​aus:

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

Ziemlich einfach. Es listet die gesamte Bibliothek der importierten Fotos auf. Mein erstes Gefühl war einfach | tail -n 1, aber die Dateien sind nicht in einer erkennbaren Reihenfolge aufgeführt.

Ich habe darüber nachgedacht | sort -V, aber es ist nicht ungewöhnlich, dass die Dateinamen (dh IMG_0123.CR2) außer Betrieb geraten.

Zu Ihrer Information: Diese BSD-Version von findunterstützt die -printfin der GNU-Version gefundene Option nicht .

0
Der Auflistungsbefehl lautet `ls -lt dir | head -n 1` liefert die erste Datei, aber ich kenne das Dateiformat nicht, dh * .jpg, das Fotos verwenden. Daher kann es nicht expliziter sein Mark vor 6 Jahren 0

2 Antworten auf die Frage

1
David Lindegren

Ich fand diese Antwort auf eine ähnliche Frage, Order Find-results nach Änderungsdatum .

In Ihrem Fall sollte es ungefähr so ​​sein:

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

Weitere Informationen zur Interaktion finden Sie im obigen Link.

0
jimtut

Speichern Sie dieses Perl-Skript irgendwo in Ihrer Suche $ PATH, chmodum es ausführbar zu machen, und führen Sie es einfach aus. Sie werden aufgefordert, einige Optionen anzugeben, einschließlich der Sortierung nach Name oder Datum. Es ist ein ALT-Skript, das ich vor Jahren geschrieben habe. es funktioniert!

#!/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"; <>; }