UNIX shell tips

The following are a variety of tips for using the shell that I am collecting as they come up throughout the course. Hopefully, you can use this as a reference when your shell does something strange that you don’t expect.

Useful commands

Command Function
ls Lists the files contained in the current directory
ls -l Gives a detailed list of files in the current directory
ls <dir> Lists the files contained in the directory <dir>
cd <dir> Changes the current directory to <dir>
pwd Lists the path to the current directory
cat <file> Dumps the contents of <file> to standard output
less <file> Views the contents of <file> and allows you to scroll
echo Dumps its arguments to standard output
nano <file> Opens the file in nano, an easy-to-use command-line text editor

Input and Output redirection

The UNIX shell allows you to redirect the input and output of a program to/from other programs and to/from files. If two commands are connected by the “|” character, the output from the first will be used as the input for the second. This operation is called a pipe. The second command will receive the input just as if you had typed it with your keyboard. Many of the standard UNIX commands such as less will accept either a or having data passed to them via standard input. Therefore, the two commands below are identical:

:::sh
$ less my_file
$ cat my_file | less

The second command uses cat to read the file and dump it to standard output. The output from cat is then used as the input for less and less displays the output of cat. This can be very useful if, for example, you want to list the files in a very full directory. The directory /usr/bin usually contains a lot of files, so if we simply run ls /usr/bin it will run off the top of the screen. If I want to more easily view the files in /usr/bin, I can run

:::sh
$ ls -l /usr/bin | less

which will allow me to scroll the output of the ls command.

Along with pipes, you can also direct input/out directly to/from files. This is done with the < and > characters. Using this, we can add one more to our list of identical file viewing commands:

:::sh
$ less my_file
$ cat my_file | less
$ less < my_file

This last one tells the shell to read the contents of my_file and put it into the standard input for less. You can also take the output of a command and dump it to a file. For example,

:::sh
$ cat my_file > my_file2

will make a copy of the file my_file by telling cat to read the file and then dumping the output of cat to my_file2. This can be very useful if you want to record the output of your program. Along with >, you can also use a double-carrot (>>) to tell the shell to append to the given file instead of over-writing it.

Killing and stopping programs

When a command is running in the shell, Control-C will kill it and Control-Z will stop (or pause) it. Killing a process with Control-C will, in general, cause the program to forcefully die regardless of what it is doing. As you are writing and testing your own programs, you will inevitably want to kill them early for one reason or another. The Control-C shortcut allows you to easily do this.

Control-Z, on the other hand, will stop your process and you will see a message that looks something like this:

:::sh
$ ./my_program

[1]+  Stopped                   my_program
$

At this point, the program has stopped. While a program is stopped, all of its resources are held in memory but the program itself does not run. If the program is a graphical program, it will stop responding. You can resume a stopped program with the fg (foreground) command. While you are likely not to actually need to stop a process, it is very easy to do accidentally, and you need to know how to resume one.