TechTutorials - Free Computer Tutorials  







Introduction to Unix 
 


Added: 06/01/2000, Hits: 5,714, Rating: 0, Comments: 0, Votes: 0
Add To Favorites | Comment on this article
Intro
Brief History
Features
Shells
Directory Structure
Working With Files
File Access
Customization
Basic Admistration
Utilities

Intro
First of all, if you are looking for one of those "Unix is so much better than Windows" discussions, you have come to the wrong place. If you are a true geek then you should have the ability to recognize that each have their strengths and weaknesses. (I carefully get off of my soapbox now). This tutorial is intended to give you a brief overview of the Unix/Linux OS and give you some of the skills that you will need to perform basic functions. If you begin to use Unix you will find that it is more intimidating than it is difficult, so don't be afraid to get your feet wet. You will find that for every difference, there are similarities to the operating systems that you currently use. Just think of it as DOS on crack.

Brief History
The Unix operating system was developed in the late '60s by Bell Labs and has gained in popularity ever since. In recent years, much of its success can be attributed to the inception and growth of the internet as Unix is often used as a web server by ISPs and webhosts. It is often the OS of choice for these applications as it is very adaptable, robust and has great built-in networking software. The term Unix is somewhat of a misnomer nowadays as many different manufacturers have created their own "flavors" of Unix. The term Unix is generally used as an umbrella term for all of the implementations of Unix that have been created by Sun, IBM, BSD, etc. More recently, a flavor called Linux has been developed and it is doing quite well. It is free, open source and very powerful.

Features
Some of the major features of Unix operating systems:
  • True pre-emptive multi-tasking - Essentially this allows multiple applications to use the processor at the same time. Provides "read aheads" and "write behinds".

  • Multi-user capability - Allows multiple users to simultaneously log on and run applications.

  • Multi-processor support - Supports the installation of multiple processors.

  • Platform support - Supports multiple hardware platforms.

  • Scalability - Supports very large numbers of concurrent users.

  • Stability - Extremely stable and rarely needs rebooting.

  • Security - They have had the benefit of time to get it right.


There are obviously many more noteworthy features, but some vary depending on the flavor of Unix and there are just too many to go into here. Those listed above are the major ones.

Shells
What the hell is a shell? A shell is a user interface which acts as an interpreter between the user and the Unix operating system. It is what you see on the screen. A shell performs 2 basic functions - It acts as a command interpreter and as a programming language. As a command interpreter it is somewhat similar to DOS. The commands that you type in are examined. If the command is one that is natively built-in, then it is executed. If it is not built in, then it is passed to the operating system to be executed. As a programming interface it allows you to process commands stored in shell scripts. This is somewhat similar to batch files in DOS. There are several different shells that you can use, the most common of which are Bourne, C, Korn and Bash. An administrator can assign the default shell.
  • Bourne shell -The Bourne shell is activated by typing sh and is represented by a $ prompt. This was one of the first shells and still one of the most popular.

  • C shell - This shell resembles the C programming language and is represented by a % prompt. The command to use this shell is csh. The C shell was buggy, but had important features so people had to switch back and forth between this one and Bourne.

  • Korn shell - This is a combination of the C and Bourne shells and is represented with a $ prompt. David Korn from AT&T came along and took features of the C shell and added them to the Bourne shell to try to fix the whole mess.

  • Bash -Bash stands for "Bourne Again Shell"(clever huh?). This one came about as the Korn shell was not free and demand for a free shell began to grow. Not surpisingly, Linux jumped all over this one.


Directory Structure
From here forward, we will begin introducing many of the common commands that you will be using. If you are ever stuck working with a command or you would like to see all of the options available, type man command_name. "man" stands for manual and using this command will present you with a list of the proper syntax and usage options. UNIX utilizes a hierarchical file system with a main directory called "root". The root directory will typically contain the bin, dev, var, etc, usr, home and tmp directories. Below is a brief description of each of these directories.
  • dev - Contains device files that define the devices connected to the system.

  • bin - Bin stands for binary and contains the most commonly used utilities and programs required to run the system.

  • etc - This directory is mainly for system administrators and contains user profiles and configuration files.

  • usr - Development related libraries, manuals and executables.

  • tmp - Temporary storage for files created by utilites that exist until the computer is rebooted.

  • var - Directory for variable data files such as spool directories and files, administrative and logging data, and temporary files.

  • home - Home directories for users. Every process has a current directory including the login shell for a user. This is where the login shell looks for its start-up files.


Navigation in UNIX is very similar to DOS and even uses some of the same commands. To start off, you will probably want to know which directory you are currently in. You can do this by typing pwd(print working directory) at the prompt. Let's say that you want to change to a different directory - type cd(change directory) followed by the directory you would like to move to.
EXAMPLE: cd /home/jasonsp

Now you would like to get out of the jasonsp directory and go to the parent /home directory. Simply type cd ..
Now let's say that you would like to get to the root directory. Type cd /. This command will take you to root from wherever you are currently located. Now you are in root and you would like to view the contents of this directory. Type ls at the prompt and you will be shown a list of files and directories that live within the directory that you are currently in. Typing ls -l will display additional attributes about each file and directory including permissions, file type, size, owner, date and date last modified. You now know how to get around the UNIX directory structure. Now it is time to learn how to manipulate it.

In order to create a new directory, you will use the mkdir command followed by the path to the directory and the directory name.


EXAMPLE: mkdir /home/jasonsp/newfolder

This would create a directory called "newfolder" in the /home/jasonsp directory. Now you wish to remove "newfolder" - Type rmdir newfolder. The rmdir command will only delete an empty directory, so if the newfolder directory contained files, you would want to use rm -r newfolder. The directory and all of its contents will be removed.

Working With Files
This section will mainly consist of the variety of commands that will allow you to work with files. First, we need a file to work with. To create a new ascii(text) file called junk, type cat > junk. You will then see a blinking cursor appear on the screen. You are now free to type the contents of your file. When you are finished, press ctrl-d to save and close the file. The file will be saved into the directory that you are currently located in. NOTE: There are many other text editors available to UNIX systems that provide more options and features than the cat command such as VI and Echo. If you would like to view the contents of a file, simply type cat filename. Another great tool for diplaying files is the more command, especially when working with files that are more than 1 page long as it allows you to view the file 1 page at a time.

EXAMPLE: more /etc/hosts

This would allow you you to view the hosts file from wherever you are in the directory structure because we specified the path to the file. If you were already in the /etc directory you would only have to type more hosts. If the file is more than one page long then you would be able to scroll through the document by pressing any key to view the next page. Back to our new file "junk" that we created. Now you would like to make a copy of your new file for backup reasons. For this we use the cp and specify the current filename followed by the name of the copy that is being created.

EXAMPLE: cp junk junk.bak

Now we want to move the junk.bak file to our backup directory which is located in the /home/jasonsp/backup directory. For this we would use the mv command.

EXAMPLE: mv junk.bak /home/jasonsp/backup

NOTE: Make sure that there is not a file with the same filename in the directory that you are copying the file to or else it will be overwritten. What else can we do with the mv command? Well, we can also use it to rename files. The following example shows how we would rename junk.bak to crud.bak.

EXAMPLE: mv junk.bak crud.bak

Now we no longer need the crud.bak file so we would like to delete it. For this we use the rm command. We introduced this command in the previous section and showed how rm -r directory_name would remove a directory and all of its contents. It is also used to remove individual files as follows:

EXAMPLE: rm crud.bak

And now crud.bak is a memory. We can also use the rm to remove multiple files at once as follows:

EXAMPLE: rm crud.bak crud1.bak crud2.bak

A year later and now you have hundreds of files on your system and you need to find the file "junk" that we created and you have no idea where it is. Fortunately, there is the find command. This command is fairly powerful, which also make it a little cumbersome to use. The find command uses the following syntax, find path criteria action. In order to make sense of this, we need to take a look at each one of these options separately. The "path" option specifies in which directory the search will begin. For example, if you only wanted to search the contents of /home/jasonsp then you would type find /home/jasonsp. Keep in mind that this search would list everything located inside this directory as we have not yet specified "criteria" to narrow it down. The following table shows the different criteria, syntax and explains what they do.

CRITERIA OPTIONSDESCRIPTIONSYNTAX
-nameSpecifies the name of a file. Wildcards(*) can be used.-name "filename"
-userSpecifies files owned by a particular user-user "username"
-sizeDenotes the block size of the files to search. This option allows you to specify the exact number of blocks, files larger than a given number of blocks or files smaller than a certain number of blocks.-size "block_size"
-size "+greater_than_size"
-size "-less_than_size"
-mtimeSearches files according to a specified date of modification. The exact date of modification or a date within or greater than a given period can be specified.-mtime "date_modified"
-mtime "-within_?_days"
-mtime "+greater_than_?_days"
-atimeSearches files according to when they were last accessed.-atime "date_accessed"
-atime "-within_?_days"
-atime "+greater_than_?_days"


I realize that this is a little complicated so let's run through a couple of examples.

EXAMPLE:
You want to find all files that start with the letters can in the /home/jasonsp directory:
The command would be: find /home/jasonsp -name "can*"

EXAMPLE:
You want to find all files in the /etc directory that are less than 12 blocks in size and were last accessed more than 10 days ago:
The command would be: find /etc -size -12 -atime +15



Finally, there is the "action" option. There are 2 choices for action, -print and -exec. The -print option will display the full path to the files that are found. The -exec option will execute a specified command on all files or directories found in the search. The -exec option should end with the following strokes: {};

EXAMPLE:
You want to find all files in the /etc directory that were modified more than 5 days ago and delete them:
The command would be: find /etc -mtime +5 -exec rm {};

The find command will take a little practice and memorization to remember all of the different options.


FILE ACCESS
Unix controls file permissions a little differently than what you may be used to, however, there are some similarities. You will remember from earlier that when ls -l is typed at a shell prompt, the contents of the current directory will be displayed. Additionally, a series of file attributes will also be displayed because of the -l option as in the example below.



On the very left side, you will see a string of dashes and letters that may not make any sense to you - these are the permissions for each file and directory listed. The very first space will tell you the type of file that it is using 1 of 5 characters.
  • - = Ordinary file. These are ASCII(text) and Binary(Executable) files and can include data, objects, source programs, and executables.

  • d = Directory files. These are directories - not files

  • b, c, p = These are special files that are used to represent devices. Most of these files live in the /dev directory


The next 9 spaces tell you the access permissions for the 3 categories of users that UNIX provides. These are:
  • Owner - This is the user that created the file or directory

  • Group Member - The "group" is a set of Unix-users specified by the owner, who may or may not be granted special file permissions. When a new file is created, it will be assigned to the primary group that the owner belongs to.

  • Other users - All other users that are not the owner and don't belong to the group.


The following figure breaks down these permissions so that you can see which permissions belong to which group.



And now all you need to know is what the letters r,w, and x stand for.

TypeAbbreviationFor filesFor directories
readrAllows a user to view the contents of a file or to make a copy of itAllows names of files in directory to be seen.
writewAllows a user to modify a fileAllows user to create or delete files in directory
executexIndicates that a file can be run as a program or as a scriptAllows a user to go through a directory to get to files within or below it, but does not allow filenames to be seen.


Now you need to now how to change permissions. Permissions are changed using the chmod command. There are 2 different ways to use the chmod command, however, we are only going to discuss one of them here. Once you get used to the method that will be presented here, you will find that it is faster to use than the other. The syntax that you will want to use will be chmod filename xxx where the first "x" represents the permissions for the owner, the second is for the group and the third is for everyone else. Each "x" is represented with a number between 1 and 7 as shown in the table below:

NumberPermissions
0 --- None
1 --x
2 -w-
3 -wx
4 r--
5 r-x
6 rw-
7 rwx


EXAMPLE:
For the file "junk" you would like to give full control to the owner and read/execute permissions to the group and all others.
The command would be: chmod junk 755

Let's say that you are the owner of the file "junk" and you would like to make someone else the owner of this file, use the chown command. Only a Superuser and the owner of a file can change the ownership. If you would like to change the group that the file or directory belongs to, a superuser would use the chgrp command. For more information about this command, type man chgrp.

Shell Customization
As we mentioned before, when you logon to the system, you will find yourself in the home directory of the username that was used to login. For example, when I logon as jasonsp, I am taken to the /home/jasonsp directory. This is my home directory. As mentioned before, the default shell will be displayed. Shells can be customized by using variables. There are 2 basic variables that we will introduce below.

System Variables (Environment Variables)
These are set by the system some during boot sequence and some after logon. These allow one to configure certain aspects of the working environment such as users' home directories, the prompt used, etc. First let's take a look at changing your home directory by using the home variable. If I want to change my home directory to the /var/spool directory, then I would type HOME=/var/spool. Next is the PATH variable. This can be a real time saver for frequently used programs. By default Unix requires that you are in the directory where a program exists in order to execute or specify the path when calling the program. Let's say that you are at root and you wanted to execute ProgramX which is located in the /usr/games directory - You would have to either switch to that directory and call the program or type the full path to it(/usr/games/ProgramX). The PATH variable can be included in the startup file and allow you to essentially create shortcuts to executable files. More specifically, the command allows you to specify as many directories as you want to be searched when the program is called. The PS1 command allows you to customize the shell prompt. For example, if you are in a Bourne shell and you do not want the $ prompt anymore, you can type PS1=HI!. You will now see a prompt that says "HI!". These are just examples of some of the items that you can configure. For more information, check out the reading materials on the left side of this article.

User Variables(Non-Evironment Variables)
These variables are different in that they are defined by users and are not exported to other shells. This means that if you logoff or switch shells, your settings are gone. However the startup file can be modified so that the variables are defined on startup. For Bourne and Korn shells this file is the .profile file. For a C shell th files are .login and .schrc. Modifications to these files will be executed on startup.

To view a list of the variables that have been defined for your system, type env at a prompt and you will see a list of the variables that have been configured and their values. If you would like to see the value of just 1 variable, you can use the echo command as follows echo $variable_name.

EXAMPLE: echo $HOME

This would display the variable value for the home directory for the user that is currently logged in.

Basic Administration
PLEASE NOTE: This is intended to be very basic and to give you enough information to perform simple tasks. It is not intended to provide information about network security. If this is a concern, please check with an expert or find more in depth documentation.

We have already touched on some administration issues in our discussion of file permissions. Now lets look at some other tasks you may need to perform. Before making any of these changes, you will probably need to be logged in as root. To switch to root, type su root and enter the password when prompted. The first thing you will want to understand is how to change your tcp/ip settings. This involves the modification of several files located in the /etc directory. To add or change the IP address of the computer, you will want to modify the /etc/hosts file using VI or other text editor. The hosts file is also used for translating names into IP addresses and IP addresses back into names as it is in a Windows environment. You will see that the file is the same as the ones that you are used to working with. Next you may want to change the subnet mask which can be done by editing the netmasks file found at /etc/netmasks. The gateway address can be changed by modifying the /etc/defaultrouter file. BOOTP and RARP(similar to DHCP) use a file to map EtherNet addresses into IP numbers. This is provided by the ethers file located at /etc/ethers, which maps the MAC address to a hostname. Then the hosts file is used to map this into an IP address. If you place the IP address in only the hosts file and making use of RARP for assigning network numbers, you can readdress an entire network just by changing the hosts file and rebooting the machines.

Now on to managing users. User information is stored in an entry in the password database. This database is typically found at /etc/passwd and is similar to the SAM file used by NT. To add a new user, use the useradd command. The synatax is useradd username. It is not a good idea to ever remove users, for the same reasons that you are told not to do it in NT. It is better to disable the account by typing passwd -l username. To configure configure your network settings you will need to modify several files in the /etc directory or use a GUI.

Finally, we need to tell you how to manage processes. Processes in UNIX are similar to those in NT and can be stopped, started, etc... To list the processes that are running, type ps at a shell prompt. You will see a list of the process preceded by a PID(process ID). To stop a process, we use the brutal command kill followed by the PID.

Utilities
Unix sytems come with several built in utilities that you may need or want to become familiar with. We will present you with a few of these.

CAL:
Cal is a calendar utility that will allow you to display calendars for any year up to the 100th century. Type cal at a prompt and you will see the calendar for the current month. Type cal year and you will see the calendar for the entire year that you entered.

DATE:
The date utility displays the current date and time. Simply type date at a shell prompt.

DC & BC:
DC and BC are both calculators. DC stands for "Desktop Calculator" that can be used by typing dc at a prompt. You will notice that the shell prompt disappears. This means that you are "in" the utility. Simply enter your math equation, press enter and the results will be calculated. BC is very similar, but a little more robust and complicated.

GREP:
The grep command is used to search one or more files for a given character string or pattern and, if desired, replace the character string with another one. For example, one could enter grep CAL introunix.shtml. CAL specifies the character string to search for on each line in the file introunix.shtml which is the file that you are looking at right now. The result would be to display any line in the introunix.shtml file that includes the character string CAL and the entire line is displayed. Grep can be used in conjunction with the more command(previously discussed) if you think that the results of your query will be too large to fit on one screen. This can be accomplished using the following syntax: grep CAL introunix.shtml | more. The vertical line in the middle is called a "pipe". Click here for a good description of how pipes work. Grep can also be used to search multiple files simultaneously.





Comments (0)

Be the first to comment on this article


Related Items








7 Seconds Resources, Inc.




IT Showcase