Basic Unix/Linux Commands for CSCI Students


Last revised 16 January 2007, 11:40 am

Basic File Management Commands

ls Lists names of files in current directory.
ls -l Long listing, gives more information.
ls Hello* Lists only files whose names start with Hello.
ls -l Hello* Long listing of same files.
cat names.txt Displays contents of textfile names.txt on screen. See also more command below.
more names.txt Same as above, but shows only one "page" at a time. Use space bar to advance to next page, type q to quit at any point.
cp a.dat b.dat Copies file a.dat to b.dat.
mv a.dat b.dat Renames ("moves") file a.dat to b.dat.
rm Hello.java Deletes ("removes") file Hello.java. Be careful with this command...
rm Hello* Deletes all files whose names start with Hello. Be even more careful with this command...

Basic Directory Management Commands

Use the following commands to create and manage directories for organizing your files.

mkdir playpen Creates new directory named playpen.
rmdir playpen Removes directory playpen (must be empty).
cp a.dat playpen Copies a.dat into directory playpen. (Keeps copy in current directory).
mv a.dat playpen Moves a.dat into directory playpen. (Removes file from current directory).
ls playpen Lists files in playpen directory.
cd playpen Changes current "default" directory to playpen ("enters" playpen).
cd .. Enters "parent" of current directory.
cd Enters "home" (login) directory.
pwd Displays name of current directory.

Creating and Editing Text Files

The following commands are used with many types of "text files", including both Java and C++ source files. These are normally given names with suffixes like .java and .cpp, but they are edited and stored as ordinary ASCII text files like readme.txt.

vi Hello.java Edits textfile Hello.java (Java source file). Creates file if it does not already exist. Ancient Unix editor, but available on almost all Unix/Linux systems. Can be used remotely via telnet connection.
pico Hello.java Edits textfile Hello.java (Java source file). Creates file if it does not already exist. Can be used remotely via telnet connection.
gedit Hello.java & Edits textfile Hello.java (Java source file). Creates file if it does not already exist. Not suitable for remote use via telnet connection.

Compiling and Running Java Programs

javac Hello.java Compiles source file Hello.java, produces Java bytecode file Hello.class.
java Hello Invokes Java Virtual Machine (JVM) to execute bytecode file Hello.class.

Saving and Extracting Java Projects: JAR Files

jar cf Project1.jar Project1 Creates new JAR (Java Archive) file named Project1.jar, compresses and stores Project1 directory and all its contents (including both data files and subdirectories).
jar tf Project1.jar Displays contents of JAR file Project1.jar. Lists complete directory structure (including subdirectories and their contents).
jar xf Project1.jar Extracts (uncompresses) complete contents of JAR file Project1.jar. Reconstructs directory Project1 and all its contents as subfolder of current working directory.

Compiling and Running C++ Programs

c++ Hello.cpp Compiles C++ source file Hello.cpp to binary executable file a.out.
c++ Hello.cpp -o Hello Compiles to executable file Hello.
a.out Loads, runs machine code in executable file a.out.
Hello Loads, runs machine code in executable file Hello.

Zipping and Unzipping C++ Projects

zip proj1 proj1/* Creates new zip file named proj1.zip, compresses and stores proj1 directory and all its toplevel contents (data files).
zip projfiles * Creates new zip file named projfiles.zip, compresses and stores all data files contained within current directory (but not current directory itself).
unzip proj1.zip Unzips project (reconstructs proj1 directory and its toplevel contents).
unzip -t proj1.zip Lists zip file contents without actually unzipping.

Save Yourself Some Typing (history)

history Displays numbered list of most recent commands.
!100 Repeats command 100 in history list.
!p Repeats most recent command starting with p.
!ca Repeats most recent command starting with ca.

Using Online Help Pages (man)

man ls Shows help page for ls command.
man g++ Shows help page for g++ (real name for GNU c++ compiler).
man man Shows help page for man help pages themselves.

And Finally...

logout Don't forget!.


UNCA Computer Science Department
UNCA Home Page