Command Line: Introduction
Terminal runs a Bourne Again Shell (BASH — one type of Unix interface).
It allows you to use command line
arguments to execute various operations on your computer.
Basic
Commands
If you know the command name which you want to know how to use, you
can normally get a detailed information by reading the man pages;
type in :
man cmd
where cmd is the command name, and the man page infromation
will be dipslayed in the Terminal window. To return back to the
BASH, type 'q'.
Here are some common UNIX commands:
help [command]
Display on screen
help.
This will list the commands on which help is provided. If
you specify command then help specific to that command will be
displayed.
cd [path]
Change your present working
directory.
This command is used to change the current directory.
path is substituted for either an absolute or relative folder
path.
ls [directory]
List the contents of a
directory.
This command will list the contents of the specified
directory to the screen. If no directory is specified then the
contents of the current folder is listed.
pwd
Present
working directory.
This command will display the path of the
current directory.
rm [files]
remove (delete)
files.
This command will remove the specified files if you have
write access to them. (DANGEROUS - try "rm -i" instead to
get a confirmation prompt.)
exit
Close Terminal
echo
[variable]
Output to the terminal
This command will display
the value stored in the specified variable.
"Tab"
Command
completion.
Terminal is capable of filename and command completion.
When specifying a path just press the tab key and Terminal will
complete the path as much as it can (up to any ambiguity). If Terminal
cannot complete the path the computer will beep. Press tab again to
see a list of all the possible completions. This can be used any time
a filename or command is expected. (Very useful.)
*
wildcard.
This
character is used to match everything (with some limitations). For
example "rm *" will delete all files in the current
directory (DANGEROUS), where as 'rm *.class' will remove every file
with a .class extension in the current directory.
File
System
For the most part Terminal follows UNIX file
conventions, and as such uses the forward slash '/' not the standard
windows back slash '\' as file separators. A notable exception to
this is Java's CLASSPATH variable which must be set using the
Windows file convention.
The symbol '.' refers to the
current directory. Thus the next 2 commands are identical
ls
ls
.
(Both list the contents of the current folder to the
screen.)
The '..' directory refers to the parent of the
current directory.
The '/' directory is the root directory and
is the folder that Terminal is installed in.
To specify a drive
on your computer you must type '//x/' where x is substituted for a
drive letter.
For example to change the directory to drive d type
'cd //d/' and press Enter.
Miscellaneous
When
reading help files, or other BASH tutorials, you may see commands
such as 'C-x'. 'C' stands for the control key, so 'C-x' would mean to
hold the control key down and press 'x'.
C-c (Control +
c)
Aborts the current program. We should never have to use
this, since every program should be able to exit by itself, but if a
program crashes or is unable to exit, we need to abort it - and you
will use it lots once you get into GUI programming in Java.
Arrow
up/down
Cycles through the history of entered commands.
Arrow
left/right
Moves the cursor in the current line.
Java
Commands (from the Sun Java Development Kit - jdk)
javac
[file].java
Compile a .java code file into byte code.
After
you have written and saved your java application as a .java file you
must use this command to compile it before it can be executed. In
most cases you will use the cd command to change the current
directory to the one the java file is located in, then use the javac
command to compile it. If there is a syntax error in your code the
javac command will fail and it will notify you of the error, telling
you the line number and what is wrong. Usually the compiler is quite
good, but don't always believe what it says, sometimes something as
simple as forgetting a closing bracket or semi-colon can cause the
compiler to report several errors when there is only one. If you have
multiple java classes that need to be compiled it is a good idea to
use 'javac *.java' which will compile all the .java files in the
current directory without you having to specify them all
individually.
java [class] [input arguments]
Execute
a Java class file.
When using this command you only specify the
name of the class file, not its extension. ie you don't include the
.class extension. If you get an error running this command make sure
you have compiled your classes and that the class you are trying to
run has a main method. Any input arguments will be picked up by your
java application and placed in the args string array.
jar
[options] [jar file] [files or directories]
Construct archives
of files after compressing them. List and extract files from
archives.
jar is similar to the Unix tar or zip commands. To
compress files, substitute [options] with 'cvf', [jar file]
with the name of the compressed file you want to create and [files
or directories] with a list of files or directories you want to
compress. To decompress files substitute [options] with 'xvf'
and [jar file] with the name of the compressed file you want
to decompress.
Java Miscellaneous
Terminal
itself doesn't run the javac or java command (or almost all of the
other commands you give it) itself. Terminal looks for these commands
when you give them in the directories specified in the PATH variable.
To see what directories are in the variable type 'echo $PATH'. If you
go looking through those directories you will find a java.exe and
javac.exe file. These are the applications Terminal calls when you
compile or execute java code.
Similarly there is a CLASSPATH
variable (you can see this by typing echo $CLASSPATH) that is used to
specify where Java packages are stored.
Both the PATH and
CLASSPATH variable can be set like this:
PATH=$PATH:'additional path'; export PATH
or like this:
export CLASSPATH=$HOME/'my_java_classpath':.