Created
December 4, 2025 16:26
-
-
Save Aetherinox/a8e7f47588ab3f798adb52fb9fc8fe2e to your computer and use it in GitHub Desktop.
Bash alias for searching / listing files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # # | |
| # define > colors | |
| # | |
| # Use the color table at: | |
| # - https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 | |
| # # | |
| declare -A c=( | |
| [end]=$'\e[0m' | |
| [white]=$'\e[97m' | |
| [bold]=$'\e[1m' | |
| [dim]=$'\e[2m' | |
| [underline]=$'\e[4m' | |
| [strike]=$'\e[9m' | |
| [blink]=$'\e[5m' | |
| [inverted]=$'\e[7m' | |
| [hidden]=$'\e[8m' | |
| [black]=$'\e[38;5;0m' | |
| [fuchsia1]=$'\e[38;5;205m' | |
| [fuchsia2]=$'\e[38;5;198m' | |
| [red]=$'\e[38;5;160m' | |
| [red2]=$'\e[38;5;196m' | |
| [orange]=$'\e[38;5;202m' | |
| [orange2]=$'\e[38;5;208m' | |
| [magenta]=$'\e[38;5;5m' | |
| [blue]=$'\e[38;5;033m' | |
| [blue2]=$'\e[38;5;033m' | |
| [blue3]=$'\e[38;5;68m' | |
| [cyan]=$'\e[38;5;51m' | |
| [green]=$'\e[38;5;2m' | |
| [green2]=$'\e[38;5;76m' | |
| [yellow]=$'\e[38;5;184m' | |
| [yellow2]=$'\e[38;5;190m' | |
| [yellow3]=$'\e[38;5;193m' | |
| [grey1]=$'\e[38;5;240m' | |
| [grey2]=$'\e[38;5;244m' | |
| [grey3]=$'\e[38;5;250m' | |
| [navy]=$'\e[38;5;62m' | |
| [olive]=$'\e[38;5;144m' | |
| [peach]=$'\e[38;5;210m' | |
| ) | |
| # # | |
| # unicode for emojis | |
| # https://apps.timwhitlock.info/emoji/tables/unicode | |
| # # | |
| declare -A icon=( | |
| ["symbolic link"]=$'\xF0\x9F\x94\x97' # 🔗 | |
| ["regular file"]=$'\xF0\x9F\x93\x84' # 📄 | |
| ["directory"]=$'\xF0\x9F\x93\x81' # 📁 | |
| ["regular empty file"]=$'\xe2\xad\x95' # ⭕ | |
| ["log"]=$'\xF0\x9F\x93\x9C' # 📜 | |
| ["1"]=$'\xF0\x9F\x93\x9C' # 📜 | |
| ["2"]=$'\xF0\x9F\x93\x9C' # 📜 | |
| ["3"]=$'\xF0\x9F\x93\x9C' # 📜 | |
| ["4"]=$'\xF0\x9F\x93\x9C' # 📜 | |
| ["5"]=$'\xF0\x9F\x93\x9C' # 📜 | |
| ["pem"]=$'\xF0\x9F\x94\x92' # 🔑 | |
| ["pub"]=$'\xF0\x9F\x94\x91' # 🔒 | |
| ["pfx"]=$'\xF0\x9F\x94\x92' # 🔑 | |
| ["p12"]=$'\xF0\x9F\x94\x92' # 🔑 | |
| ["key"]=$'\xF0\x9F\x94\x91' # 🔒 | |
| ["crt"]=$'\xF0\x9F\xAA\xAA ' # 🪪 | |
| ["gz"]=$'\xF0\x9F\x93\xA6' # 📦 | |
| ["zip"]=$'\xF0\x9F\x93\xA6' # 📦 | |
| ["gzip"]=$'\xF0\x9F\x93\xA6' # 📦 | |
| ["deb"]=$'\xF0\x9F\x93\xA6' # 📦 | |
| ["sh"]=$'\xF0\x9F\x97\x94' # 🗔 | |
| ) | |
| # # | |
| # Permissions | |
| # | |
| # lists all of the files in a folder and their info | |
| # or lists information about a specific file. | |
| # | |
| # @alias p, l, perms | |
| # @usage l <filename:optional) | |
| # l shows files and folders | |
| # l -f, --files shows only files | |
| # l -Fm --folders shows only folders | |
| # l -a, --all shows hidden and non-hidden files together | |
| # l -s, --search search for files | |
| # | |
| # $# shows argument count or files found | |
| # $@ list of files found | |
| # # | |
| alias {l,@l,:l}="perms" | |
| perms() | |
| { | |
| cmd_app="List" | |
| cmd_title="List Files & Folders" | |
| cmd_about="This command allows you to list folders or files on a server" | |
| cmd_alias="l,p,ls" | |
| cmd_build="12-04-2025" | |
| local arg_all=false | |
| local arg_date=false | |
| local limitFiles=false | |
| local limitFolders=false | |
| local argSearchTerm= | |
| while [ $# -gt 0 ]; do | |
| case "$1" in | |
| -a|-A|--all) | |
| arg_all=true | |
| ;; | |
| -H|--hide|--excludeHidden) | |
| arg_all=false | |
| ;; | |
| -d|--date) | |
| arg_date=true | |
| ;; | |
| -f|--files) | |
| limitFiles=true | |
| ;; | |
| -F|--folders) | |
| limitFolders=true | |
| ;; | |
| -s|--search|--find|--locate) | |
| if [[ "$1" != *=* ]]; then shift; fi | |
| argSearchTerm="${1#*=}" | |
| ;; | |
| -h|--help) | |
| echo -e | |
| printf " ${c[blue]}${cmd_title}${c[end]}\n" 1>&2 | |
| printf " ${c[grey2]}${cmd_about}${c[end]}\n" 1>&2 | |
| printf " ${c[grey1]}last update:${c[end]} ${c[grey1]}$cmd_build${c[end]}\n" 1>&2 | |
| printf " ${c[fuchsia2]}$FUNCNAME${c[end]} [${c[grey2]}-h|--help${c[end]}] | [${c[grey2]}-s ${c[yellow]}arg${c[end]}] [{${c[grey2]}-A|-H${c[end]}}] [${c[grey2]}-d${c[end]}] [{${c[grey2]}-f|-F${c[end]}}]" 1>&2 | |
| echo -e | |
| echo -e | |
| printf ' %-5s %-40s\n' "${c[grey1]}Syntax:${c[end]}" "" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}Command${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} [${c[grey2]}-option [${c[yellow]}arg${c[end]}]${c[end]}]" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}Options${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} [${c[grey2]}-h|--help${c[end]}]" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " " ${c[grey2]}-A${c[end]} " "required" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " " ${c[grey2]}-A...${c[end]} " "required; multiple can be specified" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " " ${c[grey2]}[ -A ]${c[end]} " "optional" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " " ${c[grey2]}[ -A... ]${c[end]} " "optional; multiple can be specified" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " " ${c[grey2]}{ -A | -B }${c[end]} " "one or the other; do not use both" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}Arguments${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} [${c[grey2]}-s${c[yellow]} arg${c[grey2]}|--search ${c[yellow]}arg${c[end]}]${c[yellow]} arg${c[end]}" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}Examples${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]}" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} ${c[grey2]}--search${c[yellow]} arg${c[grey2]}${c[end]}" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} ${c[grey2]}--all${c[yellow]}${c[end]}" 1>&2 | |
| echo -e | |
| printf ' %-5s %-40s\n' "${c[grey1]}Aliases:${c[end]}" "" 1>&2 | |
| printf ' %-5s %-40s\n' " " "$cmd_alias${c[end]}" 1>&2 | |
| echo -e | |
| printf ' %-5s %-40s\n' "${c[grey1]}Options:${c[end]}" "" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " "${c[blue2]}-A${c[grey1]},${c[blue2]} --all${c[end]} " "show all files and folders; including hidden" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " "${c[blue2]}-H${c[grey1]},${c[blue2]} --hide${c[end]} " "exclude hidden files from list" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " "${c[blue2]}-d${c[grey1]},${c[blue2]} --date${c[end]} " "show file or folder modification date" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " "${c[blue2]}-f${c[grey1]},${c[blue2]} --files${c[end]} " "show all files only; hide folders" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " "${c[blue2]}-F${c[grey1]},${c[blue2]} --folders${c[end]} " "show all folders only; hide files" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " "${c[blue2]}-s${c[grey1]},${c[blue2]} --search${c[end]} " "search for file or folder" 1>&2 | |
| printf ' %-5s %-30s %-40s\n' " " "${c[blue2]}-h${c[grey1]},${c[blue2]} --help${c[end]} " "help menu" 1>&2 | |
| echo -e | |
| echo -e | |
| return | |
| ;; | |
| *) | |
| ;; | |
| esac | |
| shift | |
| done | |
| echo -e | |
| if [ "${arg_all}" == "true" ]; then | |
| shopt -s dotglob | |
| else | |
| shopt -u dotglob | |
| fi | |
| # # | |
| # set default | |
| # | |
| # this is so that we don't have to use `perms *` as our command. we can just use `perms` | |
| # to run it. | |
| # # | |
| (( $# )) || set -- * | |
| local statfmt='%A\r%a\r%U\r%G\r%F\r%n\r%u\r%g\r%.19z\0' | |
| local perms mode user group type name uid gid date_modified du=du stat=stat | |
| local sizes=() | |
| # # | |
| # If we search a folder, and the folder is empty, it will return `*`. | |
| # if we get `*`, this means the folder is empty, report it back to the user. | |
| # # | |
| if [[ "$@" == "*" ]]; then | |
| echo -e " ${c[grey1]}Directory empty${c[end]}" | |
| echo -e | |
| return | |
| fi | |
| # only one file / folder passed and does not exist | |
| if [ $# == 1 ] && ( [ ! -f "$@" ] && [ ! -d "$@" ] ); then | |
| echo -e " ${c[end]}No file or folder named ${c[blue2]}$@${c[end]} exists${c[end]}" | |
| echo -e | |
| return | |
| fi | |
| if which gdu ; then | |
| du=gdu | |
| fi | |
| if which gstat ; then | |
| stat=gstat | |
| fi | |
| readarray -td '' sizes < <(sudo ${du} --apparent-size -hs0 "$@") | |
| local i=0 | |
| if [ "${arg_date}" == "true" ]; then | |
| date_modified="Modified" | |
| fi | |
| printf ' %s\r\033[8C %b%-44q%b %-32s %-22s %-25s %-85s %-10s\n' \ | |
| "" \ | |
| "${c[grey1]}" "Filename" "${c[end]}" \ | |
| "${c[grey1]}Binary Oct${c[end]}" \ | |
| "${c[grey1]}Size${c[end]}" \ | |
| "${c[grey1]}UID:GID${c[end]}" \ | |
| "${c[grey1]} User${c[grey1]}:${c[grey1]}Group ${c[grey1]}${c[end]}" \ | |
| "${c[grey1]}$date_modified${c[end]}" | |
| echo "${c[grey1]} ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐${c[end]}" | |
| while IFS=$'\r' read -rd '' perms mode user group type name uid gid date_modified; do | |
| if [ "$limitFolders" = true ] && [[ "$type" != "directory" ]]; then | |
| continue | |
| fi | |
| if [ "$limitFiles" = true ] && [[ "$type" == "directory" ]]; then | |
| continue | |
| fi | |
| if [[ -n "$argSearchTerm" ]] && [[ "$argSearchTerm" != "$name" ]]; then | |
| continue | |
| fi | |
| local ext="${name##*.}" | |
| if [[ -n "${icon[$type]}" ]]; then | |
| type=${icon[$type]} | |
| fi | |
| if [[ -n "${icon[$ext]}" ]]; then | |
| type=${icon[$ext]} | |
| fi | |
| if [ "${arg_date}" == "false" ]; then | |
| date_modified="" | |
| fi | |
| printf ' %s\r\033[8C %b%-44q%b %-32s %-22s %-25s %-85s %-10s\n' \ | |
| " $type" \ | |
| "${c[green]}" "$name" "${c[end]}" \ | |
| "${c[grey2]}$perms $mode${c[end]}" \ | |
| "${c[grey2]}${sizes[i++]%%[[:space:]]*}${c[end]}" \ | |
| "${c[grey1]}${uid}:${gid}${c[end]}" \ | |
| "${c[grey1]} ${c[fuchsia2]}$user${c[grey1]}:${c[fuchsia2]}$group${c[grey1]} ${c[end]}" \ | |
| "${c[grey1]}$date_modified${c[end]}" | |
| done < <(${stat} --printf "$statfmt" "$@") | |
| echo -e | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment