$ PATH에서 모든 바이너리를 나열하십시오. 파일을 나열하는

bash에서 $ PATH의 모든 실행 파일을 나열하는 하나의 라이너가 있습니까?



답변

이것은 대답이 아니지만 실행할 수있는 명령 인 바이너리를 보여줍니다.

compgen -c

(가정 bash )

다른 유용한 명령

compgen -a # will list all the aliases you could run.
compgen -b # will list all the built-ins you could run.
compgen -k # will list all the keywords you could run.
compgen -A function # will list all the functions you could run.
compgen -A function -abck # will list all the above in one go.


답변

zsh로 :

whence -pm '*'

또는:

print -rl -- $commands

(의 둘 이상의 구성 요소에 나타나는 명령의 $PATH경우 첫 번째 명령 만 나열합니다).

전체 경로가없는 명령을 원하고 적절한 측정을 위해 정렬 한 경우 :

print -rl -- ${(ko)commands}

즉, 값 대신 해당 연관 배열의 키를 가져옵니다.


답변

POSIX 셸 에서 최종 정렬을 제외하고 외부 명령을 사용하지 않고 (로 printf돌아 오지 않으면 내장되어 있다고 가정 echo) 실행 파일 이름에 개행 문자가 없다고 가정합니다.

{ set -f; IFS=:; for d in $PATH; do set +f; [ -n "$d" ] || d=.; for f in "$d"/.[!.]* "$d"/..?* "$d"/*; do [ -f "$f" ] && [ -x "$f" ] && printf '%s\n' "${x##*/}"; done; done; } | sort

빈 구성 요소가없고 $PATH( .대신 사용)으로 시작하는 구성 요소 나 PATH 구성 요소 또는 실행 파일 이름에 -와일드 카드 문자 \[?*가없고으로 시작하는 실행 파일이없는 경우 다음과 같이 .단순화 할 수 있습니다.

{ IFS=:; for d in $PATH; do for f in $d/*; do [ -f $f ] && [ -x $f ] && echo ${x##*/}; done; done; } | sort

POSIX findsed:

{ IFS=:; set -f; find -H $PATH -prune -type f -perm -100 -print; } | sed 's!.*/!!' | sort

경로에 희귀 비 실행 파일 또는 비정규 파일을 나열하려는 경우 훨씬 간단한 방법이 있습니다.

{ IFS=:; ls -H $PATH; } | sort

이것은 도트 파일을 건너 뜁니다. 필요한 경우 -A플래그가 ls있거나 POSIX를 고수하려는 경우 플래그를 추가하십시오 .ls -aH $PATH | grep -Fxv -e . -e ..

bashzsh 에는 더 간단한 솔루션이 있습니다 .


답변

나는 이것을 생각해 냈다.

IFS=':';for i in $PATH; do test -d "$i" && find "$i" -maxdepth 1 -executable -type f -exec basename {} \;; done

편집 : 이것은 아파치 사용자가 bin 디렉토리의 일부 파일을 읽는 동안 SELinux 경고를 트리거하지 않는 유일한 명령 인 것 같습니다.


답변

이건 어때요

find ${PATH//:/ } -maxdepth 1 -executable

문자열 대체는 Bash와 함께 사용됩니다.


답변

쉘에서 파이썬을 실행할 수 있다면 다음과 같은 (리우스하게 긴) 한 줄짜리 라이너도 사용할 수 있습니다.

python -c 'import os;import sys;output = lambda(x) : sys.stdout.write(x + "\n"); paths = os.environ["PATH"].split(":") ; listdir = lambda(p) : os.listdir(p) if os.path.isdir(p) else [ ] ; isfile = lambda(x) : True if os.path.isfile(os.path.join(x[0],x[1])) else False ; isexe = lambda(x) : True if os.access(os.path.join(x[0],x[1]), os.X_OK) else False ; map(output,[ os.path.join(p,f) for p in paths for f in listdir(p) if isfile((p,f)) and isexe((p,f)) ])'

이것은 ‘exec’함수를 사용하지 않고 한 줄의 파이썬 코드를 사용하여 수행 할 수 있는지 여부를 알기위한 재미있는 연습이었습니다. 좀 더 읽기 쉬운 형태로, 주석은 다음과 같습니다.

import os
import sys

# This is just to have a function to output something on the screen.
# I'm using python 2.7 in which 'print' is not a function and cannot
# be used in the 'map' function.
output = lambda(x) : sys.stdout.write(x + "\n")

# Get a list of the components in the PATH environment variable. Will
# abort the program is PATH doesn't exist
paths = os.environ["PATH"].split(":")

# os.listdir raises an error is something is not a path so I'm creating
# a small function that only executes it if 'p' is a directory
listdir = lambda(p) : os.listdir(p) if os.path.isdir(p) else [ ]

# Checks if the path specified by x[0] and x[1] is a file
isfile = lambda(x) : True if os.path.isfile(os.path.join(x[0],x[1])) else False

# Checks if the path specified by x[0] and x[1] has the executable flag set
isexe = lambda(x) : True if os.access(os.path.join(x[0],x[1]), os.X_OK) else False

# Here, I'm using a list comprehension to build a list of all executable files
# in the PATH, and abusing the map function to write every name in the resulting
# list to the screen.
map(output, [ os.path.join(p,f) for p in paths for f in listdir(p) if isfile((p,f)) and isexe((p,f)) ])


답변

#!/usr/bin/env python
import os
from os.path import expanduser, isdir, join, pathsep

def list_executables():
    paths = os.environ["PATH"].split(pathsep)
    executables = []
    for path in filter(isdir, paths):
        for file_ in os.listdir(path):
            if os.access(join(path, file_), os.X_OK):
                executables.append(file_)
    return executables