A tiny reference for the moment when I download or generate something and want to use the most recently modified file or directory without first looking up its exact name.
Zsh glob qualifiers can sort matches by modification time and select the newest one.
cp -- ~/Downloads/*(.Dom[1]) .mv -- ~/Downloads/*(/Dom[1]) .cp -r -- ~/Downloads/*(Dom[1]) ..— regular files only/— directories onlyD— include hidden entriesom— order by modification time, newest first[1]— take the first result
A few more useful examples:
# Open the newest downloaded file
xdg-open ~/Downloads/*(.Dom[1])
# Show information about it
file ~/Downloads/*(.Dom[1])
# Move it into the current directory
mv -- ~/Downloads/*(.Dom[1]) .
# Copy the newest PNG
cp -- ~/Downloads/*.png(.Dom[1]) .
# Newest file in the current directory
printf '%s\n' ./*(.Dom[1])This uses modification time, not necessarily filesystem creation time.
For scripts, Bash, or commands where a reusable selector is nicer than shell-specific glob syntax:
latest [-f|-d] [DIR] [-- COMMAND [ARGS...]]Without a command, it prints the selected path:
latest ~/Downloads
latest -f ~/Downloads
latest -d .With a command, every literal {} is replaced by the selected path:
latest -f ~/Downloads -- cp -- {} .
latest -f ~/Downloads -- mv -- {} ~/archive/
latest ~/Downloads -- xdg-open {}
latest -d . -- tree {}When {} is omitted, the selected path is appended as the final argument:
latest -f ~/Downloads -- fileEquivalent to:
file "/path/to/the/newest/file"Save as latest:
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'TXT'
usage: latest [-f|-d] [DIR] [-- COMMAND [ARGS...]]
Select the most recently modified direct child of DIR (default: .).
-f, --file regular files only
-d, --directory directories only
-h, --help show this help
Without COMMAND, print the selected path.
With COMMAND, replace each literal {} argument with the selected path.
If {} is absent, append the path as the final argument.
examples:
latest ~/Downloads
latest -f ~/Downloads -- cp -- {} .
latest -f ~/Downloads -- mv -- {} ~/archive/
latest ~/Downloads -- xdg-open {}
TXT
}
die() {
printf 'latest: %s\n' "$*" >&2
exit 1
}
kind=''
dir='.'
while (($#)); do
case $1 in
-f|--file)
[[ $kind != d ]] || die 'use either --file or --directory'
kind=f
shift
;;
-d|--directory)
[[ $kind != f ]] || die 'use either --file or --directory'
kind=d
shift
;;
-h|--help)
usage
exit 0
;;
--)
shift
break
;;
-*)
die "unknown option: $1"
;;
*)
dir=$1
shift
if (($#)) && [[ $1 != -- ]]; then
die 'put -- before the command'
fi
if (($#)); then
shift
fi
break
;;
esac
done
[[ -d $dir ]] || die "not a directory: $dir"
predicate=()
case $kind in
f) predicate=(-type f) ;;
d) predicate=(-type d) ;;
esac
record=''
IFS= read -r -d '' record < <(
find "$dir" -mindepth 1 -maxdepth 1 "${predicate[@]}" \
-printf '%T@\t%p\0' |
sort -z -nr
) || true
[[ -n $record ]] || die "nothing found in: $dir"
path=${record#*$'\t'}
if (($# == 0)); then
printf '%s\n' "$path"
exit 0
fi
command=()
replaced=0
for argument in "$@"; do
if [[ $argument == '{}' ]]; then
command+=("$path")
replaced=1
else
command+=("$argument")
fi
done
((replaced)) || command+=("$path")
printf 'latest: %q\n' "$path" >&2
exec "${command[@]}"install -Dm755 latest ~/.local/bin/latestMake sure ~/.local/bin is in PATH:
export PATH="$HOME/.local/bin:$PATH"- Selects only direct children of the chosen directory.
- Includes hidden entries.
- Safely handles spaces, tabs, newlines, and unusual filenames.
- Does not use
eval. - Uses modification time because creation/birth time is not consistently available across filesystems and tools.
- Does not replace or alias
cp,mv, or other standard commands; it composes with them.
alias newest='print -r -- ./*(Dom[1])'
alias newestfile='print -r -- ./*(.Dom[1])'
alias newestdir='print -r -- ./*(/Dom[1])'For Downloads specifically:
alias dlnew='print -r -- ~/Downloads/*(.Dom[1])'Use command substitution when needed:
cp -- "$(dlnew)" .The direct glob form is still shorter:
cp -- ~/Downloads/*(.Dom[1]) .