Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

On the command line, I can redirect or pipe output of a command to a file or another command using the > or | operator after the command. I have come across a less standard situation that I would like to redirect the output to a file, but I don't seem to have the oppurtunity to redirect it:

When at a new terminal,

[chiliNUT ~]$

if I press Tab without typing anything first, I get asked

display all 1725 possibilities? (y or n)

and if I then type y, I get a nice long list of different commands. How can I redirect or pipe this output to a file? I don't seem to get the chance to type > myfile.txt anywhere.

Using CentOS release 6.4 (Final).

share|improve this question

1 Answer 1

up vote 23 down vote accepted

You could make use of the builtin compgen:

compgen: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]

Display possible completions depending on the options.

Intended to be used from within a shell function generating possible
completions.  If the optional WORD argument is supplied, matches against
WORD are generated.

Exit Status:
Returns success unless an invalid option is supplied or an error occurs.

TAB at the prompt would list commands, shell builtins, keywords, aliases and functions. So you could say:

compgen -cbka -A function | grep '^y' > myfile.txt

to get all the options that you see upon typing yTAB at the shell prompt into the file myfile.txt.

Eliminate the grep pipeline in order to get all the possible commands, functions, ... into the file:

compgen -cbka -A function > myfile.txt
share|improve this answer
1  
Awesome! You're answer as written actually only grabs the commands / files that start with the letter y, so just compgen -cbka -A function > myfile.txt grabs the full output. Thanks! I (obviously) never knew about this new interesting function. –  chiliNUT May 2 at 2:49
    
Oh, that's clever. –  Hauke Laging May 2 at 2:49
    
@chiliNUT I did mention that the command would get those starting with y :) This was pretty much in line with what you showed in the question! –  devnull May 2 at 2:51
    
Oh, ha, misunderstood a little. The 'y' in my question actually corresponds to 'yes' vs 'no'; I enter 'y' and it spits out all those commands, I enter 'n' and it doesn't output anything. –  chiliNUT May 2 at 2:53
2  
@chiliNUT I've updated the answer to reflect that as well. –  devnull May 2 at 2:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.