#!/usr/bin/env python3 """My custom version of 'which', that shows all the possibilities in ${PATH}, in order, for each argument. For example: $ kwhich ls bean-report /bin/ls /usr/bin/ls /bin/bean-report /usr/bin/bean-report $ Hey, wait, I get why there might be two copies of bean-report, but why are there two of ls? Are they different? Let's use -v to find out: $ kwhich -v ls bean-report -rwxr-xr-x 1 root root 147176 Oct 23 14:29 /bin/ls -rwxr-xr-x 1 root root 147176 Oct 23 14:29 /usr/bin/ls -rwxr-xr-x 1 root root 972 Nov 18 2020 /bin/bean-report -rwxr-xr-x 1 root root 972 Nov 18 2020 /usr/bin/bean-report $ """ import sys import os verbose = False names = sys.argv[1:] if "-v" in names: names.remove("-v") verbose = True for name in names: for d in os.environ.get("PATH").split(":"): full_path = os.path.join(d, name) if os.path.isfile(full_path) and os.access(full_path, os.X_OK): if verbose: os.system(f"ls -l {full_path}") else: print(full_path)