The following script will output a list of duplicated executables found in the directories listed in the $PATH
variable.
It does this by creating a tab-delimited two-column list of executables, where the first column is the directory pathname and the second is the filename of an executable.
This list is read by a simple awk
program that counts how many times each executable is found and that collects all directory pathnames for each of them in a tab-delimited string.
At the end, the awk
code outputs each executable whose exact name was found more than once, together with the tab-delimited list of directory pathnames where it was found. The directories will be listed in the order they were searched, i.e. an executable would be picked from the first directory listed if used without an explicit path on the command line (given the current value of $PATH
).
#!/bin/sh
turn off filename globbing, and
split strings in unquoted variables on colons
set -f; IFS=:
set the positional parameters to the list of
directories in the PATH variable
set -- $PATH
turn on filename globbing again
(leave IFS as is, it won't affect anything else here)
set +f
loop over those directories and do the things
for dir do
for pathname in "$dir"/; do
[ -x "$pathname" ] && printf '%s\t%s\n' "$dir" "${pathname##/}"
done
done |
awk -F '\t' '
{
dir[$2] = ( dir[$2] == "" ? $1 : dir[$2] "\t" $1 )
count[$2]++
}
END {
for (util in dir)
if (count[util] > 1)
printf "%s\t%s\n", util, dir[util]
}'
Example run on my OpenBSD system, passing the output through column -t
to format it nicely, and sorting it:
$ sh script | column -t | sort
banner /usr/bin /usr/games
bash /home/myself/local/bin /usr/local/bin
bashbug /home/myself/local/bin /usr/local/bin
chgrp /bin /usr/sbin
chown /usr/sbin /sbin
clang /usr/bin /usr/local/bin
clang++ /usr/bin /usr/local/bin
clang-cpp /usr/bin /usr/local/bin
ld.lld /usr/bin /usr/local/bin
llvm-config /usr/bin /usr/local/bin
python3 /home/myself/local/bin /usr/local/bin
python3.8 /home/myself/local/bin /usr/local/bin
sysctl /usr/sbin /sbin
zsh /home/myself/local/bin /usr/local/bin
The output tells me that if I start bash
on the command line, it will be picked up from /home/myself/local/bin
, and that I have another bash
executable in /usr/local/bin
.
whereis youtube-dl
orwhich youtube-dl
might get you started – Panki Jan 04 '21 at 15:18whereis
did work for me for this, amazing. Thanks @Panki – ー PupSoZeyDe ー Jan 04 '21 at 15:52