skip to content
 

Command Line Tools

A very limited overview of what the bash shell can do to make your life easier via the command line (or prompt).

Command history (easily repeat previous commands)

  • To scroll through the commands you recently typed, use the up and down arrow keys.
  • A command can then be repeated by pressing enter, or edited before re-running.
  • Type !! to repeat the last command.
  • Type history to view the commands you recently typed. Each command will have a number beside it. To repeat that command use its number preceded by an "!" as in !123
  • Type echo $HISTSIZE to see the number of recent commands the shell remembers. You can change this number by editing your startup files.
  • Your recent commands are also stored in a hidden file called .bash.history.machine_name (updated when your shell exits e.g. when you close a terminal window).
  • Type ls -l .bash_history* to see all these files and when they were last changed.

Tab completion and command line editing (shortcuts for typing commands)

  • Use the Tab key to complete the name of a command; type in the first letter or two and then press Tab. If there is more than one command with that name, press Tab again and the shell will offer you a choice.
  • Likewise if you are operating on files, press the Tab key after typing the first few characters of the filename and the name will be completed.
  • Command Line Editing: Scroll back and forth in the command line with the left and right arrow keys. You can also use emacs-style editing e.g. Control-A to go to the start and Control-E to go to the end.

Aliases and scripts (write your own commands)

  • Can't remember the name of a command? Then use an alias to rename it to something you can remember. Or if the command is long and complicated use an alias to make it shorter. Type in alias to see which aliases we have already set up for you. To set a new one follow the syntax in the output. To set it up permanently you will need to add it to your startup files.
  • If you want to run multiple commands with a single new command, it is easiest to write a script. At the simplest level this involves three steps.
    • Create a file containing your sequence of commands.
    • Make this file executable (chmod a+x name_of_file).
    • Store it somewhere on your PATH so the shell can find it, such as in your ~/bin directory.

Useful links