Skip to content

Instantly share code, notes, and snippets.

@skorotkiewicz
Created July 18, 2026 17:09
Show Gist options
  • Select an option

  • Save skorotkiewicz/1a98d48ba7514c11d2d6166590d2e022 to your computer and use it in GitHub Desktop.

Select an option

Save skorotkiewicz/1a98d48ba7514c11d2d6166590d2e022 to your computer and use it in GitHub Desktop.
Copy or move the newest file in zsh

Copy or move the newest file without checking its name

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: built in already

Zsh glob qualifiers can sort matches by modification time and select the newest one.

Newest regular file

cp -- ~/Downloads/*(.Dom[1]) .

Newest directory

mv -- ~/Downloads/*(/Dom[1]) .

Newest item of any type

cp -r -- ~/Downloads/*(Dom[1]) .

Meaning of the qualifiers

  • . — regular files only
  • / — directories only
  • D — include hidden entries
  • om — 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.


Generic Bash command: latest

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 -- file

Equivalent to:

file "/path/to/the/newest/file"

Script

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

install -Dm755 latest ~/.local/bin/latest

Make sure ~/.local/bin is in PATH:

export PATH="$HOME/.local/bin:$PATH"

Design notes

  • 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.

Tiny aliases, when Zsh is enough

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]) .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment