Saturday, 7 May 2016

Linux Basic/Common commands

Linux Basic/Common commands

Command      Description                 
adduser           Add a new user                    
arch                  Print machine architecture                
awk                 Find and Replace text within file(s)              
bc                    An arbitrary precision calculator language                 
cal                    Display a calendar                  
cat                   Concatenate files and print on the standard output              
chdir                Change working directory                 
chgrp               Change the group ownership of files             
chkconfig        Tool for maintaining the /etc/rc[0-6].d directory hierarchy               
chmod             Change the access permissions of files and directories                    
chown             Change the user and group ownership of files                      
chroot              Change root directory           
cksum              Print CRC checksum and byte counts                      
clear                 Clear terminal screen             
cmp                 Compare two files                 
comm              Compare two sorted files line by line           
cp                    Copy one or more files to another location               
cron                 Daemon to execute scheduled commands                
crontab            Schedule a command to run at a later time               
csplit                Split a file into context-determined pieces               
cut                   Divide a file into several parts                       
date                 Display or change the date & time               
dc                    Desk Calculator                    
dd                    Data Dump - Convert and copy a file                      
df                    Display free disk space                     
diff                  Display the differences between two files               
diff3                Show differences among three files             
dir                    Briefly list directory contents           
dircolors          Colour setup for `ls'              
dirname           Convert a full pathname to just a path                      
du                    Estimate file space usage                  
echo                 Display message on screen                
ed                    A line-oriented text editor (edlin)                 
egrep               Search file(s) for lines that match an extended expression               
eject                 Eject CD-ROM                     
env                  Display, set, or remove environment variables                     
expand            Convert tabs to spaces                      
expr                 Evaluate expressions             
factor               Print prime factors                
false                 Do nothing, unsuccessfully               
fdformat          Low-level format a floppy disk                    
fdisk                Partition table manipulator for Linux           
fgrep                Search file(s) for lines that match a fixed string                   
find                 Search for files that meet a desired criteria              
fmt                  Reformat paragraph text                   
fold                 Wrap text to fit a specified width                
format             Format disks or tapes            
free                  Display memory usage                      
fsck                 Filesystem consistency check and repair                  
gawk               Find and Replace text within file(s)             
grep                 Search file(s) for lines that match a given pattern                
groups             Print group names a user is in           
gzip                 Compress or decompress named file(s)                    
head                Output the first part of file(s)           
hostname         Print or set system name                   
id                     Print user and group id's                   
info                  Help info                   
install               Copy files and set attributes              
join                  Join lines on a common field            
kill                   Stop a process from running             
less                  Display output one screen at a time              
ln                     Make links between files                  
locate               Find files                    
logname           Print current login name                    
lpc                   Line printer control program             
lpr                    Off line print             
lprm                 Remove jobs from the print queue                
ls                      List information about file(s)             
man                 Help manual              
mkdir               Create new folder(s)             
mkfifo             Make FIFOs (named pipes)              
mknod             Make block or character special files            
more                Display output one screen at a time              
mount              Mount a file system              
mv                   Move or rename files or directories               
nice                  Set the priority of a command or job            
nl                     Number lines and write files             
nohup              Run a command immune to hangups            
passwd            Modify a user password                   
paste                Merge lines of files               
pathchk           Check file name portability               
pr                     Convert text files for printing           
printcap           Printer capability database                
printenv           Print environment variables              
printf               Format and print data           
ps                     Process status            
pwd                 Print Working Directory                    
quota               Display disk usage and limits           
quotacheck      Scan a file system for disk usage                  
quotactl           Set disk quotas                       
ram                  ram disk device                     
rcp                   Copy files between two machines                
rm                    Remove files             
rmdir               Remove folder(s)                  
rpm                  Remote Package Manager                
rsync                Remote file copy (Synchronize file trees)                  
screen              Terminal window manager               
sdiff                Merge two files interactively            
sed                   Stream Editor            
select               Accept keyboard input                     
seq                   Print numeric sequences                    
shutdown        Shutdown or restart linux                 
sleep                Delay for a specified time                 
sort                  Sort text files            
split                 Split a file into fixed-size pieces                   
su                     Substitute user identity                     
sum                  Print a checksum for a file                
symlink            Make a new name for a file              
sync                 Synchronize data on disk with memory                   
tac                   Concatenate and write files in reverse                      
tail                   Output the last part of files               
tar                    Tape Archiver           
tee                   Redirect output to multiple files                   
test                  Evaluate a conditional expression                 
time                 Measure Program Resource Use                   
touch               Change file timestamps                     
top                   List processes running on the system            
traceroute        Trace Route to Host              
tr                      Translate, squeeze, and/or delete characters             
true                  Do nothing, successfully                   
tsort                 Topological sort                    
tty                    Print filename of terminal on stdin               
umount            Unmount a device                 
uname              Print system information                   
unexpand        Convert spaces to tabs                      
uniq                 Uniquify files            
units                Convert units from one scale to another                   
unshar              Unpack shell archive scripts              
useradd           Create new user account                   
usermod          Modify user account             
users                List users currently logged in            
uuencode         Encode a binary file               
uudecode        Decode a file created by uuencode              
vdir                  Verbosely list directory contents (`ls -l -b')               
watch              Execute/display a program periodically                    
wc                   Print byte, word, and line counts                  
whereis            Report all known instances of a command               
which              Locate a program file in the user's path                    
who                 Print all usernames currently logged in                     
whoami           Print the current user id and name (`id -un')             
xargs                Execute utility, passing constructed argument list(s)           
yes                   Print a string until interrupted     

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.