UNIX is an operating system. The job of an operating system is to orchestrate the various parts of the computer to perform useful tasks. The operating system is the master controller of the computer, the glue that holds together all the components of the system, including the administrators, programmers, and users. When you want the computer to do something for you, like start a program, copy a file, or display the contents of a directory, it is the operating system that must perform those tasks for you.
UNIX was created in the late 1960s, in an effort to provide a multiuser, multitasking system for use by programmers. The philosophy behind the design of UNIX was to provide simple, yet powerful utilities that could be pieced together in a flexible manner to perform a wide variety of tasks.
The UNIX operating system comprises three parts: The kernel, the standard utility programs, and the system configuration files.
The kernel is the core of the UNIX operating system. Basically, the kernel is a large program that is loaded into memory when the machine is turned on, and it controls the allocation of hardware resources from that point forward.
These programs include simple utilities like cp, which copies files, and complex utilities, like the shell that allows you to issue commands to the operating system.
The system configuration files are read by the kernel, and some of the standard utilities. The UNIX kernel and the utilities are flexible programs, and certain aspects of their behavior can be controlled by changing the standard configuration files. One example of a system configuration file is the filesystem table "fstab" , which tells the kernel where to find all the files on the disk drives. Another example is the system log configuration file "syslog.conf", which tells the kernel how to record the various kinds of events and errors it may encounter.
When you first login, your current working directory is your home directory. Your home directory has the same name as your user-name, for example gkinsella, and it is where your personal files and subdirectories are saved.
To find out what is in your home directory, type
ls does not, in fact, cause all the files in your home directory to be listed, but only those ones whose name does not begin with a dot (.) Files beginning with a dot (.) are known as hidden files and usually contain important program configuration information. They are hidden because you should not change them unless you are familiar with UNIX!!!
To list all files in your home directory including those whose names begin with a dot, type
ls is an example of a command which can take options: -a is an example of an option. The options change the behaviour of the command. There are online manual pages that tell you which options a particular command can take, and how each option modifies the behaviour of the command. (See later in this tutorial)
To make a subdirectory called stuff in your current working directory type
To see the directory you have just created, type
The command cd directory means change the current working directory to "directory". The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree.
To change to the directory you have just made, type
Type ls to see the contents (which should be empty)
Make another directory inside the stuff directory called backups
Still in the stuff directory, type
As you can see, in the stuff directory (and in all other directories), there are two directories called . and ..
In UNIX, . means the current directory, so typing
Note: there is a space between cd and the dot means stay where you are (the stuff directory).
This may not seem very useful at first, but using . as the name of the current directory will save a lot of typing, as we shall see later in the tutorial.
.. means the parent of the current directory, so typing
will take you one directory up the hierarchy (back to your home directory). Try it now.
Note: typing cd with no argument always returns you to your home directory. This is very useful if you are lost in the file system.
Pathnames enable you to work out where you are in relation to the whole file-system. For example, to find out the absolute pathname of your current-directory type:
The full pathname will look something like this:
Use the commands ls, pwd and cd to explore the file system.
(Remember, if you get lost, type cd by itself to return to your home-directory)First type cd to get back to your home-directory, then type
to list the contents of your stuff directory. Now type
You will get a message like this:
The reason is that backups is not in your current working directory. To use a command on a file (or directory) not in the current working directory (the directory you are currently in), you must either cd to the correct directory, or specify its full pathname. To list the contents of your backups directory, you must type
Home directories can also be referred to by the tilde ~ character. It can be used to specify paths starting at your home directory. So typing
will list the contents of your stuff directory, no matter where you currently are in the file system.
If you are not familiar with a Unix editor, please look at the Emacs tutorial or Vi tutorial.
Generate a file (file1) with a few lines of text.
cp file1 file2 is the command which makes a copy of file1 in the current working directory and calls it file2
mv file1 file2 moves (or renames) file1 to file2
To move a file from one place to another, use the mv command. This has the effect of moving rather than copying the file, so you end up with only one file rather than two.
It can also be used to rename a file, by moving the file to the same directory, but giving it a different name.
To delete (remove) a file, use the rm command.
Create a directory called tempstuff using mkdir , then remove it using the rmdir command.
Before you start the next section, you may like to clear the terminal window of the previous commands so the output of the following commands can be clearly understood.
At the prompt, type
This will clear all text and leave you with the % prompt at the top of the window.
The command cat can be used to display the contents of a file on the screen. Type:
The command less writes the contents of a file onto the screen a page at a time. Type
Press the [space-bar] if you want to see another page, type q if you want to quit reading. As you can see, less is used in preference to cat for long files.
The head command writes the first ten lines of a file to the screen.
First clear the screen then type
Then type
What difference did the -5 do to the head command?
The tail command writes the last ten lines of a file to the screen.
Clear the screen and type
How can you view the last 15 lines of the file?
A handy little utility is the wc command, short for "word count". To do a word count on your file type
To find out how many lines the file has, type
We use the > symbol to redirect the output of a command. For example, to create a file called list1 containing a list of fruit, type
Then type in the names of some fruit. Press Return after each one.
orange
banana
apple
^D (Control D to stop)
What happens is the cat command reads the standard input (the keyboard) and the > redirects the output, which normally goes to the screen, into a file called list1
To read the contents of the file, type
Using the above method, create another file called list2 containing the following fruit: melon, plum, mango, grapefruit. Read the contents of list2
The form >> appends standard output to a file. So to add more items to the file list1, type
Then type in the names of more fruit
peach
grape
orange
^D (Control D to stop)
To read the contents of the file, type
You should now have two files. One contains six fruit, the other contains four fruit. We will now use the cat command to join (concatenate) list1 and list2 into a new file called biglist. Type
What this is doing is reading the contents of list1 and list2 in turn, then outputing the text to the file biglist
To read the contents of the new file, type
We use the < symbol to redirect the input of a command.
The command sort alphabetically or numerically sorts a list. Type
Then type in the names of some vegetables. Press Return after each one.
carrot
turnip
broccoli
^D (control d to stop)
The output will be
broccoli
carrot
turnip
Using < you can redirect the input to come from a file rather than the keyboard. For example, to sort the list of fruit, type
and the sorted list will be output to the screen. To output the sorted list to a file, type,
Use cat to read the contents of the file slist
To see who is on the system with you, type
One method to get a sorted list of names is to type,
This is a bit slow and you have to remember to remove the temporary file called names when you have finished. What you really want to do is connect the output of the who command directly to the input of the sort command. This is exactly what pipes do. The symbol for a pipe is the vertical bar |
For example, typing
will give the same result as above, but quicker and cleaner. To find out how many users are logged on, type
The character * is called a wildcard, and will match against none or more character(s) in a file (or directory) name. For example, in your stuff directory, type
This will list all files in the current directory starting with list... Try typing
This will list all files in the current directory ending with ...list. The character ? will match exactly one character. So ls ?ouse will match files like house and mouse, but not grouse. Try typing
We should note here that a directory is merely a special type of file. So the rules and conventions for naming files apply also to directories.
In naming files, characters with special meanings such as / * & % , should be avoided. Also, avoid using spaces within names. The safest way to name a file is to use only alphanumeric characters, that is, letters and numbers, together with _ (underscore) and . (dot).
File names conventionally start with a lower-case letter, and may end with a dot followed by a group of letters indicating the contents of the file. For example, all files consisting of C code may be named with the ending .c, for example, prog1.c . Then in order to list all files containing C code in your home directory, you need only type ls *.c in that directory.
Beware: some applications give the same name to all the output files they generate.
For example, some compilers, unless given the appropriate option, produce compiled files named a.out. Should you forget to use that option, you are advised to rename the compiled file immediately, otherwise the next such file will overwrite it and it will be lost.
There are on-line manuals which gives information about most commands. The manual pages tell you which options a particular command can take, and how each option modifies the behaviour of the command. Type man command to read the manual page for a particular command. For example, to find out more about the wc (word count) command, type
Alternatively
gives a one-line description of the command, but omits any information about options etc.
In your unixstuff directory, type
You will see that you now get lots of details about the contents of your directory, similar to the example below.
Each file (and directory) has associated access rights, which may be found by typing ls -l (-l for long listing). Also, ls -lg gives additional information as to which group owns the file (ichec in the following example):
In the left-hand column is a 10 symbol string consisting of the symbols d, r, w, x, - and occasionally s or S. If d is present, it will be at the left hand end of the string, and indicates a directory: otherwise - will be the starting symbol of the string.
The 9 remaining symbols indicate the permissions, or access rights, and are taken as three groups of 3.
The symbols r, w, etc. have slightly different meanings depending on whether they refer to a simple file or to a directory.
So, in order to read a file, you must have execute permission on the directory containing that file, and hence on any directory containing that directory as a subdirectory, and so on, up the tree.
| -rwxrwxrwx | a file that everyone can read, write and execute (and delete). |
| -rw------- | a file that only the owner can read and write - no-one else can read or write and no-one has execution rights (e.g. your mailbox file). |
Only the owner of a file can use chmod to change the permissions of a file. The options of chmod are as follows:
| Symbol | Meaning |
|---|---|
| u | user |
| g | group |
| o | other |
| a | all |
| r | read |
| w | write (and delete) |
| x | execute (and access directory) |
| + | add permission |
| - | take away permission |
For example, to remove read write and execute permissions on the file biglist for the group and others, type
This will leave the other permissions unaffected. To give read and write permissions on the file biglist to all,
Try changing access permissions on the file file1 and on the directory backups.
Use ls -l to check that the permissions have changed.
| ls | list files and directories |
| ls -a | list all files and directories |
| mkdir | make a directory |
| cd directory | change to named directory |
| cd | change to home-directory |
| cd ~ | change to home-directory |
| cd .. | change to parent directory |
| pwd | display the path of the current directory |
| cp file1 file2 | copy file1 and call it file2 |
| mv file1 file2 | move or rename file1 to file2 |
| rm file | remove a file |
| rmdir directory | remove a directory |
| cat file | display a file |
| more file | display a file a page at a time |
| head file | display the first few lines of a file |
| tail file | display the last few lines of a file |
| wc file | count number of lines/words/characters in file |
| command > file | redirect standard output to a file |
| command >> file | append standard output to a file |
| command < file | redirect standard input from a file |
| command1 | command2 | pipe the output of command1 to the input of command2 |
| cat file1 file2 > file0 | concatenate file1 and file2 to file0 |
| sort | sort data |
| who | list users currently logged in |
| * | match any number of characters |
| ? | match one character |
| man command | read the online manual page for a command |
| whatis command | brief description of a command |