You should use a csgrad or similar system to complete this tutorial. Follow the instructions on the PuTTY tutorial to login to csgradxx.nwmissouri.edu, where xx is your server number, using the secure shell protocol (ssh).
This tutorial includes both reading material and exercises.
Statements such as "Enter the following command..." or "Type..."
denote that you should type an appropriate response at the command
line to get a response from the operating system. UNIX commands
will be in bold font. You may write or type answers in the spaces
provided so that you can keep the results for your future use.
From the command line it is often necessary to use a text editor. nano is a simple text editor that you will learn to use in this tutorial.
After opening a terminal, you will see some text followed by a $ (dollar prompt) and a cursor. You can type commands at this prompt and run them by hitting the enter/return key.
At the $ prompt, run the command nano practice.txt
What happens?
_____________________________________________________
___________________________________________________________________
Using nano, you can type in text, move the cursor using the arrow keys, delete characters using the backspace key, and interact with text using the controls shown at the bottom of the page. These controls are in the format ^character, meaning that you would press and hold the Control key and press a character key to run a command. For example, ^C (Control-C) shows the current cursor position.
Press ^C, what happens? _____________________________________
Type the following text into the editor, only typing enter
whenever you see the <cr> symbol:
The nano editor is a simple text
editor that is typically available in linux and <cr>
UNIX. <cr> <cr>
You can move the cursor around using the arrow keys.<cr>
Delete a line.<cr>
Put a line here.<cr>
The controls are listed at the bottom of the page.
Saving your work
Before going any further it is best you learn to save your work.
Save your work by pressing ^O
(Control-O) and verifying the file name (typically by hitting
enter).
Trying out nano
Press ^C (Control-C). What
happens? ________________________________________________
Press ^V (Control-V). What
happens? ________________________________________________
Press ^Y (Control-Y). What
happens? ________________________________________________
Press ^W (Control-W). What
happens? ________________________________________________
Search for the word Delete
then press ^K (Control-K).
What happens? ________________________________________________
Move the cursor below the line containing the text "Put a line
here." Press ^U
(Control-U). What happens?
________________________________________________
You now know many of the commands nano has to offer. ^C (Control-C) allows you to see
the current cursor position. ^V
(Control-V) and ^Y
(Control-Y) let you page-up and page-down. To search for a
particular word in your code, you can use ^W (Control-W). Finally, to cut and paste
lines of text, you can use ^K
(Control-K) and ^U
(Control-U).
^G (Control-G) will allow
you to access the help file for nano to learn more.
Press ^X (Control-X) to
exit nano making sure to save your file.
You should now be back at the $ prompt.
The ls command allows you
to view the files within the current directory.
Learning UNIX Commands
The first UNIX command you will learn is the man command. This command will
allow you to look up the manual (also called man pages) for a
given command.
Try out this command by typing man
nano and then pressing enter.
What happens? _________________________________________
The up and down arrows will let you scroll through the manual. You
can scroll more quickly by using the space bar (to scroll down a
page) and the character b
(to scroll up a page). You will need to type the character q to return to the command
prompt from the manual.
Now you can try out some more commands at the $ prompt. Generally, to run a command, you will need to press enter after typing the command. Most further references to pressing enter will be omitted.
The who command displays who is logged in. What is output when you run this command?_______________________________
The cat command allows you to concatenate and print (output) files. Explain what is output when you run cat practice.txt.
The man command allows you to learn the details (manual pages) of Unix commands. This will be a very important tool since you will be learning about many commands. Documentation on a command can be found by typing the following:
man <command>
where <command> is the name of the command.
To try out the man command, issue the command:
man who
What happens?_______________________________________
To navigate the manual, use the space bar to page down and the character b to page up. Type the character q to exit the manual.
Use nano to create three new files named filea, fileb, and filec. Type one sentence into each of these files. For example, you could put the sentence This is filea. in filea and so on.
Test the cat command on each of these files. Now issue the following commands:
cat filea; cat
fileb; cat filec
cat filea fileb filec
Explain what the commands above accomplished. ________________________________________________
Wildcard Characters
Wildcard characters allow you to abbreviate file names using the * or ? characters. These symbols represent any combination of zero or more characters and any one character respectively. Run the commands below.
cat file*
cat file?
cat fi*
cat fi?
What is the difference between the results of the commands above?
________________________________________________________________
________________________________________________________________
Output Redirection
Output redirection allows you to send output provided by a command to a file. The > character allows for output redirection. Run the following command:
cat file? > threefiles
Now run the following command:
cat threefiles
What happened?___________________________________________________
Directory Listings
The ls command allows you to view the contents of the current directory. Run the command:
ls
What was listed on the screen?______________________________________
Options may be provided with the ls command and with many other commands. Two common options are -a and -l. You can combine these options as -al. Run the following commands:
ls -a
ls -l
ls -al
List at least two lines from the output of ls -al.
_____________________________________________________________________
_____________________________________________________________________
Notice that the -l option generates what is referred to as a "long listing", and the -a option lists "invisible" files. When combined, the two options list all files, including invisible files, with a long listing.
Notice that each line of the directory listing looks similar to the following line:
drwxr-xr-x 12 root
knoppix 408 Aug 25 00:51 MPI_exercises
-rw-r--r-- 1 root knoppix 14 Aug 26 21:26 filea
The lines in the directory listing above contain detailed information about each file. The columns have specific meanings. The first refers to access permissions and file type. The second is the number of links. The third is the owner's username. The fourth is the group username. The fifth is the file size in bytes. The sixth is the date and time of the last modification, and the seventh is the file name.
Access permissions consist of a directory designator (first character) and three groups of three permission categories. If the directory designator is a "d", the file is a directory. A "-" for the directory designator indicates a plain file.
drwxrwxrwx
indicates a directory
-rwxrwxrwx
indicates a file
Read, write, and execute permissions are indicated by the letters r, w, and x, respectively. A "-" indicates no access for that option in that category. Permission categories are user, group, and world, from left to write. So, the access permissions for the following file are read, write, and execute for the user, read and execute for the group, and read for the world (everyone).
-rwxr-xr--
How many invisible files are in the current directory? Files?
Directories?
_____________________________________________________________________
Copy command
The cp command allows you to copy one file to another and to copy one or more files to a directory. The source file is listed first, and the destination file is listed second. Run the command:
cp practice.txt practiceCopy.txt
Then run the commands:
cat practice.txt
cat
practiceCopy.txt
ls
What happened? ____________________________________________
Move command
The mv command allows you to rename and move one or more files to another directory location. The source file is listed first, and the destination file is listed second. Run the command:
mv practiceCopy.txt newPracticeCopy.txt
Then run the commands:
cat
practiceCopy.txt
cat
newPracticeCopy.txt
ls
What happened? ____________________________________________
Remove command
The rm command allows you to remove a file, remove a list of named files, or, by using wildcards, remove groups of files. Run the command:
rm newPracticeCopy.txt
Then run the commands:
cat newPracticeCopy.txtWhat happened? ____________________________________________
Working with Directories
It is assumed that you understand the concept of a directory and a file. UNIX and Linux directories are stored as a tree with the root of the tree specified as /. The root directory is specified as / and each user has a directory under /home, called the home directory. Typically, the user's home directory is specified as the username. For example, a user with the username, "bob", would typically have his home directory under /home/bob.
File and directory names follow some rules. UNIX file names only contain the following characters:
Uppercase characters (A - Z)
Lowercase characters (a - z)
Numerals (0 - 9)
Period (.), Underscore (_), and Comma (,)
Filenames should not contain any special characters!
Present Working Directory
The pwd command shows the present working directory. This is the directory you are in. Run the command:
pwd
What was output? ______________________________________________
Working with directories
The mkdir command allows you to create a directory. Create a directory by running the following command:
mkdir myNewDirectory
List the directory contents. What happened? ____________________________
To change to a different directory, use the cd command. Change to your new directory using the following command:
cd myNewDirectory
Run the command:
ls -al
What was output? ______________________________________________________
Run the following commands:
pwd
cd ..
pwd
ls
What happened? What did cd ..
do?
______________________________________________________________________________
______________________________________________________________________________
Notice that cd .. changes to the parent directory of the current directory. If you ever need to get back to your home directory you can use the command cd ~ to get back there.
Removing directories
Use rm -R to remove a directory.
rm -R myNewDirectory
What happened?
__________________________________________________________________________
Writing a C Program
Now that you have learned some basic commands and how to use a
simple text editor, you should be able to write a simple C
program.
Return to your home directory by typing cd ~ at the $
prompt, and open a file called HelloWorld.c
by typing:
nano HelloWorld.c
Once the text editor is open, type in the following program.
#include <stdio.h>
int main(int argc, char ** argv)
{
printf("Hello, world!\n");
return 0;
}
After you have typed in the program, save and exit from nano using Ctrl-X
At the $ prompt, type the
following command:
gcc HelloWorld.c -o HelloWorld.exe
The gcc compiler is a
free, open source C compiler. In the command above, you
specified the file to be compiled, and the output, or executable,
file with the -o option.
Provided you typed in the file correctly, you should immediately
return to the $ prompt
with no output. The compiler should output errors if there
are errors in your program.
Type the ls command.
Do you see your executable file? _______ Why?________________________________