Tuesday 12 March 2013

UNIX BASICS



What is Unix?
Unix is a operating system. It is a set of programs that acts a interface between computer and the user.
            
                             Computer--------> Unix OS<----------------- User

The computer programs that allocate the system resources and coordinate all the details of the computer's internals is called the operating system or kernel.

Users communicate with the kernel through a program known as the shell. The shell is a command line interpreter; it translates commands entered by the user and converts them into a language that is understood by the kernel.

History of  Unix
1.Unix was originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Douglas McIlroy, and Joe Ossanna.

2.
Several people can use a UNIX computer at the same time; hence UNIX is called a Multiuser system.
3   A user can also run multiple programs at the same time; hence UNIX is called multitasking.

Unix support multiuser  and multitasking environment.

Unix Architecture:

The main concept that unites all versions of UNIX is the following four basics:

  • Kernel: The kernel is the heart of the operating system. It interacts with hardware and most of the tasks like memory management, tash scheduling and file management.
  • Shell: User Communicate via kernel by writing the program known as the Shell.The shell is the utility that processes your requests. When you type in a command at your terminal, the shell interprets the command and calls the program that you want. The shell uses standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are most famous shells which are available with most of the Unix variants.
  • Commands and Utilities: There are various command and utilities which you would use in your day to day activities. cp, mv, cat and grep etc. are few examples of commands and utilities. There are over 250 standard commands plus numerous others provided through 3rd party software. All the commands come along with various optional options.
  • Files and Directories: All data in UNIX is organized into files. All files are organized into directories. These directories are organized into a tree-like structure called the filesystem.

Login Unix:

When you first connect to a UNIX system, you usually see a prompt such as the following:
login:

To login:
  Need the username and password. These are case sensitive so be careful while enter it.
 Read the information and messages that come up on the screen something as below.
login : amrood
amrood's password:
Last login: Sun Jun 14 09:32:32 2009 from 62.61.164.73
[amrood]$
 
You would be provided with a command prompt ( sometime called $ prompt ) where you would type your all the commands. For example to check calendar you need to type cal command as follows:

[amrood]$ cal
     June 2009
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

[amrood]$
 

Change Password:

All Unix systems require passwords to help ensure that your files and data remain your own and that the system itself is secure from hackers and crackers. Here are the steps to change your password:
  1. To start, type passwd at command prompt as shown below.
  2. Enter your old password the one you're currently using.
  3. Type in your new password. Always keep your password complex enough so that no body can guess it. But make sure, you remember it.
  4. You would need to verify the password by typing it again.
[amrood]$ passwd
Changing password for amrood
(current) Unix password:******
New UNIX password:*******
Retype new UNIX password:*******
passwd: all authentication tokens updated  successfully

[amrood]$
Note: I have put stars (*) just to show you the location where you would need to enter the current and new passwords otherwise at your system, it would not show you any character when you would type.
 
 Listing Directories and Files:

All data in UNIX is organized into files. All files are organized into directories. These directories are organized into a tree-like structure called the filesystem.
You can use ls command to list out all the files or directories available in a directory. Following is the example of using ls command with -l option.
[amrood]$ ls -l
total 19621
 
-rwxr-xr-x   1 ranga  users          2368 Jul 11 15:57 .profile* 
 drwxrwxr-x  2 amrood amrood      4096 Dec 25 09:59 uml
-rw-rw-r--  1 amrood amrood      5341 Dec 25 08:38 uml.jpg
drwxr-xr-x  2 amrood amrood      4096 Feb 15  2006 univ
drwxr-xr-x  2 root   root        4096 Dec  9  2007 urlspedia
-rw-r--r--  1 root   root      276480 Dec  9  2007 urlspedia.tar
drwxr-xr-x  8 root   root        4096 Nov 25  2007 usr
-rwxr-xr-x  1 root   root        3192 Nov 25  2007 webthumb.php
-rw-rw-r--  1 amrood amrood     20480 Nov 25  2007 webthumb.tar
-rw-rw-r--  1 amrood amrood      5654 Aug  9  2007 yourfile.mid
-rw-rw-r--  1 amrood amrood    166255 Aug  9  2007 yourfile.swf

[amrood]$
 
 
Here enteries starting with d..... represent directories. For example uml, univ and urlspedia are directories and rest of the enteries are files.


Because the first character is a hyphen (-), you know that this is a regular file. Several characters appear after this hyphen. The first three characters indicate the permissions for the owner of the file, the next three characters indicate the permissions for the group the file is associated with, and the last three characters indicate the permissions for all other users.
EX: Checkout the first example..
The permission block for this file indicates that the user has read, write, and execute permissions, whereas members of the group users and all other users have only read and execute permissions.

After the permissions block, the owner and the group are listed. For this file, the owner is ranga and the group is users.


Here is the information about all the listed columns:
  1. First Column: represents file type and permission given on the file. Below is the description of all type of files.  (r)- read ,(w)-write and (x)-execute permission.
  2. Second Column: represents the number of memory blocks taken by the file or directory.
  3. Third Column: represents owner of the file. This is the Unix user who created this file.
  4. Fourth Column: represents group of the owner. Every Unux user would have an associated group.
  5. Fifth Column: represents file size in bytes.
  6. Sixth Column: represents date and time when this file was created or modified last time.
  7. Seventh Column: represents file or directory name.
Permissions Directories Group Size Date Directory or file
drwx------ 2 users 4096 Nov 2 19:51 mail/
drwxr-s--- 35 www 32768 Jan 20 22:39 public_html/
-rw------- 1 users 3 Nov 25 02:58 test.txt
Who is Logged In?
As discussed already that unix is a multiuser environment so if you want to know  who is logged in to the computer at the same time.
There are three commands are available to get you this information, based on how much you'd like to learn about the other users: users, who, and w.
[amrood]$ users
 amrood bablu qadir

[amrood]$ who
amrood ttyp0 Oct 8 14:10 (limbo)
bablu  ttyp2 Oct 4 09:08 (calliope)
qadir  ttyp4 Oct 8 12:09 (dent)

[amrood]$
Try w command on your system to check the output. This would list down few more information associated with the users logged in the system.
 To log out:
  1. Just type logout command at command prompt, and the system will clean up everything and break the connection.

    One most important point about  unix is: Everything in unix is the Files and directories i.e how to create and remove files, copy and rename them, create links to them etc.

Meta Characters:

Meta characters have special meaning in Unix. For example * and ? are metacharacters. We use * to match 0 or more characters, a question mark ? matches with single character.
For Example:
[amrood]$ls ch*.doc
Displays all the files whose name start with ch and ends with .doc:
ch01-1.doc   ch010.doc  ch02.doc    ch03-2.doc 
ch04-1.doc   ch040.doc  ch05.doc    ch06-2.doc
ch01-2.doc ch02-1.doc c
Here * works as meta character which matches with any character. If you want to display all the files ending with just .doc then you can use following command:
[amrood]$ls *.doc 
 

Creating Files:

You can use vi editor to create ordinary files on any Unix system. You simply need to give following command:

[amrood]$ vi filename
Above command would open a file with the given filename. You would need to press key i to come into edit mode. Once you are in edit mode you can start writing your content in the file as below:
This is unix file....I created it for the first time.....
I'm going to save this content in this file.
Once you are done, do the following steps:
  • Press key esc to come out of edit mode.
  • Press two keys Shift + ZZ together to come out of the file completely.
Now you would have a file created with filemame in the current directory.
[amrood]$ vi filename [amrood]$

Editing Files:

You can edit an existing file using vi editor. We would cover this in detail in a separate tutorial. But in short, you can open existing file as follows:
[amrood]$ vi filename
Once file is opened, you can come in edit mode by pressing key i and then you can edit file as you like. If you want to move here and there inside a file then first you need to come out of edit mode by pressing key esc and then you can use following keys to move inside a file:
  • l key to move to the right side.
  • h key to move to the left side.
  • k key to move up side in the file.
  • j key to move down side in the file.
So using above keys you can position your cursor where ever you want to edit. Once you are positioned then you can use i key to come in edit mode. Edit the file, once you are done press esc and finally two keys Shift + ZZ together to come out of the file completely.

Display Content of a File:

You can use cat command to see the content of a file. Following is the simple example to see the content of above created file:
[amrood]$ cat filename
This is unix file....I created it for the first time.....
I'm going to save this content in this file.
[amrood]$
 

Counting Words in a File:

You can use the wc command to get a count of the total number of lines, words, and characters contained in a file. Following is the simple example to see the information about above created file:
[amrood]$ wc filename 2 19 103 filename [amrood]$
 
 Here 2-(1st coloumn) represents the total number of lines in the file.
      19-(2nd column) represents the total number of words in the file.
      103(3rd column)- reprents the total number of bytes in the file.And this is actually
                        size of the file.
 
You can give multiple files at a time to get the information about those file. Here is simple syntax:

[amrood]$ wc filename1 filename2 filename3

Copying Files:

To make a copy of a file use the cp command. The basic syntax of the command is:
[amrood]$ cp source_file destination_file
 

Renaming Files:

To change the name of a file use the mv command. Its basic syntax is:
[amrood]$ mv old_file new_file

Deleting Files:

To delete an existing file use the rm command. Its basic syntax is:
[amrood]$ rm filename
Caution: It may be dangerous to delete a file because it may contain useful information. So be careful while using this command. It is recommended to use -i option along with rm command.
Following is the example which would completely remove existing file filename:
[amrood]$ rm filename
[amrood]$
You can remove multiple files at a tile as follows:
[amrood]$ rm filename1 filename2 filename3
[amrood]$

Here are the list of all the commands that are commonly used  in the unix environment.

go and check it out hope it will helpful to you.


Home Directory:

The directory in which you find yourself when you first login is called your home directory.
You can go in your home directory anytime using the following command:
[amrood]$cd ~
[amrood]$
Here ~ indicates home directory. If you want to go in any other user's home directory then use the following command:
[amrood]$cd ~username
[amrood]$
To go in your last directory you can use following command:
[amrood]$cd -
[amrood]$
 
 The directories . (dot) and .. (dot dot)
 
The filename . (dot) represents the current working directory; and the filename .. (dot dot) represent the directory one level above the current working directory, often referred to as the parent directory.

File Access Modes:

The permissions of a file are the first line of defense in the security of a Unix system. The basic building blocks of Unix permissions are the read, write, and execute permissions, which are described below.  read(r), write(w), and execute(x). Check the command (ls -l ) which explained in the above of this post.

Changing Permissions:(chmod)

To change file or directory permissions, you use the chmod (change mode) command. There are two ways to use chmod: symbolic mode and absolute mode.

Using chmod in Symbolic Mode:

The easiest way for a beginner to modify file or directory permissions is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the following table.
Chmod operatorDescription
+Adds the designated permission(s) to a file or directory.
-Removes the designated permission(s) from a file or directory.
=Sets the designated permission(s).

Here's an example using testfile. Running ls -1 on testfile shows that the file's permissions are as follows:

Each permission is assigned a value, as the following table shows, and the total of each set of permissions provides a number for that set.
NumberOctal Permission RepresentationRef
0No permission---
1Execute permission--x
2Write permission-w-
3Execute and write permission: 1 (execute) + 2 (write) = 3-wx
4Read permissionr--
5Read and execute permission: 4 (read) + 1 (execute) = 5r-x
6Read and write permission: 4 (read) + 2 (write) = 6rw-
7All permissions: 4 (read) + 2 (write) + 1 (execute) = 7rwx

Numeric Permissions:CHMOD can also to attributed by using Numeric Permissions:
400 read by owner
040 read by group
004 read by anybody (other)
200 write by owner
020 write by group
002 write by anybody
100 execute by owner
010 execute by group
001 execute by anybody

Examples
The above numeric permissions you can visit the site:

http://www.computerhope.com/unix/uchmod.htm

 

Sending Email:

You use the Unix mail command to send and receive mail. Here is the syntax to send an email:
[amrood]$mail [-s subject] [-c cc-addr] [-b bcc-addr] to-addr
Here are important options related to mail command:
OptionDescription
-sSpecify subject on command line.
-cSend carbon copies to list of users. List should be a comma-separated list of names.
-bSend blind carbon copies to list. List should be a comma-separated list of names.
Following is the example to send a test message to amrood@gmail.com.
[amrood]$mail -s "Test Message" admin@yahoo.com 
You are then expected to type in your message, followed by an "control-D" at the beginning of a line. To stop simply type dot (.) as follows:
Hi,

This is a test
.
Cc: 
You can send a complete file using a redirect < operator as follows:
[amrood]$mail -s "Report 05/06/07" admin@yahoo.com < demo.txt 
To check incoming email at your Unix system you simply type email as follows:
[amrood]$mail
no email
  
These are the basics of the unix there are many more topics present in the unix.
Hope you find some basic concepts from my blog-spot. And that's I want from you.
Believe me I too don't know the Unix and i learn some concepts when I make some research
about it. So my suggestion is to you 'stop thinking start learning'. Good luck. 
 

 




 





 


  


 

 

 





      
 
 

No comments:

Post a Comment