The UNIX operating system is concerned with managing resources on the computer. It is in control of things like memory, access to the CPU and any peripherals like disks, the keyboard, the display and so on. The core processing part of the operating system is called the kernel. It does not contains a user interface of any sort. So people wrote programs called shells that act as an interpreter between the user and the operating system. There are a number of different shells but all are just programs written by users and have no special connection to the operating system.
When using a shell, a user types commands along with arguments and the shell looks up the commands and runs them. Each command is a program that is also not part of the operating system although it may use library routines from the kernel to do the work. Arguments to a command are typed on the same line after the command name. The main arguments are just typed in while others, called options are preceded by dashes. We will see some examples of this shortly.
The UNIX file system is organized like a tree. This is the same as the Windows file systems, at least as far as it looks. They are implemented differently. The top of the tree is, oddly, called the root. In Windows, this is represented by the drive letter, for example, C: In UNIX, this is the /(forward slash) character.
All files and directories (folders) in UNIX on a given disk are under the root directory. There are a number of standard directories under root on most UNIX systems. Here are some examples:
All files in UNIX are represented the same way. Each is long collection of bytes. The operating system sees all files this way. To it, picture files, documents, programs and music are all the same. Different applications will see them differently but the OS just sees bytes.
As you move around the file system, it helps to think of directories as places you can go. In fact, most UNIX users refer to entering a directory as 'going into' that directory. At any time, the directory you are in is known as the current directory. Or sometimes as the working directory.
Having said they can contain any characters, it is unwise to use the Windows practice of using spaces in the names. Most UNIX commands use the space as the separator between arguments and will get confused by file or directory names with spaces in them.
The two most common commands for maneuvering around the file system are cd, change directory and ls, list. The cd command takes one argument which is the name of a directory to go to. If it is used without arguments, it takes you back to your home directory. the ls command lists the files and directories in the current directory, if it is not given a directory name.
Use the ls command without arguments to show the files and directories in the current directory.
--> ls asgs examples index.html nav.js sched.html teach.css bcard.html images links.html notes syll.html typescript
If a UNIX file has a period (.) in front of its name, then it becomes invisible to the ls command. This is so the view isn't cluttered up with little configuration files. Each directory has in it two special files. The first is simply called dot(.). This file refers to the current directory. So using the command ls . shows the same directory as just ls.
The second one is called dotdot (..) and refers to the directory just above this one. That is, the parent directory. This is handy when moving around as a command like cd .. will change to the parent directory of whatever directory you are in. You don't have to know the name of the directories to refer to them. The -a option will show these files.
--> ls -a . asgs examples index.html nav.js sched.html teach.css .. bcard.html images links.html notes syll.html typescript
In UNIX, directories are really just files that have special information in them. So when using ls, they look just like other files. Some versions of the shell use different colors to show the difference but if that isn't used, you can use the -F option to show the directories by putting a / after the name of the directories. This doesn't change the directories name, just how they are shown.
--> ls -aF ./ asgs/ examples/ index.html nav.js sched.html teach.css ../ bcard.html images/ links.html notes/ syll.html typescript
Notice how we combined the -a and -F options as -aF.
To see where we are, we can use the pwd (Print Working Directory) command.
--> pwd /home/kent/comp217/web
Now let's move to the parent directory of this one.
--> cd ..
As mentioned above, this takes us up one level in the file system. We'll use pwd again to verify this.
--> pwd /home/kent/comp217
Ls will show the files at this level. Notice the web listing, that is the directory we were just in.
--> ls bookstuff code labs other students web
To go back to the directory we came from, we could use the cd command like this:
cd web
But there is a simpler way where we don't have to remember the name of the directory.
Sometimes, when you are moving all over the system, it is easy to forget.
--> cd - /home/kent/comp217/web
This took us back to the web directory.
To list the files in a directory, you just have to put the name of the directory on the end of the ls command.
--> ls asgs index.html labsheet.html
The -l option on ls shows us more detail about the file.
--> ls -l index.html -rw-r--r-- 1 kent root 453 Mar 10 12:37 index.html
There are several sections to this listing, here is a short explanation of each.
-rw-r--r-- |
These are file permissions.
Each character is important.
The first, tells whether this is a file or directory.
If it is a '-', then this is a file.
If its is a 'd', then it is a directory.
The rest of the characters are broken into three groups of three.
Each group represents what kind of access is allowed for different groups of users. The first group is for the owner of the file, the second is for members of a group and the last is for people who are not the owner nor are part of the group, that is, everybody else.
Within each group, there are three letters.
The first letter is either a 'r' or a '-'.
If it is 'r', then the group has read permissions for the file.
If it is a '-', then they cannot read it. The second group, for group members, is here 'r--'. This means that group members can only read the file, not write it. The last group, for everybody else is 'r--'. So they also can only read the file. Any combination of these permissions can be set using the chmod (change mode) command. |
---|---|
1 kent root |
This section has three separate pieces of information. The 1 is the number of links to this file. Don't worry about links for now. The 'kent' is the owner of the file. The 'root' is the group it is part of. Don't worry about groups either. |
453 Mar 10 12:37 index.html |
Here are three more things. The 453 is the size of the file in bytes. The date is the date it was last changed or created. And the last is the name of the file. |
Often, you want to look for a certain file or kind of files. You can use the ls command to list the files that match a pattern. The simplest, and most common pattern is to match either the first or last part of a file. So, if we only want to see the html files in a directory
--> ls *.html bcard.html index.html links.html sched.html syll.html
The '*' (asterisk), usually pronounced 'star', means match any number of any characters. So any file that ends in '.html' will match.
This can work from the front as well, if you want all the files that start with 's', do this
--> ls -l s* -rw-r--r-- 1 kent root 5489 Mar 10 12:37 sched.html -rw-r--r-- 1 kent root 2881 Mar 10 12:37 syll.html
Let's try copying, renaming and deleting files. First, we go into the examples directory and list the contents.
--> cd examples --> ls ARRAY1.ZIP functions.zip library.zip pizza.zip structures.zip REFPARAM.ZIP index.html library2.zip shelves.zip tempclass.zip enumswitch.zip inherit1.zip pex1.cc strings.zip
To copy a file, we use the cp command. The first argument is the file you want to copy and the second is where you want to copy it. If the second argument is a directory, then the first is copied to that directory with the same name.
--> cp index.html index2.html --> ls ARRAY1.ZIP functions.zip inherit1.zip pex1.cc strings.zip REFPARAM.ZIP index.html library.zip pizza.zip structures.zip enumswitch.zip index2.html library2.zip shelves.zip tempclass.zip
Now we list the files starting with 'index'.
--> ls -l index* -rw-r--r-- 1 kent root 1315 Mar 10 12:37 index.html -rw-r--r-- 1 root root 1315 Mar 10 12:44 index2.html
In addition to copying, we can move files. One way use move is to rename a file. This could also involve moving it to another directory. Here we move the file we just copied to a name. Like cp, the first argument is a file or directory that you want to move and the second is where it goes.
--> mv index2.html index3.html --> ls -l index* -rw-r--r-- 1 kent root 1315 Mar 10 12:37 index.html -rw-r--r-- 1 root root 1315 Mar 10 12:44 index3.html
Notice that index2.html is gone. It has been renamed as index3.html. Removing a file is just as easy. The rm takes a list of the files you want to remove.
--> rm index3.html --> ls -l index* -rw-r--r-- 1 kent root 1315 Mar 10 12:37 index.html
See that index3.html is gone. Also note that it doesn't ask if you want to remove the file, it just does it.
To look at the contents of a file, we use the oddly name cat command. The real purpose of this command is to concatenate files. If you use it with just a file name, it concatenates the file onto the standard output, that is, the screen.
--> cat index.html <DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <script LANGUAGE="JavaScript" src="../nav.js"><head> <body> <script LANGUAGE="JavaScript"> navup(1); <h1 align="center">Examples Pizza problem code String object examples Updated Library example Composition example Inheritance example Tiny Inheritance example First pointer code --> In class 2D array example Collection of function examples reference parameter example Structures switch and enum example simple class example navdown(1); footer();
In UNIX, the output from commands goes, by default, to the screen. This is known as the standard output. We can change where the output goes by using re-direction. This is indicated by using the '>' operator. Putting a '>' after a command sends the output to somewhere else. Usually, a file name is put after the '>' and this is where the output goes.
--> cat index.html pex1.cc > both
No output appeared on the screen, it all went into the file both. What cat did in this case is first to write the file index.html followed by pex1.cc. So if we display the file both using cat like this, we will see the contents of both files.
--> cat bothExamples
Pizza problem code
String object examples
Updated Library example
Composition example
Inheritance example
Tiny Inheritance example
In class 2D array example
Collection of function examples
reference parameter example
Structures
switch and enum example
simple class example #include "stdafx.h" #includeint main(int argc, char* argv[]) { int *ipointer,itarget=42; double *fpointer,ftarget=2.4; char *cpointer, ctarget='c'; // initialize pointers ipointer = &itarget; fpointer = &ftarget; cpointer = &ctarget; cout << "contents of ipointer is " << ipointer << endl; cout << "contents of fpointer is " << fpointer << endl; cout << "contents of cpointer is " << cpointer << endl; cout << "The size of ipointer is " << sizeof ipointer << endl; cout << "The size of itarget is " << sizeof itarget << endl; cout << "The size of fpointer is " << sizeof fpointer << endl; cout << "The size of ftarget is " << sizeof ftarget << endl; cout << "The size of cpointer is " << sizeof cpointer << endl; cout << "The size of ctarget is " << sizeof ctarget << endl; cout << "The value in itarget is " << itarget << " using the pointer it is " << *ipointer << endl; cout << "The value in ftarget is " << ftarget << " using the pointer it is " << *fpointer << endl; cout << "The value in ctarget is " << ctarget << " using the pointer it is " << *cpointer << endl; return 0; } // main
Let's look at the sizes of the files concerned.
--> ls -l index.html pex1.cc both -rw-r--r-- 1 root root 2520 Mar 10 12:47 both -rw-r--r-- 1 kent root 1315 Mar 10 12:37 index.html -rw-r--r-- 1 kent root 1205 Mar 10 12:37 pex1.cc
If you want to print a short message on the screen, you can use the echo command.
--> echo "this is a note" this is a note
The UNIX shell has variables as well as commands. Some of them are built in and other are made by users. One example is the home variable that contains the name of the users home directory.
--> echo $HOME /root
If you want to know you own login name, you can use the id command. This is more useful than it sounds as sometimes you change your login during a session and can forget who you are. Here we first print the id we are using now and then we log in as someone else.
--> id uid=0(root) gid=0(root) groups=0(root) --> su - kent kent@moosebase:~$ id uid=1000(kent) gid=1000(kent) groups=1000(kent),24(cdrom) kent@moosebase:~$ pwd /home/kent kent@moosebase:~$ echo $HOME /home/kent
The PATH variable is used to help find commands. When you try to run a command, the shell searches for it in different places. The list of places is contained in the PATH variable. It is a list of directories separated by ':' (colon).
kent@moosebase:~$ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
In this case, the shell first looks in /usr/local/bin for a command and if it doesn't find it there it looks in /usr/bin and so on until it either finds the command or doesn't in which case it prints an error message.