MandrakeUser.Org - Your Mandrake-Linux Knowledge Base!


 
 

* DocIndex - Basics

Using The Shell II

* Auto-Completion
* Commandline History
* Commandline Editing
* Available Shell Shortcuts

Related Resources:

MdkRef, 3.5
man bash

Revision / Modified: Feb. 28, 2002
Author: Tom Berger

 

Unix (and its younger brother Linux) have been born on the command line. Because of this, the command line in Unix features a lot of mechanism to spare you some menial typing. This page introduces some of them.

* Auto-Completion

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'.

* section index * top

* Commandline History

Using the up-arrow key you can scroll through all the shell commands you have issued on that console recently. Using the down-arrow key you can scroll back again. Together with the SHIFT key, you can scroll through previous output on the console. You can also edit 'old' command lines and issue them again.

Pressing <CTRL r> puts the shell into "reverse-i(ncremental)-search" mode. Now type the first letter of the command you are looking for:

(reverse-i-search)`':. Typing 'i' may change this line to:

(reverse-i-search)`i': isdnctrl hangup ippp0

If you now press the <ENTER> key, this command will be executed again. If you press the left or right cursor key or<ESC> instead, you will have this command on a normal command line where you can edit it.

* section index * top

* Commandline Editing

You can navigate and edit the command line with the cursor and the function keys ('Home', 'End' etc), if you like, but there are also keyboard shortcuts for most standard editing tasks:

  • <CTRL k>: delete ('kill') from cursor position to the end of the line
  • <CTRL u>: delete from cursor position to the beginning of the line
  • <ALT d>: delete from cursor position to the end of the current 'word'
  • <CTRL w>: delete from cursor position to the beginning of the current 'word'
  • <CTRL a>: move cursor to the first character of the line
  • <CTRL e>: move cursor beyond the last character of the line
  • <ALT a>: move cursor to the first character of the current 'word'
  • <ALT e>: move cursor to the last character of the current 'word'
  • <CTRL y>: insert latest deleted 'word'
  • <!$>: repeats the last argument of the previous command.
    Example: You created a directory with mkdir peter/pan/documents/tinkerbell. Now you want to change into that directory with 'cd'. Instead of typing the path again, you type cd !$ and the shell will append the path from the previous 'mkdir' command to the current 'cd' command.

As you venture deeper into Linux land, you'll find that these keyboard shortcuts are also used in other applications when it comes to entering text, for example in browser input fields.

* section index * top

* Available Shell Shortcuts

Mandrake Linux comes with a row of shortcuts, some are native features of bash, some are pre-configured (you'll learn later on how to configure your own shortcuts).

Since the home directory is the focus point of activity for every user, many Unix systems provide special shortcuts for it.
'~' for example is a short form for the name of your home directory. Let's say you are in some far away directory and want to copy a file called 'sometext' to the directory 'docs' in your home directory. Instead of typing cp sometext /home/myusername/docs, you type cp sometext ~/docs which is much shorter, but has exactly the same effect.
In theory, this also applies to the 'cd' command. cd ~ would take you to your home directory, no matter where you are. But even that was considered too much typing. Just type cd and you're back home.

Mandrake Linux provides you with a set of pre-configured shortcuts (called 'aliases'):

  • cd.. executes 'cd ..' (go to parent directory).
  • d executes 'ls' (list directory).
  • l executes 'ls' (list directory).
  • la executes 'ls -a' (list complete directory, i.e. including files starting with a dot)
  • ll executes 'ls -l -k' (list directory in long format, i.e. with file attributes, print file size in KB and not in bytes)
  • ls executes 'ls -F --color=auto' (list directories, append file type indicators and use colors)
  • lsd executes 'ls -d */' (list subdirectories only, no files)
  • md executes 'mkdir' (create directory)
  • p executes 'cd -' (go back to previous directory)
  • rd executes 'rmdir' (delete (empty) directory)
  • s executes 'cd ..' (go to parent directory)
  • used executes 'du -sm * | sort -n' (display disk usage of subdirectories in MB, list by size)

Now that you are a bit more familiar with the shell and some of the shortcuts it provides, it's time to have a look what you can actually do with it besides running simple commands.

* Queuing, jobbing and substituting commands


 
Legal: All texts on this site are covered by the GNU Free Documentation License. Standard disclaimers of warranty apply. Copyright LSTB (Tom Berger) and Mandrakesoft 1999-2002.