The commands in the examples below are always the first word, while the rest of the words are arguments to the commands.
Task:
Make a directory
Example:
mkdir hw1
mkdir makes a directory that can hold files and other subdirectories. Above, we issued a command to make a directory called hw1 under the current directory.
Task:
Change (to another) directory
Example:
cd hw1 OR cd .. OR cd foo/bar
cd allows you to go to another directory, and thus, change
your current directory.
Above, we first go into the directory named hw1, which must
exist under the current directory. Then, we go to the directory above
us (dot dot (..
) is shorthand for the directory
above). Finally, we go to a directory named bar, under a
directory named foo under our current directory.
Task:
Find out what is your current directory
Example:
pwd
pwd always reports the full path of your current directory.
Task:
List files, directories or their information
Example:
ls OR ls hw1 OR ls -l prog1.c
ls is used to list files (not their contents). For example,
ls
above list the files (and subdirectories) in the current directory. Similarly,
ls hw1
ls -l prog1.c
prog1.c
, such as how big it is etc. The
-l
(minus ell, not one) part of the command is a flag for the ls
command.
Task:
Look at the contents of a text file
Example:
cat hw1.c
Spits the contents of a text file on the screen.
Task:
Look at the contents of a text file page by page
Example:
less hw1.c
To view a longer text file, use the less program followed by the name of the file you want to view, as in the example.
Less will allow you to page forward through the file using the <SPACE> bar and page backward using the b key.
When you are done looking at the file, type q to quit less.
Task:
Copy a file
Example:
cp hw1.c hw1.c.bak OR cp testfile1 testfile2 ~/cs2110
cp makes a copy of a file. This last argument specifies the destination name. The arguments preceding it are the source file(s).
When the destination is a directory, files with the same name as the source files are placed in that directory. In this case, any number of source files (to be copied) may precede the destination directory.
In both cases, since a copy is made, the original source file is left untouched.
When the command would cause a file to be copied over a file that already
exists, you may be asked to confirm the copying command. To enforce
this confirmation behavior, you may want to use the -i
flag with the copy command:
cp -i source destination
In our first example, a backup copy of the file hw1.c
is
made--the copy's name is hw1.c.bak
and will reside in
the current directory.
In the second example, copies of the files testfile1
and
testfile2
are made in the directory
cs2110
, which resides under our home directory (i.e., referred to by
the ~
).
Task:
Move (or rename) a file
Example:
mv writeup.bak writeup OR mv hw3.c hw3.h ~/homeworks
mv moves or renames files. The last argument specifies the destination. When the destination includes a path or is the name of a directory, files with the same name as the source files are placed in that path/directory. In this case, any number of source files (to be moved) may precede the destination.
In both cases, the original source file won't exist any more (except with their new names and possibly new locations).
When the command would cause a file to overwrite a file that already
exists, you may be asked to confirm the move command. To ensure this
confirmation behavior, You may want to use the -i
flag with the move command:
mv -i source destination
In our first example, the file writeup
is restored from a
backup copy (which we could have made with cp) named
writeup.bak
. After, the file writeup.bak
no
longer exists. The new file writeup
will end up in the current directory.
In the second example, the files hw3.c
and
hw3.h
are moved into the directory homeworks
,
which resides under our home
directory (i.e., referred to by the ~
). There
will no longer be files named hw3.c
and hw3.h
in the current directory.
Task:
Remove a file
Example:
rm file1 file2
Removes the files. There is no easy way to undelete these files. They can be retrieved from a backup tape sometimes.
Task:
Show the Manual Page (documentation) for a command (or function)
Example:
man ls OR man -s3m pow OR man -k pow
man gives documentation for a command, such as what it does, what flags it takes, etc. Man pages are a bit technical, but you get used to using them over time.
You can also look up C(++) library functions. While looking up a library function, as in:
man printf
Task:
Compile with the make utility
Example:
make OR make -f hw1.mak
The make utility makes compiling more complicated programs easier, more consistent, and more efficient. You run the command make in the directory where your source code for a program exists.
By default, the make utility expects a file called a make file
to be present in the same directory. This make file usually
specifies how to compile your program. The make utility looks
for a make file in a UNIX file named either
Makefile
or makefile
in the directory where
make is run. The -f
option can be used to tell
make to look for a different make file as in the
second example.
Make will display what commands it is using to compile, etc. as it goes along.
Task:
Compile with the gcc compiler
Example:
gcc file1.c file2.c OR gcc -g -o hw1-bank hw1-bank.c -lX
gcc is the compiler we are using for this course. For more complicated programs, you may want to use the make utility for compiling.
The easiest way to compile a program (although not always
the best way) is to type gcc
followed by the names
of all .c
files that make up the program (as in the
first example). By default, gcc creates an executable named
a.out
.
Caution: This may not work always work.
For example, -o
(that's oh, not zero) followed by
the name for the executable. In the second example, -o
hw1-bank
hw1-bank
instead of a.out
.
The -g
flag must be used when compiling code that you
want to be able to debug with gdb
.
The -lX
flag at the end of the command tells gcc to
link a library with your
executable (in the example, the X Windows library). You'll normally be
told when you must use the -l
(minus ell, not one) option
to link in a library.
sum.c:39: unterminated string or character constant
sum.c:32: possible real start of unterminated constant
Resolve the first errors in the code first since they often cause later errors.
Task:
Debug executable compiled with gcc
Example:
gdb a.out
Gdb is the debugger that
goes with the gcc
compiler. You
always run the debugger on the executable for a program.
To be able to debug an executable, its source code must have been
compiled using the -g
flag
of gcc
.
The debugger will allow you to run the program, stop it at certain points, and examine/change variables. It also tells you what the offending line is when a program crashes in the debugger.
When you run the debugger on an executable, make sure that the source code files for the program are in the same directory.
Task:
Edit or create files with Emacs
Example:
emacs hw1.c & OR emacs &
Emacs is an editor for text files. Thus, you'll probably want to use it to create and edit your source code files.
If you give a file name with the emacs command, as in the
first example, it will load that file if it exists, or start a new one
using that name if it does not. Since emacs brings up a
window, you should run it with the ampersand
(&
).
When you are editing in emacs, you are editing a copy of the file, so remember to save edits to disk before you exit emacs.
Task:
Use &
(running in background)
Example:
firefox & OR emacs hw1.c &
The ampersand (&
) at the end of a UNIX
command runs the command and gives you the prompt back right away. This
way you can continue to type more UNIX commands.
It is useful to put the ampersand at the end of commands that bring up their own windows like a web browser or emacs.
The amperand (&
) means something different when
used immediately after a greater than
(>
) for output redirection or after the
pipe symbol
(|
) for piping.
Task:
Use <
(input redirection)
Example:
a.out < data1
The less than (<
) character makes an executable take input
from a file instead of waiting for something to be typed at the
keyboard. This is called input redirection.
For example, if data1
above contained the following:
2 3.4 4.5
Then, the program a.out
would behave as if we
typed 2
,
then hit <RETURN>
,
typed 3.4
,
then hit <RETURN>
, and finally,
typed 4.5
and hit <RETURN>
.
Task:
Use >
(output redirection)
Example:
a.out > output1 OR gcc file.c >& output_and_errors OR a.out >> output1
The greater than (>
) character sends
the output of an executable to a file
instead of putting it on the screen.
This is called output redirection.
For example, if the executable a.out
above normally prints
out "Hello, world!" on the screen, then the first example would cause
nothing to appear on the screen, but instead, "Hello, world!" would be
found in the file output1
.
Some output from programs is meant as error messages. Sometimes these
will not be redirected by greater than (>
).
To redirect both regular and error output to a file, use the ampersand
after the greater than symbol (i.e., >&
), as in
the second example.
When you use output redirection, the first thing UNIX does is to create
(or clear out, if it exists) the output file (e.g.,
output1
and output_and_errors
in our examples).
Be careful with this, since if you try to do something like:
cat a b c > a
You may run into problems since a gets cleared out (the
> a
"cat a b c
"
When you don't want the output file cleared out first, but rather, want
the output of the command to be appended to the end of the file, use 2
greater thans (>>
), as in the third example.
Using the ampersand to redirect both regular and error output also
works with appending (i.e., >>&
).
Task:
Use |
(piping)
Example:
a.out | sort OR gcc file.c |& less OR a.out | sort | less
The pipe symbol (|
) sends
the output of an executable as the input
of the command that follows it, instead of putting the output on the
screen.
This is called piping.
For example, if the executable a.out
above normally prints
out a bunch of names on the screen, then the first example would cause
those names to go as input to the sort
command, and then
sort
would print out the names in sorted order on the
screen.
Some output from programs is meant as error messages. Sometimes these
will not be piped by |
. To pipe both regular and
error output to another program, use the ampersand after the pipe
symbol (i.e., |&
), as in the second example,
where the output of the compiler gcc is paged with
less.
You can string along several commands with pipes. In the third example,
the output of the program a.out
is first sorted and then
paged with the program less.
Task:
Use Up and Down arrows (command history)
On our system, you can use the up and down arrows to scroll through the commands that you typed during the same login session.
This is an easy way to retype or edit commands you've already typed in.