summaryrefslogtreecommitdiff
path: root/dot/local/bin/latest
diff options
context:
space:
mode:
Diffstat (limited to 'dot/local/bin/latest')
-rwxr-xr-xdot/local/bin/latest36
1 files changed, 29 insertions, 7 deletions
diff --git a/dot/local/bin/latest b/dot/local/bin/latest
index ef95cfe..12a0a9f 100755
--- a/dot/local/bin/latest
+++ b/dot/local/bin/latest
@@ -3,23 +3,45 @@ usage()
3{ 3{
4 cat <<EOF 4 cat <<EOF
5Usage: $0 [directory] [...] 5Usage: $0 [directory] [...]
6Usage: $0 --help
6 7
7Print the filename of the newest file. 8Searches the current directory if none is specified.
9
10Print one filename: the single newest ordinary file from all (not each)
11of the specified directories.
12
13If there are no ordinary files, there is no output.
14
15Symbolic links are not listed.
8 16
9Searches the directories specified on the command line,
10or else the current directory if none specified.
11EOF 17EOF
12} 18}
13 19
14case $# in 20case $# in
15 0) set -- . ;; 21 0) set -- . ;;
16 1) if [ "$1" = --usage ]; then usage; exit 0; fi ;; 22 1) if [ "$1" = --usage ]
23 then
24 usage
25 exit 0
26 fi
27 ;;
17esac 28esac
18 29
19extract() 30drop_first_word()
31{
32 sed -ne 's/^[^ ]* //p'
33}
34
35extract_newest()
36{
37 sort -z -n -k1,1 -r | head -z -n 1 | xargs -r -0 printf '%s\n'
38}
39
40find_simple_files()
20{ 41{
21 sort -z -n -k1,1 -r | head -z -n 1 | xargs -0 printf '%s\n' | perl -pe 's/.*? //' 42 pattern="%C@ %h/%f\0"
43 find "$@" -maxdepth 1 -name '.*' -o \( -type f -printf "$pattern" \)
22} 44}
23 45
24find "$@" -maxdepth 1 -name '.*' -o \( -type f -printf "%C@ %h/%f\0" \) | extract 46find_simple_files "$@" | extract_newest | drop_first_word
25 47