#!/usr/bin/env bash # description: Find files by name/path pattern, sorted by modification time (newest first) # Args: [path] pattern="$1" search_path="${2:-$PWD}" [ -z "$pattern" ] && echo "usage: file_glob [path]" && exit 1 if [ ! -d "$search_path" ]; then echo "error: path is not a directory: $search_path" exit 1 fi # collect matches and sort by mtime descending rg_out=$(rg --no-config --files -g "$pattern" "$search_path" 2>&1) rg_exit=$? if [ $rg_exit -gt 1 ]; then echo "error: $rg_out" exit 1 fi if [ -z "$rg_out" ]; then echo "no matches" exit 0 fi echo "$rg_out" \ | xargs -d '\n' stat --printf '%Y\t%n\n' 2>&1 \ | sort -rn \ | cut -f2-