diff --git a/README.md b/README.md index d71e3d6..5fec9e7 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,8 @@ A bar-style prompt showing current time, execution time of the last command | `meteo` | info | Display weather forecast for the configured or given city | | `myextip` | net | Get information about your public IP address | | `get_pkgmgr` | packages | Detect the active package manager of the running distribution (`apt`, `dnf`, `yum`, `zypper`, `pacman`, `apk`, `portage`, `xbps`, `nix`) | -| `pkgs` | packages | Search for a pattern in installed package names (distro-aware via `get_pkgmgr`, supports `-i`) | +| `pkgf` | packages | Find which installed package owns a given file (distro-aware via `get_pkgmgr`) | +| `pkgs` | packages | Search for a pattern in installed package names (distro-aware via `get_pkgmgr`) | | `ppg` | processes | Look for the given pattern in running processes | | `ppn` | processes | List processes matching an exact command name | | `ppu` | processes | List processes owned by a specific user | @@ -133,7 +134,7 @@ A bar-style prompt showing current time, execution time of the last command | `pwdscore` | pwd | Calculate the strength score of a given password | | `rain` | rain | Console screensaver with falling-rain effect (multiple color themes) | | `rainbow` | rain | Full-screen rainbow screensaver using only background colors, with horizontal color shifting | -| `rmhost` | ssh | Remove host (name and IP) from SSH known_hosts; supports `--all-users` as root | +| `rmhost` | ssh | Remove host(s) (name and IP) from SSH known_hosts; supports `--all-users` as root | | `rmspc` | filefct | Replace spaces in filenames with underscores (or a custom character) | | `setc` | lang | Set locale to standard C (POSIX) | | `setlocale` | lang | Set console locale to any installed locale | diff --git a/profile.d/help.sh b/profile.d/help.sh index df559de..f4c8624 100644 --- a/profile.d/help.sh +++ b/profile.d/help.sh @@ -86,7 +86,8 @@ help() printf "mdcat\t\tRender Markdown files in terminal with colors and code frames\n" printf "meteo\t\tDisplay weather forecast for the configured or given city\n" printf "myextip\t\tGet information about your public IP address\n" - printf "pkgs\t\tSearch for a pattern in installed package names (dpkg/rpm, supports -i)\n" + printf "pkgf\t\tFind which installed package owns a given file (distro-aware)\n" + printf "pkgs\t\tSearch for a pattern in installed package names (distro-aware)\n" printf "ppg\t\tLook for the given pattern in running processes\n" printf "ppn\t\tList processes matching an exact command name\n" printf "ppu\t\tList processes owned by a specific user\n" diff --git a/profile.d/packages.sh b/profile.d/packages.sh index ad0958a..1dfbc4d 100644 --- a/profile.d/packages.sh +++ b/profile.d/packages.sh @@ -194,6 +194,84 @@ export -f pkgs # ------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ +# Find which installed package a file belongs to. +# Usage: pkgf +pkgf() +{ + local PARSED + PARSED=$(getopt -o h --long help -n 'pkgf' -- "$@") + # shellcheck disable=SC2181 + if [[ $? -ne 0 ]]; then + disp E "Invalid options, use \"pkgf --help\" to display usage." + return 1 + fi + eval set -- "$PARSED" + + while true; do + case "$1" in + -h|--help) + printf "pkgf: Find which installed package owns a given file.\n\n" + printf "Usage: pkgf [options] \n\n" + printf "Options:\n" + printf "\t-h, --help\tDisplay this help screen\n" + return 0 + ;; + --) + shift + break + ;; + *) + disp E "Invalid option: $1" + return 1 + ;; + esac + done + + local file="$1" + [[ -z "$file" ]] && { + disp E "Please specify a file path." + return 1 + } + + local pkgmgr + pkgmgr=$(get_pkgmgr) || { + disp E "No usable package manager could be detected on this system." + return 2 + } + + case "$pkgmgr" in + apt) + dpkg -S "$file" + ;; + dnf|yum|zypper) + rpm -qf "$file" + ;; + pacman) + pacman -Qo "$file" + ;; + apk) + apk info --who-owns "$file" + ;; + portage) + qfile "$file" + ;; + xbps) + xbps-query -o "$file" + ;; + nix) + nix-locate "$file" + ;; + *) + disp E "Package manager '$pkgmgr' is not supported by pkgf." + return 2 + ;; + esac +} +export -f pkgf +# ------------------------------------------------------------------------------ + + load_conf "packages" # EOF