An Introduction To The Linux Terminal and Other Useful Information

For CSE20211, we will be using the Red Hat Linux Enterprise operating system (Version 6)

The following is an introduction to several simple concepts. Please note that these are NOT intended to be step by step instructions, but rather guidelines to get you started.

Logging In

The monitors on the desk are connected to machines with both Linux and Windows installed. If you see a windows start screen:

Opening A Terminal

While Linux, like any modern operating system, has an intuitive desktop interface, performing sophisticated tasks requires a command-line terminal. The advantage of the command line terminal is that commands can be specified very precisely, allowing both for repetition and clear specification of complex tasks.

Navigating the Directory Tree

You should already be familiar with the notion of a directory tree. UNIX based directory trees sit upon at / which is the "root" file-system directory. Each folder can contain files or other folders within it. In addition to the root folder, you also have a "home" folder, which, by default is your personal folder in the AFS space.

Most commands require you to specify an operator on the command line after the command. For example:
cd /afs [return]
The cd command specifies that you want to change directories. The /afs specifies the destination. Pressing [return] tells the terminal to execute the command.

The following commands can help you navigate the directory tree:
pwdWhere am I?: This command tells you your current location in the directory tree.
lsList: Tells you what is in the folder at your current location (or the specified location).
cdChange directory: This command changes the current directory to a new directory.
cd ..Change directory up a level: This command changes the current directory to the directory containing the current location.
manManual: Gives information about other commands.

File Manipulation

In addition to moving about the directory tree, commands are available to create, copy and remove files and folders. For example:
mkdirMake Directory: Creates a new directory
catQuickly outputs the contents of a file
cpCopy: Copies a file from a source to a destination
mvMove: Move or rename a file from a source to a destination
lnLink: Creates links between files. A link is (essentially) a short cut between one file and another.
rmRemove: Deletes a file. BE CAREFUL with this command
rm -rRecursive Remove: Deletes an entire directory with all contents.

File Editing

Many file editors exist for creating files and editing them. To start with, for this class, we will introduce a simple editor called nano. (If you are already comfortable with another editor that is fine.)
Try the command:
nano foobar.c
If you have a file called "foobar.c" at your current location, this will open it for editing. If not, then the file will first be created, and then opened.

Note that the ^ symbols at the bottom of the nano screen stand for the "control" key. So, Control-X means exit. Most key commands (such as saving your work) can be found at the bottom of the nano screen.

Compiling Some Code

The name of the compiler we will use for this course is gcc. This is an open-source C compiler which is in common use.

By convention, C code files end in the suffix .c
That is, test.c is an example of a C code file. If you want to compile test.c into an executable, run the command:
gcc -o test test.c
The -o flag specifies that "test" should be the name of the output file.
If the code is good, it will compile. If not, fix it.

Handing in Assignments

All assignments for this course will be handed in electronically in the AFS course dropbox. Your dropbox is located at:
/afs/nd.edu/coursefa.11/cse/cse20211.01/dropbox/[your user name]/
Within this directory, the TAs will create directories called lab1, lab2, lab3, etc. Put required files in the the corresponding folder for the lab.

WARNING: Once the lab is due, write access to the folders for that lab will be shut off. As such, it is technologically impossible to hand in work after that time.

Using Multiple Terminals

It can often be useful to launch multiple terminal windows. Each terminal will be able to have its own location in the directory tree and run independently.

One common scheme is to have one window open to edit the file that you are working on and to have a second window open to the directory of that file, to compile and run the program as you are debugging it. In this way, one can make quick changes to a file and test them. Also, long lists of errors for gcc can be viewed side by side with the code.

The up, down and tab keys

In (most) Linux shells, the up key can be used to display the previous command that you ran. Pressing the up key multiple times will go back further and further into less recent commands. Pressing the down key will display more recent commands. This feature can be used to repeat commands without having to completely retype them.

The tab key is an "auto-complete" key. That is, if you partially type a filename or command, pressing tab will complete that filename, if possible, or suggest completions. For example,
cd ./myVe [tab]
can be a short way of typing:
cd ./myVeryLongFolderName

The "Indent" Command

The indent command will attempt to organize visual appearance of your code with proper indentation. This can be useful to visually see you code structure and find problems. For example if you meant '}' but typed '{' , you will get many strange errors, especially if your code is large. Using this can help you find such mistakes:
indent myCode.c

The Remove Command

First of all, be careful with this command. It will do what you tell it to. If you want to remove an individual file, do this:
rm aFile
If you want to remove a folder and everything in it, do this:
rm -r aFolder
If you want to remove a whole bunch of files, this will remove all files in the current folder beginning with filePrefix:
rm filePrefix*
This will remove all files in the current folder ending with .c:
rm *.c
This will attempt to erase all files and folders in the current folder, if you have permission.
Don't do this:
rm -rf *
Again, don't do this, ever.

Logging Out

Once you are done, type "exit" at the terminal to log out of a terminal. Then, use the graphical desktop menus to log out of the machine.

The lab management requests, upon logging out of the machines, that you do not turn them off.

Summary Cheat-Sheet:

Console Commands:

manmanual, explains other commands
lslist, lists the contents of current folder
cdchanges the current folder
cd /Go back to root folder
cd ..Go back to containing folder (up a level)
cd ~Go back to home folder
catoutput a file to console
moreread a file at the console
mkdirmake a folder
cpcopy a file
mvmove/rename a file
rmdelete stuff (USE WITH CAUTION)
nanoa simple file editor
gccC Compiler
sshremote login
lnmakes links between files (i.e. shortcuts)
exitclose the console/remote connection

Useful Key Combo:

Control-c: Will (usually) kill the terminal program that you are in. Use for runaway/broken programs that won't stop.

Useful Websites:

http://cplusplus.com/reference/clibrary/ - C library reference
http://sunsite.utk.edu/UNIX-help/quickref.html - UNIX reference
Hint: That first website is a great place to find info on printf and scanf, two functions that you might want for this lab.

Advice to avoid common mistakes/pitfalls: