Power Shell Usage

Bash Tips & Tricks

Simon Myers

UKUUG Linux 2003 Conference • August 2003

This view lists all the slides from the talk in a continuous format. There is a PDF version available, suitable for printing on A4 paper.

The slides are also designed to be viewed as a slideshow from this file with Mozilla or a compatible browser. JavaScript is used to make pressing N progress to the next slide. The slides fit on a 1024 × 768 pixel display. To toggle full-screen mode in Mozilla press F11.

Switch to the slideshow view.

1 Intro

Happy crowd scene

2 Not a List of Keystrokes

Rat in academic dress, typing

3 History

4 The ‘New Window’ Problem

A terminal with 400 lines of history read from disk, and 100 lines typed

For example a long compilation, or an FTP or SSH session, and you need to do something else local too.

5 The ‘New Window’ Problem

A second terminal, with 400 lines of history read from disk

The history on disk still has just the original 400 lines, so that’s what gets loaded into the 2nd window.

6 ‘Loser Takes All’

Closing the first terminal writes its 500 lines to disk

7 ‘Loser Takes All’

Closing the second terminal writes its 405 lines to disk, over the previous file

8 Rewriting History

If you’ve configured your terminal windows to run log-in shells then you might need to use .bash_profile instead of .bashrc. But on many systems .bash_profile sources .bashrc anyway. This also applies to other mentions of .bashrc in this presentation.

9 Searching the Past

10 Searching the Past

11 Sane Incremental Searching

zip Up goes to the most recent zip command. Further presses of Up cycle through previous zip commands.

gv Up goes to the most recent command starting with those letters. Suppose that found a gvim command and you were looking for a gv command: pressing Space Up would then go to the most recent line starting gv␣.

12 Configuring Up and Down

13 Repeating Command Bits

Suppose these commands have been executed:

$ mount /mnt/cdrom
$ ls /var/tmp

It is then possible to type the start of the next command line (such as ls -l) and simply press Meta+. to have /var/tmp be ‘typed’ for you. Pressing Meta+. again (without an intervening keystroke) will ‘untype’ /var/tmp and replace it with /mnt/cdrom.

Note that on PCs Alt typically functions as Meta, so Alt+. is what would be pressed. This applies to all mentions of Meta in this talk.

14 Grabbing Other Bits

15 Magic Space

Suppose you squeeze a png image to use maximum compression:

$ pngcrush pineapple.png crushed_pineapple.png

You can then list the files’ sizes without having to type their names again:

$ ls -lh !*

Then you can rename the new file to the original name (deleting the uncrushed file in the process). The new name can be ‘typed’ with Meta+., and the original name picked out of a previous command:

$ mv Meta+. !-2^

Before committing a file to CVS you check over which changes you’ve made. Vim conveniently uses colour to highlight the changes, and using a separate window means that they can be kept on-screen while typing the commit message:

$ cvs diff GBdirect/DocTools/Util.pm | gview -

Then the file can be committed, picking its name out of the previous command line:

$ cvs com !:2

16 Magic Space Set-Up

For example the MySQL client and the Perl debugger can also use the readline library.

17 Forgetting Options

If you (attempt to) view a directory’s contents:

$ ls -l /var/spool/exim/
ls: /var/spool/exim: Permission denied

then to see the permissions of the directory itself, add the -d flag:

$ ls -d -l /var/spool/exim/

grep can search through all files in a directory:

$ grep RewriteCond /usr/share/doc/apache/manual/
grep: /usr/share/doc/apache/manual: Is a directory

But only if you remember the -r flag:

$ grep -r RewriteCond /usr/share/doc/apache/manual/

Sometimes processes refuse to die:

$ killall xmms

The -9 flag leaves them with little choice:

$ killall -9 xmms

Other places where flags can be added include ls -tr, rm -r, and chmod -R.

18 Insert-Option Macro

The 2 unused keystrokes with Ctrl are the rather awkward Ctrl+\ and Ctrl+^.

But there are 15 letters available for use with Meta, namely: Meta+A, Meta+E, Meta+G, Meta+H, Meta+I, Meta+J, Meta+K, Meta+M, Meta+O, Meta+Q, Meta+S, Meta+V, Meta+W, Meta+X, and Meta+Z.

19 Default Command Options

ls can be made always to include the -F flag, to add symbols marking directory and command names among the list of filenames.

mkdir can have the -p flag so that it is is possible to create nested subdirectories in one go.

grep can have the -r flag so that it will work on directories. This won’t cause any harm when greping ordinary files.

Other default flags to consider include scp -pr, dirs -v and jobs -l.

20 Defining Functions

21 New Command Names

This ll function produces a long-format directory listing:

function ll
  command ls -Flh "$@"
export -f ll

doc can be made to change to the specified package’s documentation directory and display the files therein:

function doc
  pushd "/usr/share/doc/$1" && ls
export -f doc

Functions can be created for performing file conversions. For example this function takes a single XFig file, such as network.fig, and produces a PostScript file with the same basename, such as network.ps:

function fig2ps
  fig2dev -L ps "$1" "${1%.fig}.ps"
export -f fig2ps

22 Graphical Commands

Mrs Gimp

This is useful for pretty much any command which opens a window, including gv, mozilla, acroread, xfig, and ooffice.

23 Specifying Directories

If you have a tarball in the current directory but you wish to extract it in a different directory, you can change directory then use ~- to refer to the directory containing the tarball:

~/downloads/Mozilla/ $ cd /var/tmp
/var/tmp $ tar xzf ~-/moz Tab Meta+S Meta+S

Afterwards you can change back to where you were:

/var/tmp $ cd -
~/downloads/Mozilla/ $

24 Directory Name Typos

Bash will cope with each component of the typed path having one missing character, one extra character, or a pair of characters transposed:

$ cd /vr/lgo/apaache

25 Directory Bookmarks

Suppose $CDPATH contains ~/pending and /home/www-data; you would then be able to change to their subdirectories from anywhere on the system without typing a full path:

$ cd conference
$ cd intranet/logs

If .. is also in $CDPATH then you can easily change to sibling directories. For example, following on from above you could do:

$ cd docs

26 Setting $CDPATH

27 Completion

28 Completion Cycling

For example, suppose you want to edit your .bashrc. You could start by typing:

$ xemacs ~/.b

Then pressing Tab yields a list of matching files, and completes the name as far as possible:

.bash_history  .bash_profile  .bashrc
$ xemacs ~/.bash

Meta+S will cycle through the completions, so pressing it once gives:

$ xemacs ~/.bash_history

Tapping it another couple of times completes to the required filename.

29 Cycling Set-Up

30 Programmable Completion

When writing a document you often end up with several files with similar names but different extensions:

$ ls
Bash_tips.aux  Bash_tips.log  Bash_tips.pdf  Bash_tips.tex

But with programmable completion, Bash will pick the filetype that matches the command and ignore all the others:

$ acroread ba

Pressing Tab converts the above to:

$ acroread Bash_tips.pdf

If that was the only PDF file in the directory you don’t need to type any of the filename: just type the command name then press Tab and the filename will be inserted. And even if there are a few PDF files, it still may be quicker not to type any of their names and just use Meta+S to cycle through them.

This is useful for any application which only works with a limited group of filetypes. It can also be used to exclude filetypes from commands. For example image and sound files can be excluded from filename completions for text editors.

31 Completion Caveats

32 Accidental Exiting

$ Ctrl+D
$ Use "exit" to leave the shell.
$ Ctrl+D
$ exit

33 Summary

Dancing penguins

© Copyright GBdirect Ltd 2003
This material may not be distributed without explicit permission.