summaryrefslogtreecommitdiff
path: root/dot/local/bin/latest
blob: 12a0a9fc851a4e0bc5a20cf7898e1f31ab586884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/sh
usage()
{
    cat <<EOF
Usage: $0 [directory] [...]
Usage: $0 --help

Searches the current directory if none is specified.

Print one filename: the single newest ordinary file from all (not each)
of the specified directories.

If there are no ordinary files, there is no output.

Symbolic links are not listed.

EOF
}

case $# in
    0) set -- . ;;
    1) if [ "$1" = --usage ]
       then
           usage
           exit 0
       fi
       ;;
esac

drop_first_word()
{
    sed -ne 's/^[^ ]* //p'
}

extract_newest()
{
    sort -z -n -k1,1 -r | head -z -n 1 | xargs -r -0 printf '%s\n'
}

find_simple_files()
{
    pattern="%C@ %h/%f\0"
    find "$@" -maxdepth 1 -name '.*' -o \( -type f -printf "$pattern" \)
}

find_simple_files "$@" | extract_newest | drop_first_word