Saturday, 7 May 2016

Basic and Common commands

Before you turn over to the next chapter where you are going to meet a plethora of commands, remember a few things that apply to all UNIX commands.
  •      All UNIX commands must always be entered in small case letters
  •      Between the command name and the options that may be available with the command there must always be a space or a tab, for example, ls –l. Here is the command whereas –l is the option and the two have been separated by space. The option is usually preceded by a minus (-) sign. The option available with a command are often known as switches
  •       Two or more options available with command can usually be combined, for example, the command ls –l –a is same as ls –la
  •        If you make a typing mistake, press backspace to erase characters Don’t try back using arrow keys and then attempt deleting using the del key
  •      To cancel the entire command before you press Enter, press ctrl+c Or del key

Basic Commands:
~]$ date             #To see the date of the system.
~]$ date +%d-%Y-%H-%M  #To see particular date format you always use date options

~]$ cal                #To see the current month calendar.
~]$ cal 11 2014            #To display the calendar, Month November (11) and year 2014


cal
      May 2016
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 31

~]$ clear            #To clear the screen
~]$ ls –l            #List files, directories with their properties
~]$ ls                   #To list files and directories
~]$ ls –a            #To list all hidden files and directories
~]$ ls –d            #To list only directories




 ~]$ pwd             #Print working directory

~]$ who am I    #To see from which user you have logged in
~]$ who             #To see all who is logged in yet this point of time from which IP
~]$ w                  #More details about user related info

 ~]$ uptime        #To see the server up time, boot time, users and load

 ~]$ uname –a     #Verify Operating system version, kernel version and architecture

~]$ touch <File Name>         #Create an empty file / Multiple empty files yet a time

Options:
~]$ touch –am                                   #it will change a file time to current time
~]$ touch –r file1 –B 30 file2  #it will create two files with 30 seconds time difference
~]$ cat /dev/null > file                       #To empty the data file
~]$ cat > <File Name>                      #Create an single file with text

       In above example two files 'kumar' and 'ravi' where created because we have provided the space in between 'kumar' and 'ravi' file names.


~]$ rm –rf <File / Directory Name>    #Delete files and directories forcefully
~]$rmdir <directory>    #Delete directories only
 ~]$ mkdir <Directory Name> #Create an empty directory / directories
~]$ mkdir –p <directory/directory/directory> #to create parent directories
 ~]$ cd <Path of the directory>         #Change directory

 ~]$ cat <File Name>              #View content of file

 ~]$ time             #Calculate response time of the activity / command

 ~]$ hwclock                  #to see detailed date and time with time zone


~]$ cp <Source path> <Destination path>  #Copy the files from one path to another path
~]$cp –Rv <source> <destination> #copy directories from source to destination

Options:
-R, -r,              #copy directories recursively
-v                     #verbose to see progress of copy job
-p                     #preserve
-f                      #forcefully
-I                     #interactive: Ask before overwriting file

 Copying directory must use –R to copy directories

 ~]$ mv <source> <destination>       #Move files/directories
~]$ mv <old name> <new name>    #Rename the file and directory

 ~]$ last                          #Check who logged in and when logged in duration

~]$ arch             #to know architecture
 
 ~]$ reboot / init 6                     #Restart server
~]$poweroff / init 0                  #To shut down the server
~]$ dmesg                                 #Check boot process logs

 ~]$ nsloookup <Server Address>    #check dns resolution
~]$ dig  <server address>      #check dns resolution to debug
~]$ tree <directory>               #it will show the tree of parent directory
~]$ stat <file name>               #detailed information about file

 ~]$ wc                            #word count, character count and line count
Options:
-l          #Check line count
-c         #Character Count
-w        #Word Count

 Help relate commands:
~]$ whatis <Command Name>  #It will display single line description about command

 ~]$ whereis <Command Name> #It will provide you path of the command

~]$ man <command>                 #manual page of the command
~]$ info <command>              #information about the command
~]$ <command> --help           #it will gives a command options and there usage
~]$ apropos <keyword>         #to know about the command use

Linux Booting procedure

The stages involved in Linux Booting Process are:
1. BIOS
2. Boot Loader
    - MBR
    - GRUB
3. Kernel
4. Init
5. Runlevel scripts
6. User Interface
STEP 1. BIOS
  • This is the first thing which loads once you power on your machine.
  • When you press the power button of the machine, CPU looks out into ROM for further instruction.
  • The ROM contains JUMP function in the form of instruction which tells the CPU to bring up the BIOS
  • BIOS determine all the list of bootable devices available in the system.
  • Prompts to select bootable device which can be Hard Disk, CD/DVD-ROM, Floppy Drive, USB Flash Memory Stick Etc..
  • Operating System tries to boot from Hard Disk where the MBR contains primary boot loader.
STEP 2. Boot Loader 
To be very brief this phase includes loading of the boot loader (MBR and GRUB/LILO) into memory to bring up the kernel.

MBR (Master Boot Record)
  • It is the first sector of the Hard Disk with a size of 512 bytes.
  • The first 434 - 446 bytes are the primary boot loader, 64 bytes for partition table and 6 bytes for MBR validation timestamp.
NOTE: Now MBR directly cannot load the kernel as it is unaware of the file system concept and requires a boot loader with file system driver for each supported file systems, so that they can be understood and accessed by the boot loader itself.

To overcome this situation GRUB is used with the details of the file system in
/boot/grub.conf and file system drivers

GRUB (Grand Unified Boot loader)

this loads the kernel in 3 stages

GRUB stage 1: 
  • The primary boot loader takes up less than 512 bytes of disk space in the MBR - too small a space to contain the instructions necessary to load a complex operating system. 
  • Instead the primary boot loader performs the function of loading either the stage 1.5 or stage 2 boot loader.
GRUB Stage 1.5: 
  • Stage 1 can load the stage 2 directly, but it is normally set up to load the stage 1.5. 
  • This can happen when the /boot partition is situated beyond the 1024 cylinder head of the hard drive. 
  • GRUB Stage 1.5 is located in the first 30 KB of Hard Disk immediately after MBR and before the first partition.
  • This space is utilized to store file system drivers and modules.
  • This enabled stage 1.5 to load stage 2 to load from any known location on the file system i.e. /boot/grub

GRUB Stage 2:
  • This is responsible for loading kernel from /boot/grub/grub.conf and any other modules needed
  • Loads a GUI interface i.e. splash image located at /grub/splash.xpm.gz with list of available kernels where you can manually select the kernel or else after the default timeout value the selected kernel will boot
The original file is /etc/grub.conf of which you can observe a symlink file at /boot/grub/grub.conf
STEP 3. Kernel
This can be considered the heart of operating system responsible for handling all system processes.

Kernel is loaded in the following stages:
  1. Kernel as soon as it is loaded configures hardware and memory allocated to the system.
  2. Next it uncompresses the initrd image (compressed using zlib into  zImage or bzImage formats) and mounts it and loads all the necessary drivers.
  3. Loading and unloading of kernel modules is done with the help of programs like insmod, and rmmod present in the initrd image.
  4. Looks out for hard disk types be it a LVM or RAID.
  5. Unmounts initrd image and frees up all the memory occupied by the disk image.
  6. Then kernel mounts the root partition as specified in grub.conf as read-only.
  7. Next it runs the init process
STEP 4. Init Process
  • Executes the system to boot into the run level as specified in /etc/inittab

You can check current runlevel details of your system using below command on the terminal

# who -r

         
run-level 3  Jan 28 23:29                   last=S
  • Next as per the fstab entry file system's integrity is checked and root partition is re-mounted as read-write (earlier it was mounted as read-only).
STEP 5. Runlevel scripts
A no. of runlevel scripts are defined inside /etc/rc.d/rcx.d
  • Based on the selected runlevel, the init process then executes startup scripts located in subdirectories of the /etc/rc.d directory.
  • Scripts used for runlevels 0 to 6 are located in subdirectories /etc/rc.d/rc0.d through /etc/rc.d/rc6.d, respectively.
  • For more details on scripts inside /etc/rc.d follow the below link
    What are the s and k scripts in the etc rcx.d directories
  • Lastly, init runs whatever it finds in /etc/rc.d/rc.local (regardless of run level). rc.local is rather special in that it is executed every time that you change run levels.
NOTE: rc.local is not used in all the distros as for example Debian.
Next if everything goes fine you should be able to see the Login Screen on your system.

Linux Directory Structure

Overview
Everything in Linux can be reduced to a file. Partitions are associated with files such as /dev/hda1. Hardware components are associated with files such as /dev/modem. The Filesystem Hierarchy Standard (FHS) is the official way to organize files in Unix and Linux directories.
Linux file system and directory structure
Several major directories are associated with all modern Unix/Linux operating systems. These directories organize user files, drivers, kernels, logs, programs, utilities, and more into different categories. The standardization of the FHS makes it easier for users of other Unix-based operating systems to understand the basics of Linux. All of the other directories shown in Table are subdirectories of the root directory, unless they are mounted separately.
Directory
Description
/
The root directory, the top-level directory in the FHS. All other directories are subdirectories of root, which is always mounted on some partition. All directories that are not mounted on a separate partition are included in the root directory’s partition.
/bin
Essential command line utilities. Should not be mounted separately; otherwise, it could be difficult to get to these utilities when using a rescue disk.
/boot
Includes Linux startup files, including the Linux kernel. Can be small; 16MB is usually adequate for a typical modular kernel. If you use multiple kernels, such as for testing a kernel upgrade, increase the size of this partition accordingly.
/etc
Most basic configuration files.
/dev
Hardware and software device drivers for everything from floppy drives to terminals. Do not mount this directory on a separate partition.
/home
Home directories for almost every user.
/lib
Program libraries for the kernel and various command line utilities. Do not mount this directory on a separate partition.
/mnt
The mount point for removable media, including floppy drives, CD-ROMs, and Zip disks.
/opt
Applications such as WordPerfect or Star Office.
/proc
Currently running kernel-related processes, including device assignments such as IRQ ports, I/O addresses, and DMA channels.
/root
The home directory of the root user.
/sbin
System administration commands. Don't mount this directory separately.
/tmp
Temporary files. By default, Red Hat Linux deletes all files in this directory periodically.
/usr
Small programs accessible to all users. Includes many system administration commands and utilities.
/var
Variable data, including log files and printer spools.

Bash Shell features

BASH Features


BASH: Bourne again shell

 when you login to the Server using any CLI based tool OR login to the CLI mode you can see as like below screen. 

$ sign = local user/non-privilege user
# sign = Administrator/Privileged user
 

 [root@sankar ~]#


Shells can be changed based the user compatibility and requirements, most of default shell is 'BASH' 


[root@sankar ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/dash
You have new mail in /var/spool/mail/root
[root@sankar ~]#



Command Line Completion:
Single Tab – it will provide the best option
Double Tab – it will provide all the possible options (Autocompletes)
Command Line Editing:
Ctrl+a            -           It moves cursor to the Home line position
Ctrl+b            -           Moves the cursor back one character
Ctrl+c            -           Sends the signal SIGINT to the current task, which aborts and close it.
Ctrl+d            -           Close current shell prompt
Ctrl+e            -           It will move a cursor to end of the bash
Ctrl+f             -           Moves cursor forward one character
Ctrl+g            -           Abort the research and restore the original file
Ctrl+h            -           Deletes the previous character (Same as backspace)
Ctrl+k            -           It is used to delete the command from the courser to line home position
Ctrl+l             -           Clear the screen
Ctrl+u            -           Clears the line content before the cursor and copies it into the clipboard
Ctrl+y            -           Yank the content from the cursor position
Ctrl+z            -           Sends the signal SIGTSTP to the current task, which suspend it
Ctrl+Shift+c -           Copy selected text
Ctrl+Shift+v            -           Paste the copied content
Ctrl+Shift+t             -           Tab
Ctrl+Shift+n            -           New Terminal
Ctrl+Shift+w            -           Close Tab
Ctrl+Shfift+q           -           Close a Terminal
Alt+b              -           Moves the cursor backward one word
Alt+c              -           Capitalizes the character under the cursor and moves to the end of word
Alt+d              -           Cut the word after the cursor
Alt+f               -           Moves the cursor forward one word
Alt+l               -           Lowers the case of every character from the cursor’s position to the end
Alt+.               -           Insert the last argument to the previous command
Command Line History: Command line history will be saved, when you execute any command in terminal.
 
$ history        -           this command will display all the previous executed commands
$ history –c   -           Clear the command history
$ !<number> -          it executes mentioned number command
Note: Default history size is 1000 commands

[root@sankar ~]# history





$ !<charectar>         -           it will display/execute matching character command
$ !!                              -           it will execute last executed command
Sophisticated prompt control:
?          -           It will replace a single character
Example:       rm –rf a?        -           it will delete the files with two characters
                        Rm –rf a??     -           it will delete the three characters files after “a”
*          -           replace multiple number of characters

Example:       rm –f  a*        - it will remove all the files which are starting with “a”

Piping and Redirecting:
Redirecting input and output from standard stream to user defined place
1.    Input RD       -0                                 <         
2.    Output RD -1              Values        >          Symbols
3.    Error RD       -2                                 >

Example:       $ sort < <file name>
                        $ wc < <file name>
                        $ history > f2
                        $mkdir d1 2> e1




Piping sending output or one command as a input to the another command

| pipe
pipe Symbol





Page related command:
            Less: is used to see the command output page by page in up and down way
            More: we can’t go upward downward, just see the output fit to the screen
 

Windows Vs Linux

Windows Vs Linux

                                Windows
                                      Linux
    • It is a proprietary software everything need to buy
    • It is an open source software everything is free
    • Less Secure
    • More Secure
    • More costly
    • Less Cost compare to windows
    • Chance to get a carpal tunnel syndrome
    • There is no chance to get carpal tunnel syndrome
    • Developed by Microsoft corporation
    • Developed by Linus Torvalds
    • Options need to select
    • You can develop anything as per your requirement
    • Kernel is not editable
    • Kernel is editable