Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

is there a better way to complete only executables in a given dir #748

Answered by akinomyoga
calestyo asked this question in Q&A
Discussion options

Hey.

I want to complete any executables (regular files or symlinks pointing to such) in a given directory (but not in it's subdirectories).
Ideally with no dependencies (e.g. find) which are not guaranteed to be there

My current approach is:

{ PATH="${someDir}" compgen -c; compgen -abk -A function; } | sort | uniq -u

which first takes the output of -c and then filters out any aliases, built-ins, keywords and functions.

But that's of course also a bit ugly... and requires two additional processes.

Anything better?

Thanks,
Chris.

You must be logged in to vote

This question is unrelated to bash-completion. This kind of question should be asked on the help-bash list.

I don't think there is a single command to obtain only the executable files. The suggested line can be rewritten in a more natural way as follows, but further improvements depend on the use case.

PATH=$PWD compgen -c | grep -vxF "$(compgen -abk -A function)"

Or rather than relying on hacks, files can be simply tested as

# Just for illustration of the idea, *not tested*
result=()
for file in "$someDir"/*; do
 if [[ -x $file ]]; then
 result+=("${file##*/}")
 fi
done

This is plain, clean, and flexible.

Replies: 2 comments

Comment options

This question is unrelated to bash-completion. This kind of question should be asked on the help-bash list.

I don't think there is a single command to obtain only the executable files. The suggested line can be rewritten in a more natural way as follows, but further improvements depend on the use case.

PATH=$PWD compgen -c | grep -vxF "$(compgen -abk -A function)"

Or rather than relying on hacks, files can be simply tested as

# Just for illustration of the idea, *not tested*
result=()
for file in "$someDir"/*; do
 if [[ -x $file ]]; then
 result+=("${file##*/}")
 fi
done

This is plain, clean, and flexible.

You must be logged in to vote
0 replies
Answer selected by calestyo
Comment options

This question is unrelated to bash-completion. This kind of question should be asked on the help-bash list.

Well bash itself obviously has no direct way... and like for so many other things, bash-completion might have just provided some helper function or similar that I couldn't find.

PATH=$PWD compgen -c | grep -vxF "$(compgen -abk -A function)"

Guess I'll need to check which of the two is faster. But probably that one.

Or rather than relying on hacks, files can be simply tested as

Sure, but that was noticeable slower in my tests.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /