Basic Linux Commands for CSCI Students


Last revised 20 August 2010, 11:35 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 Python, Java, and C++ source files. These are normally given names with suffixes like .py, .java, and .cpp, but they are edited and stored as ordinary ASCII text files like readme.txt.

nano Hello.java Edits textfile Hello.java (Java source file). Creates file if it does not already exist. Can be used remotely via ssh connection.
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 ssh connection.
gedit Hello.java & Edits textfile Hello.java (Java source file). Creates file if it does not already exist. Not suitable for remote sessions via ssh 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.
java -cp MyStuff.jar Hello Invokes Java Virtual Machine (JVM) to execute bytecode file Hello.class, archived in JAR file MyStuff.jar. Note: option -cp indicates that classpath will be specified by following argument.

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

cc Hello.c Compiles C source file Hello.c to binary executable file a.out.
cc Hello.c -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.

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/C++ Projects

zip -r proj1dir proj1 Creates new zip file named proj1dir.zip, compresses and stores proj1 directory and all its contents (preserving subdirectory structure).
zip proj1files proj1/* Creates new zip file named proj1files.zip, compresses and stores proj1 directory and all its toplevel contents (data files). Subdirectory contents are not stored.
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.

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...

exit Closes terminal window.


UNCA Computer Science Department
UNCA Home Page