Unix commands

Overview

The aim is to explain why we are recommending Unix commands, and to give you a quick overview of a few useful commands:

clang man ls cp mv rm mkdir cd chmod ps kill quota less grep find locate od

There are also some general points

history redirection

Why Unix?

You need to become familiar with multiple platforms (Linux, MacOS, Windows, as well as Android, iOS, ...), but we want you to work in a cross-platform way

Linux (a version of Unix) is the most open, non-proprietary, programmer-friendly system, so it is what the lab workstations run, and MacOS is also Unix-based

Windows has no native programming tools that pay any attention to compatibility with other systems, so you must install Linux dual-boot, or use a virtual machine, or install Cygwin or Bash, all of which are Unix-based

Windows

If you are using your own Windows computer, we will assume that you have installed Ubuntu Linux, either dual boot or within Windows, or a virtual machine, or Cygwin

All of these provide a Unix-like command prompt to replace the Windows cmd prompt

From now on, these notes will assume you are using a Unix-like terminal window rather than cmd so that Unix commands and forward slashes work

Why commands?

You may be surprised that we recommend using command line tools in the age of graphical mouse or touch interfaces, but there are several reasons:

Understanding

It is essential to understand command line work before using sophisticated tools such as IDEs

You will be able to see the individual steps in the process of compiling and running programs

Later, if you want, you will understand what the sophisticated tools are doing, and will be able to judge, configure and customize them more easily

Productivity

For 'experts', the keyboard is faster than the mouse

Typing a command (with abbreviation techniques) is faster than finding it from a multi-layered menu

With graphical tools, it pays to find and use keyboard shortcuts rather than mouse buttons and menus

Compare the time it takes to press CTRL/S in your editor with the time it takes to move your hand from the keyboard to the mouse, home in on the save button, and then move your hand back to the keyboard

Customisation

When using command lines, you don't have to keep typing the same commands over and over

You can use arrow keys to find a previous command and edit it

You can create aliases or shell scripts or build tools to abbreviate commands or set up default options or trigger sequences of commands

Programmability

With command scripts, you can use a loop to arrange to process hundreds of files automatically, without having to interact with a tool for each one

You can arrange to run programs automatically overnight

For many interactive graphical tools, it is worth finding a command-line program that does similar processing for use in scripts

Commands

When you type a command like ls, that means 'find and run the program ls in one of the standard places'

The standard places are defined by your PATH variable, which you can customize

Try echo $PATH to check what it is set to

You can also specify the file explicitly: /bin/ls

Programs

If you have compiled a program into a file prog, or prog.exe on Windows, then you have to run it by typing ./prog

In the past, it was common to include the current directory (.) in the PATH, so you could run your program by typing prog just like a command

But that's a security risk, so now you have to be explicit

The clang command

Compile a C program

The man command

To get the manual entry for any Unix command, you can use man, E.g.

You can use Google, especially if you don't know what the command you are looking for is called, but man is more likely to match what is actually installed on your computer

The ls command

List the contents of a directory (folder)

The cp command

Copy a file from one filename to another

The mv command

Move and/or rename a file:

The rm command

Delete (remove) a file

Note: there is no command to undelete a file in Unix, when it is gone, it is gone...

That applies to the target of cp, mv or any other command, as well as rm

The mkdir command

Make (create) a new directory

The cd command

Change directories

The chmod command

Change the protection of a file or directory

The changes are "u" for you, "g" for your group, "o" for others or "a" for all (=ugo), then "+" to add or "-" to remove permissions, then "r" for read or "w" for write, or "x" for execute (or directory search) permission

The ps command

See what processes are running

A process is a running program

The kill command

Kill a process

The -KILL option makes sure the process dies, even if it is not listening or responding, e.g. it has gone into an infinite loop

The quota command

See how much disk space you have used and what is left

The less command

Display the contents of a text file (without editing it)

Type <SPACE> to get the next screenful, or q to quit. Arrow keys take you through the file one line at a time, G sends you to the end, '/string' searches for string

The original was more before enhancement - remember `less is more'

The grep command

Search text files for specific strings or patterns, and print out each line which contains a match

The pattern which grep uses is called a regular expression or regexp for short - try man grep and man regexp

The find command

The quotes around the pattern prevent the shell from interpreting the characters in the regexp, allowing find to see them

The search will start in the directory DIR and recursively scan any subdirectories to check for a match

The locate command

If you want to search system directories for something installed, this will usually be much quicker than using find, but it only works on predefined directories

The od command

View the exact bytes in a file, in hex

And you can combine them, so for 2-line file eg.txt:

> od -c -t x1 eg.txt
0000000   O   n   e  \n   T   w   o  \n
         4f  6e  65  0a  54  77  6f  0a
0000010

Option -A x gives hex addresses rather than octal (there are better commands, but od is always available)

History

See or re-use previous commands

Standard input and output

The idea of standard input and output streams applies to almost all programming languages and almost all operating systems

A program has a standard input stream which represents the text typed into a program by the user, assuming that the program is run in or from a terminal window

Its standard output stream is a default stream of text printed by the program and displayed in the terminal window

Standard error output

There is usually a second output stream, the error output stream, for error messages

By default, this also goes to the terminal window

But, e.g. when a program is run from a script, the normal and error outputs can be sent to two different places

Redirection

Redirection is the idea of getting the standard input text from somewhere else other than the keyboard, or sending the standard output or error text somewhere else other than the terminal window

Input and output files

A simple example is to get the input from a file:

program < inputfile

Or to send the output to a file:

program > outputfile

The mechanism

This redirection is carried out by the command line processor (terminal window, dos prompt or whatever) regardless of what language the program is written in

The program itself has no control

It is system dependent, but the basics are the same across Linux, MacOs and Windows

You may be able to improve the way it works by using a different command line processor (e.g. Cygwin instead of dos prompt on Windows)

Advantages

The main advantage is to be able to run programs non-interactively, even though they were written to be interactive, without having to re-write or re-compile them

So, it is useful in debugging and testing

For a more information, see wikipedia