#!/bin/sh # The MIT License (MIT) # # Copyright (c) 2026 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # files [options...] [folders/files...] # # # Show all files in all the folders given, digging recursively into any/all # subfolders. When not given any name, the current folder is used by default. # # All filenames given are given back, which allows picking files using the # so-called `glob` patterns, which can be mixed with folder names. # # Output is lines of absolute filepaths, one per line, without repetitions. # # The options are, available both in single and double-dash versions # # -e, -empty look only for empty files # -h, -help show this help message # -t, -top limit search to top-level files, ignoring subfolders case "$1" in -h|--h|-help|--help) if [ ! -e "$1" ]; then awk '/^# +files /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 fi ;; esac empty='' max_depth='' for arg in "$@"; do if [ "${arg}" = '--' ]; then shift break fi case "${arg}" in -e|--e|-empty|--empty) empty='-empty' shift continue ;; -t|--t|-top|--top) max_depth='-maxdepth 1' shift continue ;; esac break done awk_command='awk' find_command='find' if [ -e /usr/bin/stdbuf ]; then if [ -p /dev/stdout ] || [ -t 1 ]; then awk_command='stdbuf -oL awk' fi find_command='stdbuf -oL find' fi options="${max_depth} -type f ${empty}" for arg in "${@:-.}"; do if [ -f "${arg}" ]; then printf "%s\n" "$(realpath "${arg}")" continue fi if [ -d "${arg}" ]; then if echo "${arg}" | grep -q '/$'; then ${find_command} "${arg}" ${options} -exec realpath {} + else ${find_command} "${arg}/" ${options} -exec realpath {} + fi fi done | ${awk_command} '!c[$0]++; END { exit(NR == 0) }'