Bash

This is the new area for BASH or the Bourne Again SHell. BASH was created by Brian Fox in 1987 and since 1990 Chet Ramsey became the primary maintainer. It's based off BSH which was a shell created by Stephen Bourne.

So what is a shell you might ask. Well shells are just user interfaces. They provide a way of tell the underlining software, which is known as the Operation System, what the user wants. Either by the user typing in commands, as in BASH, or moving the mouse and using windows like Gnome and KDE. So BASH is a command line interface. This is where you type in commands and by pressing enter you tell BASH do what I told you to do.

Basic BASH Concepts

A couple of things we need to talk about before we begin typing in commands.

The Unix file system is hierarchical. That means that Unix file systems, if you would draw them out, have a tree like structure. The main directory or as I'll sometimes refer to it is the / directory. This directory contains all the other directories. You use commands under Bash to move between these directories and to alter the contents of the directories.

Another concept we need to look at is prompts. If you take a look at a Bash screen you will see that before the flashing cursor is what they call the prompt. The prompt is there waiting for you to enter a command. In Bash it often contains useful information. Depending on how it's been setup it might contain your username, the machine name. At the end of the prompt you will see a $ or a # sign. The $ indicates that your a normal user. If you have # that mean that your in superuser or admin mode.

This then brings us to an important point. Unix was designed from the outset to be a multiuser OS. Right from the word go it was decided to divide users into 2 main groups.

Standard user: These are users that have just enough access to get what they need done done. They are prevented from modifying system files etc.

Admin/Superuser: This user (because you only tend to have one of them) has full permissions under Linux to modify the system files etc.

Basic Bash Commands

Bash commands are entered in a very simple format. Often but not always in the form of command -Options A good example of this is the ls command. ls -la where ls is the list command and -al is the options.

ls is the list command. It provides a simple list of what is in the directory. "List"

pwd shows you where you are in the directory tree. "Print Working Directory"

cd Allows you to move between directories. cd .. will take you down one directory cd subdir/ will move you up to the subdirectory called subdir and cd /etc/network/ will move you from your current place to a new trunk. "Change Directory"

cp This command copies files from one place to another. example cp /home/me/mytestfile.txt /home/me/testdir/. This leaves a copy in the original place. "Copy"

mv Very similar to cp except it doesn't leave a copy. It can also be used to rename a file. "Move"

rm Removes unwanted files. Example rm mytestfile.txt will delete or remove the file "mytestfile.txt". Be very careful with this command as you can delete the entire system. "Remove"

mkdir This command creates directories. Example mkdir mytestdir. "Make Directory"

rmdir This will remove directories provided there are no files contained within. "Remove Directory"

man command This will give you a list of the options contained within the command. Example man ls will show you that there is an option called a that gives you a complete list of all the files and l that gives more information about the files in the directory. "Manual"

cat Useful if you want to take a quick look at what's contained in a text file.

nano/vi/emacs These are very simple text editors.

Tips and Tricks

So far the vast majority of this has come from Daniel Mashonkin's talk (Excellent by the way) detailing his "Top Ten Tips and Tricks". For his slides click here. So from Daniel's Tips and Trick these are his favourites:

  1. $ du -sh /*  # it prints results for each directory (except hidden .*)

  2. $ find . -name “*.sh” -exec chmod +x {} \; This will convert a whole pile of scripts to be executable.

  3. Using the pv –pipe viewer. It provides processors with a progress bar. Examples are:
    1. $ pv somefile | gzip > rt94-171-06.gz  

      • 128MB 0:00:15 [ 9.1MB/s] [=====>.....................] 18% ETA 0:01:07

    2. $ tar -czf - . | pv -s $(du -sb | grep -o '[0-9]*') > out.tgz #shows ETA

      • 44.3MB 0:00:27 [1.73MB/s] [>..........................] 0% ETA 13:36:22

    3. $ tar -cf - . | pv -cN tar -s $(du -sb | grep -o '[0-9]*') | gzip | pv -cN gzip > out.tgz #more then one copy of pv

      • tar: 97.1MB 0:00:08 [12.3MB/s] [>......................] 0% ETA 1:50:26

      • gzip: 13.1MB 0:00:08 [1.6MB/s] [....<=>................]

    4. Or Finally ;-)

      • %pv /dev/urandom > /dev/null

      • 18MB 0:00:05 [ 3,6MB/s] [...<=>............................]

  4. At the beginning of a BASH shell put #!/bin/bash -x instead of just #!/bin/bash This will produce debugging information for you. Example:

    • #!/bin/bash -x

    • echo "hello world"

    • exit 0

    • This produces
    • $  /home/user/bin/test.sh

    • + echo 'hello world'

    • hello world

    • + exit 0

  5. strace will perform the same task. you can use it by typing $ strace echo “test”

  6. To find out what files have been accessed in the last 5min I use find like this $ find /var/log -type f -cmin -5 .

  7. lsof will give you a list of open files. You call also use flags to specify the type of file. Example lsof -U will only show you open Unix sockets.

  8. Using the comand screen. This useful tool screen is a screen manager with VT100/ANSI terminal emulation. Examples:

    • $ screen runs new VT

    • $ screen -ls lists active VT

    • $ screen -r re-attach to VT

    • Hot keys are: Ctrl+a d  this detaches active sessions. Ctrl+a H Starts writing logs. Ctrl+a S Splits the screen. Ctrl+a Tab Switches between screens and finally Ctrl+a c Creates a new screen.

  9. Instead of creating your own shell script to repeatedly run a program you can use watch to do it for you. Example $ watch df This will print the disk usage every 2 seconds.

  10. Use reset to reset the screen. Useful for the odd time the screen fills with crap.

  11. By installing the gpm or general purpose mouse you can then use the mouse within the console. Very useful for copying and pasting.
  12. Last but not least Daniel likes openssh. Openssh is an open secure shell program. It can be used in the following ways:
    1. To create a secure link ssh daniel@example.org. By doing this you can then send commands to the example.org server via a secure link.

    2. Do some port forwarding $ ssh -L 127.0.0.1:3389:192.168.1.1:3389 example.org .

    3. Send a single command over to a server ssh user@example.org cat /proc/cpuinfo

    4. Create a network tunnel.
      • # ssh -f -w 0:0 example.org "ifconfig tun0 192.168.0.2 pointopoint 192.168.0.1 netmask 255.255.255.252 mtu 1400 && route add -net 10.17.2.0/24 gw 192.168.0.1" 

    5. And if you want to setup a connection that doesn't need a password then use:
      • $ ssh-keygen

      • $ cat ~/.ssh/id_rsa.pub #and copy contents

      • Then on the remote system create the directory ~/.ssh/authorized_keys and paste contents there. Vuala! After that no passwords required!

    • Note: some features might disabled by default. To enable edit /etc/ssh/ssh_config. If your using windows then the putty client is very good.

Tab Key The tab key is useful. The tab key will try to complete the command you have typed in. If it can't it will produce a list of possible options.

Up/Down Keys These enable you to cycle through the commands that you have entered. These are stored in the .bash_history file.