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
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
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
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:
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
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
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
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
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
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
clang commandCompile a C program
clang -std=c11 eg.c compiles a program in eg.c,
producing a program in a.out (or a.exe on
Windows)clang -std=c11 eg.c -o eg names the program egclang -std=c11 -g -pedantic -Wall
eg.c -o eg switches
on warnings./eg runs the programman commandTo get the manual entry for any Unix command, you can use
man, E.g.
man ls gives information about how to use the ls
command - press q to quitYou 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
ls commandList the contents of a directory (folder)
ls lists only the names of the files and directoriesls -l is the long version showing protection
etc.ls -s shows the names and the sizes of the files and
directoriesls -a shows all the files, including the dot filesls *.sql shows all the files ending with
.sql the * is a 'wildcard' symbolcp commandCopy a file from one filename to another
cp filename newfilename copies the contents
of filename to newfilenamecp *.tex directoryname copies all files with the
extension .tex into directorynamecp -r directoryname otherdirectory copies
directory directoryname to otherdirectory
recursively (i.e. including all sub-directories, sub-sub-directories, ...)mv commandMove and/or rename a file:
mv filename newfilename renames the file filename
to newfilenamemv filename directory/newfilename moves the file to another
directory as well as renaming itrm commandDelete (remove) a file
rm filename deletes the file filenamerm * .sql deletes all the files in the directory - a big mistakeNote: 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
mkdir commandMake (create) a new directory
mkdir directoryname creates a directory
called directorynamecd commandChange directories
cd .. change to the directory above the current directorycd (or cd ~) change to your own home
directorycd directoryname change to the directory called
directorynamechmod commandChange the protection of a file or directory
chmod go+r file change the protection of file to allow
group and others read accesschmod a+rx directory change the protection of directory to
allow read/search for everyoneThe 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
ps commandSee what processes are running
psA process is a running program
kill commandKill a process
kill -KILL process_id kills the process process_id, the
process_id is shown after the ps commandThe -KILL option makes sure the process dies, even if it is not
listening or responding, e.g. it has gone into an infinite loop
quota commandSee how much disk space you have used and what is left
quota -vless commandDisplay the contents of a text file (without editing it)
less filename displays the contents of filename, one screenful
at a timeType <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'
grep commandSearch text files for specific strings or patterns, and print out each line which contains a match
grep FIXME *.{h,c} will find all the places in my .h and .c
files where I've reminded myself to fix somethingThe pattern which grep uses is called a regular expression or regexp for
short - try man grep and man regexp
find commandfind DIR -name 'NAME' will search for files that match the
pattern NAME, which is a simple filename or a regexpThe 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
locate commandlocate NAME will search some system locations for a file that
contains the string NAMEIf 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
od commandView the exact bytes in a file, in hex
od -c file shows the characters in fileod -t x1 file the hex bytes in fileAnd 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)
See or re-use previous commands
history lists previous commands!N repeats the N'th command!name repeats the last command that started with name,
e.g. !ls repeats the last ls command.!! repeats the last commandcontrol-r incrementally searches through the history
backwards, e.g. C-R fi will find the previous command beginning
with fiThe 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
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 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
A simple example is to get the input from a file:
program < inputfile
Or to send the output to a file:
program > outputfile
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)
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