Wednesday, July 10, 2013

Unix/Linux Fundamentals


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.





Unix/Linux Fundamentals



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




Unix/Linux Fundamentals



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.



Basics of Unix/Linux - 2



 
Unix - Pipes and Filters
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.