Thursday, July 18, 2013

Unix Fundamentals



What is Unix ?
The UNIX operating system is a set of programs that act as a link between the computer and the 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.

# 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.

# There are various Unix variants available in the market. Solaris Unix, IBM-AIX, HP-UX unix.
# UNIX is called multitasking a user can run multiple programs at the same time.

# UNIX is called a multiuser system because several users can access at the same time.

What is Linux ?
Linux is an operating system that was first created at the University of Helsinki in Finland by a
young student named Linus Torvalds. At this time the student was working on a UNIX system that
was running on an expensive platform. Because of his low budget, and his need to work at home,
he decided to create a copy of the UNIX system in order to run it on a less expensive platform,
such as an IBM PC.

The Linux operating system is developed under the GNU General Public License (also known as
GNU GPL) and its source code is freely available to everyone who downloads it via the Internet.
The CD-ROM version of Linux is also available in many stores, and companies that provide it will
charge you for the cost of the media and support. Linux may be used for a wide variety of
purposes including networking, software development, and as an end-user platform. Linux is
often considered an excellent, low-cost alternative to other more expensive operating systems
because you can install it on multiple computers without paying more.

Advantages of Linux
# There are no royalty or licensing fees for using Linux, and the source code can be modified to fit
your needs. The results can be sold for profit, but original authors retain copyright and you must
provide the source to your modifications.

# Because it comes with source code to the kernel, it is quite portable. Linux runs on more CPUs
and platforms than any other computer operating system.

#The recent direction of the software and hardware industry is to push consumers to purchase
faster computers with more system memory and hard drive storage. Linux systems are not
affected by those industries’ orientation because of it capacity to run on any kind of computers,
even aging x486-based computers with limited amounts of RAM.

# Linux is a true multi-tasking operating system similar to his brother UNIX. That means that if a
program crashes you can kill it and continue working with confidence.

#Another benefit is that Linux is practically immunized against all kinds of viruses that we find in
other operating systems. To date we have found only two viruses that were effective on Linux
systems.

Unix Components
Kernel
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.

Cell
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. 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 file system.

File Management in Unix
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 file system.
Thare are three basic types of files in unix
Ordinary  Files
An ordinary file is a file on the system that contains data, text, or program instructions. In this tutorial, you look at working with ordinary files.
Directories
Directories store both special and ordinary files. For users familiar with Windows or Mac OS, UNIX directories are equivalent to folders.
Special Files
Some special files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts and enable you to access a single file using different names.

Frequently Use Unix Commands
List Comand: ls
The ls command shows a list of files in the present directory.
For example:
[oracle@localhost ~]$  ls
Desktop  oraInventory  Screenshot.png

If the ls commands is use with  -l option it would help to get more information about the listed files.
For example:
[oracle@localhost ~]$  ls -l
total 148
drwxr-xr-x    2  oracle  oinstall   4096     Jun 30 09:35  Desktop
drwxrwx---   6  oracle  oinstall   4096     Jun 30 09:45   oraInventory
-rw-r--r--      1  oracle  oinstall 128924   Jul  1 00:18    Screenshot.png

First Column: represents file type and permission given on the file. Below is the description of all type of files.
Second Column: represents the number of memory blocks taken by the file or directory.
Third Column: represents owner of the file. This is the Unix user who created this file.
Fourth Column: represents group of the owner. Every Unix user would have an associated group.
Fifth Column: represents file size in bytes.
Sixth Column: represents date and time when this file was created or modified last time.
Seventh Column: represents file or directory name.

Unix Meta Characters
Meta characters have special meaning in Unix. For example * and ? are meta characters. We use
* to match 0 or more characters, a question mark ? matches with single character.
For example:
[oracle@localhost dbs]$  ls *.ora
initdw.ora  init.ora  spfileorcl.ora

Unix Hidden Files
An invisible file is one whose first character is the dot or period character (.). UNIX programs
(including the shell) use most of these files to store configuration information.

Creating and Editing Files
Vi editor:
It is use to create or edit ordinary files on any Unix system.
[oracle@localhost ~]$ vi filename

After this press i for editing the file. For saving the file press Esc then Shift+: and wq!. Exit saving the file.

Display Content of a File
Cat Command:
It is use to see the content of a file.
[oracle@localhost ~]$ cat filename

The above command will display the contents of the filename file.

Copying Files
Cp Command: It is use to make a copy of a file. The basic syntax of the command is:
[oracle@localhost ~]$  cp source_file destination_file

The above command copy the contents of source_file to the destination_file

Renaming or Moving File
mv command: To change the name of a file use the mv command. Its basic syntax is:
[oracle@localhost ~]$  mv old_file new_file

Following is the example which would rename existing file filename to newfile:
[oracle@localhost ~]$  mv filename newfile

Deleting Files:
Rm command: To delete an existing file use the rm command. Its basic syntax is:
[oracle@localhost ~]$ rm filename

You can remove multiple files at a tile as follows:
[oracle@localhost ~]$ rm  filename1  filename2  filename3

Unix - Pipes and Filters(2)
You can connect two commands together so that the output from one program becomes the input of the next program. Two or more commands connected in this way form a pipe.

To make a pipe,  put a vertical bar (|) on the command line between two commands.

When a program takes its input from another program, performs some operation on that input, and writes the result to the standard output, it is referred to as a filter.

The grep Command
The grep program searches a file or files for lines that have a certain pattern. The syntax is-
[oracle@localhost ~]$ ps -ef|grep "pmon"
oracle    7249     1  0 13:35 ?        00:00:01 ora_pmon_orcl
oracle    8869  1426  0 19:50 pts/1    00:00:00 grep pmon

Standard Unix Streams
stdin :  This is referred to as  standard input  and associated file descriptor is 0. This is also represented as STDIN. Unix program would read default input from STDIN.
stdout : This is referred to as standard output and associated file descriptor is 1. This is also represented as STDOUT. Unix program would write default output at STDOUT.
stderr :  This is referred to as standard error  and associated file descriptor is 2. This is
also represented as STDERR. Unix program would write all the error message at STDERR.

Unix - Directory Management
A directory is a file whose sole job is to store file names and related information. All files, whether ordinary, special, or directory, are contained in directories.

UNIX uses a hierarchical structure for organizing files and directories. This structure is often referred to as a directory tree . The tree has a single root node, the slash character ( /), and all other directories are contained below it.

Home Directory
The directory in which you find yourself when you first login is called your home directory.

Absolute/Relative Pathnames
Relative Pathnames - Directories are arranged in a hierarchy with root (/) at the top. The position of any file within the hierarchy is described by its pathname.
Elements of a pathname are separated by a /. A pathname is absolute if it is described in relation to root, so absolute pathnames always begin with a /.
Ex: /u01/app/oracle

Relative Pathnames - A pathname can also be relative to your current working directory. Relative pathnames never begin with /.
Ex: home/app/oracle

Present Working Directory(pwd)
To determine where you are within the file system hierarchy at any time, enter the command pwd to print the current working directory.

Create Directory(mkdir)
It is used to create a directory as follows-
[oracle@localhost ~]$ mkdir dirname

In order to create more than one directory-
[oracle@localhost ~]$ mkdir dirname1 dirname2… dirnamen

You can create Parent Directory with the  -p  option to the  mkdir  command. It creates all the
necessary directories for you.
[oracle@localhost ~]$ mkdir –p /home/app/oracle/oradata/orcl
Removing Directories(rm)
Directories can be deleted (Single/Multiple Directories) using the rmdir command as follows-
[oracle@localhost ~]$ rmdir dirname
[oracle@localhost ~]$ rmdir dirname1 dirname2 dirname3

Changing Directories(cd)
The  cd  command to do more than change to a home directory. You can use it to change to any directory by specifying a valid absolute or relative path.
[oracle@localhost ~]$ cd dirname
[oracle@localhost ~]$ cd  /home/app/oracle/oradata/orcl

Renaming Directories(mv)
The mv (move) command can also be used to rename a directory.
[oracle@localhost ~]$ mv filename1 filename2

It changes from filename1 to filename2.

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 Permission in Unix
File ownership is an important component of UNIX that provides a secure method for storing files. Every file in UNIX has the attributes as follows-
# Owner permissions:  The owner's permissions determine what actions the owner of the file can perform on the file.
# Group permissions: The group's permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the file.
# Other (world) permissions: The permissions for others indicate what action all other users can perform on the file.

[oracle@localhost ~]$ ls -l
total 32
drwxrwx---  6 oracle oinstall 4096 Jan 29 10:50 oraInventory

The permissions are broken into groups of threes, and each position in the group denotes a specific permission, in this order: directory(d), read (r), write (w), execute (x)

# The first four characters (1-4) i.e. drwx represent the permissions for the file's owner.
# The second group of three characters (5-7) i.e. rwx consists of the permissions for the group to which the file belongs.
# The last group of three characters (8-10) i.e. --- represents the permissions for everyone else. Here, any other person has no permission on oraInventory.

File Access Modes
There are three different access modes for a file/directory as follows-
# Read- Having read permission can read the file i.e. view the contents of the file.
# Write- Having write permission can modify, delete the contents of the file.
# Execute- Having the execute permission can run the file as a program.

Changing Permissions in file(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.
(+)    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).

Using chmod with Absolute Permissions
The second way to modify permissions with the chmod command is to use a number to specify
each set of permissions for the file.

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.
Number representation for File permission
      0    No permission (---)
1      Execute permission (--x)
2      Write permission (-w-)
3      Execute and write permission: 1 (execute) + 2 (write) = 3; (-wx)
4      Read Permission (r--)
5      Read and execute permission: 4 (read) + 1 (execute) = 5 (r-x)
6      Read and write permission: 4 (read) + 2 (write) = 6 (rw-)
7      All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 (rwx)

Changing Owners and Groups(chown, chgrp)
# chown:  The chown command stands for "change owner" and is used to change the
owner of a file.
# chgrp: The chgrp command stands for "change group" and is used to change the group
of a file.

Changing Ownership(chown)- The chown command changes the ownership of a file. It is used as follow-
[oracle@localhost ~]$ chown user_name filename

The above command changes the owner of filename file to user_name. The value of user can be either the name of a user on the system or the user id (uid) of a user
on the system.

Changing Group Ownership(chgrp)- The chrgp command changes the group ownership of a file.
It is used as follows-
[oracle@localhost ~]$ chown group_name filename

The above command changes the group owner of filename file to group_name.

Setting the PATH
When you type any command on command prompt, the shell has to locate the command before
it can be executed.
The PATH variable specifies the locations in which the shell should look for commands. Usually it
is set as follows-
[oracle@localhost ~]$ PATH=/bin:/usr/bin
[oracle@localhost ~]$

Process in Unix
# Forground Process - Every process that you start runs in the foreground. It gets its input from the
keyboard and sends its output to the screen.
# Background Processes - A background process runs without being connected to your keyboard. If the background process requires any keyboard input, it waits. The simplest way to start a background process is to add an ampersand ( &) at the end of the command. The advantage of running a process in the background is that you can run other commands also. We do not have to wait until it completes to start another.

Listing Running Processes(ps)
It is easy to see your own processes by running the ps (process status) command. It can be used with the –f option for more information. It is used as follows –

[oracle@localhost ~]$ ps
  PID TTY          TIME CMD
 7214 pts/1    00:00:00 bash

[oracle@localhost ~]$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
oracle    7214  7212  0 19:51 pts/1    00:00:00 bash

Stopping Processes
Ending a process can be done in several different ways. Often, from a console-based command,
sending a CTRL + C keystroke (the default interrupt character) will exit the command. This
works when process is running in foreground mode.

Kill Command(kill)
If a process is running in background mode then first you would need to get its Job ID using ps
command and after that you can use kill command to kill the process as follows-

[oracle@localhost ~]$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
oracle    7214  7212  0 19:51 pts/1    00:00:00 bash

[oracle@localhost ~]$ kill 7214

Here  kill  command would terminate first_one process. If a process ignores a regular kill command, you can use kill -9 followed by the process ID as follows-
[oracle@localhost ~]$ kill -9 7214

Ping Utility in Unix
The ping command sends an echo request to a host available on the network. Using this command you can check if your remote host is responding well or not. The ping command is useful for the following-
# Tracking and isolating hardware and software problems.
# Determining the status of the network and various foreign hosts.
# Testing, measuring, and managing networks.

Ping is done as follow-
$ping hostname or ip-address

File Transfer Protocol(ftp) Utility
Here ftp stands for File Transfer Protocol. This utility helps you to upload and download your file
from one computer to another computer. The ftp utility has its own set of UNIX like commands which allow you to perform tasks as follows-
# Connect and login to a remote host.
# Navigate directories.
# List directory contents
# Put and get files
# Transfer files as ascii, ebcdic or binary

The ftp command is used as follows-
$ftp hostname or ip-address

telnet in Unix
For connecting to a remote Unix machine and work on that machine remotely. Telnet is a utility that allows a computer user at one site to make a connection, login and then conduct work on a computer at another site. Once you are  login using telnet, you can perform all the activities on your remotely connect machine.

Finger Command(finger)
The finger command displays information about users on a given host. The host can be either local or remote. Finger may be disabled on other systems for security reasons. Following are the simple syntax to use finger command-
[root@localhost ~]# finger
Login     Name       Tty      Idle  Login Time   Office     Office Phone
oracle              *:0             Jul  7 00:50
oracle               pts/1       7  Jul  8 17:07 (:0.0)
oracle               pts/2          Jul  9 19:07 (:0.0)

[root@localhost ~]# finger oracle
Login: oracle                           Name: (null)
Directory: /home/oracle                 Shell: /bin/bash
On since Sun Jul  7 00:50 (IST) on :0 (messages off)
On since Mon Jul  8 17:07 (IST) on pts/1 from :0.0
   10 minutes 53 seconds idle
On since Tue Jul  9 19:07 (IST) on pts/2 from :0.0
Last login Tue Jul  9 19:09 (IST) on pts/3 from 192.168.0.1
No mail.
No Plan.

Unix Editors(vi, vim)
There are many ways to edit files in Unix and for me one of the best ways is using screen-oriented text editor vi. This editor enable you to edit lines in context with other lines in the file. Now a days you would find an improved version of vi editor which is called  VIM (Vi Improved).
You can use  vi  editor to edit an  existing file or to create a new file from scratch. You can also use this editor to just read a text file.

Operation Modes
While working with vi editor you would come across following two modes:
# Command mode:  This mode enables you to perform administrative tasks such as saving files, executing commands, moving the cursor, cutting (yanking) and pasting lines or words, and finding and replacing. In this mode, whatever you type is interpreted as a command.
# Insert  mode:  This mode enables you to insert text into the file. Everything that's typed in this mode is interpreted as input and finally it is put in the file.

The vi always starts in command mode. To enter text, you must be in insert mode. To come in insert mode you simply press i. To get out of insert mode, press the  Esc key, which will put you back into command mode.

Quit and Save vi Editor
The command to quit out of vi is :q. Once in command mode, type colon, and 'q', followed by return. If your file has been modified in any way, the editor will warn you of this, and not let you quit. To ignore this message, the command to quit out of vi without saving is  :q!. This lets you exit vi without saving any of the changes.

The command to save the contents of the editor is  :w. You can combine the above command
with the quit command, or :wq and return.

You can specify a different file name to save to by specifying the name after the :w. For example, if you wanted to save the file you were working as another filename called filename2, you would type :w filename and return.

The vi is case-sensitive, so you need to pay special attention to capitalization when using commands.

Replacing Text
The substitution command (:s/) enables you to quickly replace words or groups of words within
your files. Here is the simple syntax-
:s/search/replace/g

The g stands for globally. The result of this command is that all occurrences on the cursor's line are changed.

Running Commands
The vi has the capability to run commands from within the editor. To run a command, you only need to go into command mode and type :! command.

For example, if you want to check whether a file exists before you try to save your file to that filename, you can type :! ls and you will see the output of ls on the screen.


Reference www.tutorial.com

 

No comments:

Post a Comment