Jump to content

Command auto-completion


Guest anon
 Share

Recommended Posts

Written by Tom Berger

What is the shortest way to switch your current working directory from your home directory to the directory '/usr/src/linux/Documentation/isdn/' using the 'cd' ('change directory') command????

 

It's

cd /u<TAB>sr<TAB>l<TAB>/D<TAB>is<TAB>

 

This is called 'automatic command line completion' and it is indispensable. Let's have a closer look at the example:

 

cd /u<TAB>

 

expands to cd /usr/. Easy. Next

 

cd /u<TAB>sr<TAB>

 

expands to cd /usr/src/. If you just enter cd /u<TAB>s<TAB>, you will be presented with a choice of three subdirectories of '/usr' which all match this pattern ('cd /u*/s*'): '/usr/sbin', '/usr/share' and '/usr/src'.

 

So the <TAB> key is a handy means to search directories for files or subdirectories you know the first letters of. For example ls /usr/bin/zip<TAB> gives you a list of all files and subdirectories in '/usr/bin' that start with the letters 'zip'. Of course there are much more powerful commands for this tasks, but it does the trick when you're in a hurry.

Furthermore it comes in handy with really long filenames. Say you want to install an RPM called 'boomshakalakwhizbang-4.6.4.5-mdk586.rpm'. You type rpm -i boom<TAB> and if there are no other files in this directory that match this pattern, the shell will fill in the rest by itself.

 

cd /u<TAB>sr<TAB>l<TAB>

 

expands to cd /usr/src/linux and waits for a decision. There are two directories in '/usr/src' that match: '/usr/src/linux-[...]' and '/usr/src/linux'. How do you tell the shell you want the latter? Append a slash ('/'), thus indicating the end of this last name.

Presume you are not sure if it was '/usr/src/linux/Documentation' or '/usr/src/linux/documentation'. As you may know, Linux discriminates between upper and lower case. If you have read carefully until here, you know the answer already:

 

cd /u<TAB>sr<TAB>l<TAB>/d<TAB>

 

expands to '/usr/src/linux/drivers/'. Looks like it was 'Documentation' (with a capital 'D') then.

 

This kind of completion works for commands, too:

 

[tom@belbo tom]$ gre<TAB>

grecord grefer grep

[tom@belbo tom]$ gre

 

Here the shell presents me with a list of all the commands it knows about which start with the string 'gre'.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...