Search This Blog

Wednesday, 17 May 2017

UNIX Commands



What is Operating System ?

Operating system is an interface between user and the computer hardware. The hardware of the computer cannot understand the human readable language as it works on binaries i.e. 0's and 1's. Also it is very tough for humans to understand the binary language, in such case we need an interface which can translate human language to hardware and vice-versa for effective communication.  

Types of Operating System: w  Single User - Single Tasking Operating System c
  Multi User - Multitasking Operating System  

Single User - Single Tasking Operating System :  In this type of operating system only one user can log into system and can perform only one task at a time. E.g.: MS-DOS  

Single User - Multi tasking operating System : This type of O/S supports only one user to log into the system but a user can perform multiple tasks at a time, browsing internet while playing songs etc. E.g.: Windows -98,Xp,vista,Seven etc.  

Multi User - Multi Tasking Operating System:  These type of O/S provides multiple users to log into the system and also each user can perform various tasks at a time. In a broader term multiple users can logged in to system and share the resources of the system at the same time. E.g.: UNIX, LINUX 

HISTORY OF UNIX  
In PIthe beginning, there was AT&T.   
Bell Labs’ Ken Thompson developed UNIX in 1969 so he could play games on a scavenged DEC PDP-7. With the help of Dennis Ritchie, the inventor of the “C” programing language, Ken rewrote UNIX entirely in “C” so that it could be used on different computers. In 1974, the OS was licensed to universities for educational purposes. Over the years, hundreds of people added and improved upon the system, and it spread into the commercial world. Dozens of different UNIX “flavors” appeared, each with unique qualities, yet still having enough similarities to the original AT&T version. All of the “flavors” were based on either AT&T’s System V or Berkeley System Distribution (BSD) UNIX, or a hybrid of both.  

During the late 1980’s there were several of commercial implementations of UNIX: 
 Apple Computer’s A/UX  
 AT&T’s System V Release 3 
 Digital Equipment Corporation’s Ultrix and OSF/1 (renamed to DEC UNIX)
 Hewlett Packard’s HP-UX 
 IBM’s AIX 
 Lynx’s Real-Time UNIX
 NeXT’s NeXTStep 
 Santa Cruz Operation’s SCO UNIX 
 Silicon Graphics’ IRIX  
 SUN Microsystems’ SUN OS and Solaris 
  and dozens more.  

The Open Standards Foundation is a UNIX industry organization designed to keep the various UNIX flavors working together. They created operating systems guidelines called POSIX to encourage interoperability of applications from one flavor of UNIX to another. Portability of applications to different gave UNIX a distinct advantage over its mainframe competition.  

Then came the GUIs. Apple’s Macintosh operating system and Microsoft’s Windows operating environment simplified computing tasks, and made computers more appealing to a larger number of users. UNIX wizards enjoyed the power of the command line interface, but acknowledged the difficult learning curve for new users. The Athena Project at MIT developed the X Windows Graphical User Interface for UNIX computers. Also known as the X11 environment, corporations developed their own “flavors” of the UNIX GUIs based on X11. Eventually, a GUI standard called Motif was generally accepted by the corporations and academia.  

During the late 1990’s Microsoft’s Windows NT operating system started encroaching into traditional UNIX businesses such as banking and high-end graphics. Although not as reliable as UNIX, NT became popular because of the lower learning curve and its similarities to Windows 95 and 98. Many traditional   

UNIX companies, such as DEC and Silicon Graphics, abandoned their OS for NT. Others, such as SUN, focused their efforts on niche markets, such as the Internet.  

Linus Torvalds had a dream. He wanted to create the coolest operating system in the world that was free for anyone to use and modify. Based on an obscure UNIX flavor called MINIX, Linus took the source code and created his own flavor, called Linux. Using the power of the Internet, he distributed copies of his OS all over the world, and fellow programmers improved upon his work. In 1999, with a dozen versions of the OS and many GUIs to choose from, Linux is causing a UNIX revival. Knowing that people are used to the Windows tools, Linux developers are making applications that combine the best of Windows with the best of UNIX.

UNIX Principles
 Everything is a file:- UNIX system have many powerful utilities designed to create and manipulate files. The UNIX security model is based around the security of files. By treating everything as a file, you can secure access to hardware in the same way as you secure access to a document. 
 Configuration data stored in text: - Storing configuration in text allows an administrator to move a configuration from one machine to another easily, provide the ability to roll back a system configuration to a particular date and time. 
 Small, Single-Purpose Programs: - UNIX provides many utilities. 
 Avoid captive user interfaces:-
 Ability to chain programs together to perform complex tasks:- A core design feature of UNIX is that output of  one program can be the input for another. This gives the user the flexibility to combine many small programs together to perform a larger, more complex task.  

GNU Project/ FSF 
 GNU project started in 1984       
a)  Goal: Create ‘free’ UNIX clone 
b)  By 1990, nearly all required user space application created.       Example:-gcc, emacs, etc. 
 Free Software Foundation a) Non-Profit organization that manages the GNU project. 

GPL – GNU (General Public License) 
 primary license for open source software 
 encourages free software 
 All enhancements and changes to GPL software must also be GPL 
 Often called ‘copy left’ (All rights reversed) 

Linux Origins 
 LINUS TORVALDS 
a) Finnish college student in 1991 
b) Created Linux Kernel 
 When Linux Kernel combined with GNU applications, complete free UNIX like OS was developed.  

Why Linux? 
 Fresh implementation of UNIX APIs 
 Open source development model 
 Supports wide variety of hardware 
 Supports many networking protocols and Configurations 
 Fully supported 

1) Linux is a UNIX like OS: Linux is a similar to UNIX as the various UNIX versions are to each other. 
2) Multi-User and Multi-tasking:  Linux is a multi-user and multi-tasking operating system. That means that more than one person can be logged on to the same Linux computer at the same time. The same user could even be logged into their account from two or more terminals at the same time; Linux is also Multi-Tasking. A user can have more than one program executing at the same time. 
3) Wide hardware support: Red Hat Linux support most pieces modern x86 compatible PC hardware. 4) Fully Supported: Red Hat Linux is a fully supported distribution Red Hat Inc. provides many support programs for the smallest to the largest companies.   

ARCHITECTURE OF UNIX 

The architecture of UNIX can be divided into three levels of functionality, as shown in Figure . The lowest level is the kernel , which schedules tasks , manages resources, and controls security. The next level is the shell, which acts as the user interface, interpreting user commands and starting applications. The highest level is utilities, which provides utility functions. In other words it is the USER level, as user is the one who operates those utilities.   




FILESYSTEM HIERARCHY SYSTEM   


Linux uses single rooted, inverted tree like file system hierarchy  
  /       This is top level directory    It is parent directory for all other directories    It is called as ROOT directory    It is represented by forward slash (/)    C:\ of windows     

/root  it is home directory for root user (super user)    It provides working environment for root user    C:\Documents and Settings\Administrator  

/home  it is home directory for other users      It provide working environment for other users (other than root)    c:\Documents and Settings\username    

/boot  it contains bootable files for Linux    Like vmlinuz (kernel)..... ntoskrnl    Initrd (INITial Ram Disk)and    GRUB (GRand Unified Boot loader)....  boot.ini, ntldr  

/etc  it contains all configuration files    Like /etc/passwd.....  User info     
/etc/resolv.conf... Preferred DNS     
/etc/dhcpd.conf.... DHCP server    C:\windows\system32\dirvers\  

 /usr  by default soft wares are installed in /usr directory    (UNIX Sharable Resources)    c:\program files  

 /opt  It is optional directory for /usr    It contains third party softwares    c:\program files  

 /bin  it contains commands used by all users    (Binary files)  

 /sbin  it contains commands used by only Super User (root) 

/dev  it contains device files    Like  /dev/hda  ... for hard disk    /dev/cd rom ... for cd rom    Similar to device manager of windows  

/proc  it contain process files    Its contents are not permanent, they keep changing    It is also called as Virtual Directory    Its file contain useful information used by OS    like /proc/meminfo ... information of RAM/SWAP    /proc/cpuinfo ... information of CPU 

/var  it is containing variable data like mails, log files     /mnt  it is default mount point for any partition    It is empty by default  

/media it contains all of removable media like CD-ROM, pen drive 
   
/lib  it contains library files which are used by OS    It is similar to dll files of windows    Library files in Linux are SO (shared object) files     

Basic Unix Commands

date : To display the current sys date.
Ex:  date
Tue Jan 17 13:06:18 EST 2017

date+ format

$date "+%Y-%m-%d"
2017-05-17

$date "+%Y/%m/%d"
2017/05/17

$date "+%m-%d-%Y"

05-17-2017

$date "+%A %m-%d-%Y"

Wednesday 02-10-2017

$date "+%B %m-%d-%Y"

February 02-10-2017

$date "+%m-%d-%y "

02-10-17


cal : To display the current month calendar.
Ex: cal
    January 2017
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

hostname : It will display the system name or host name.
Ex: hostname
oraclelinux
uname      : It will display the operating system version.
Ex: uname
Linux
uname –a : It will display the operating system information in detail
Ex: uname -a
Linux oraclelinux 3.8.13-44.1.1.el6uek.x86_64 #2 SMP Wed Sep 10 06:10:25 PDT 2014 x86_64 x86_64 x86_64 GNU/Linux
ifconfig : To display ip address of the system
Ex: ifconfig eth0 | grep "inet addr"
          inet addr:192.168.220.128  Bcast:192.168.220.255  Mask:255.255.255.0
logname : To display current login name.
Ex: logname
pbandaru
who : To display  all current logged in users details.
Ex: who
pbandaru pts/0        2017-01-17 11:28 (192.168.220.1)
pbandaru pts/1        2017-01-17 12:08 (192.168.220.1)
w : To display detail information about current logged in users
Ex: w
 13:37:19 up  2:11,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
pbandaru  pts/0     192.168.220.1    11:28        7.00s  0.45s     0.06s    w
vsindhu    pts/1     192.168.220.2    12:08        1:27m 0.03s     0.03s   -bash
root          pts/2     192.168.220.3    13:40         3.00s  0.03s     0.03s   -bash
who am i : To display  currently logged in user details [or] one user details
Ex: who am i
pbandaru pts/0        2017-01-17 11:28 (192.168.220.1)
Working with files:
1.cat :-  It used for to create a new file [or] to open an existing file [or] to append data to the existing file.
Create a new file:
Syn: cat>filename 
Ex: $ cat >sample
           ---------
         <Enter the content from the Key board>
          ----------
ctrl+d[save&exit]

     Hello world 
ctrl+d[save&exit]

How to open a file:
Syn: $cat<filename  [or] $cat  filename
Ex: $cat<sample [or] $cat  sample
How to add another line to existing file:
Syn: $cat>>filename
  ---------------
  ---------------
Ctrl+d
Ex:$cat>>sample
What are you doing?
Ctrl+d
Open a file:
$cat<sample
Hi how are you?
I am fine
How to open multiple files:
Syn:  $cat  file1 file2……….
Ex: $cat s1 s2 s3
How to redirect content from multiple files into one file
Ex:$cat  s1 s2 s3>emp [ > : Output Redirection operator]
    $cat emp
How to display the file with line numbers
Ex: $cat -n filename
1 Hi how are you?
2 I am fine

Touch:- It is used to create an empty file. i.e zero byte file.
Syn:$touch filename
Ex: $touch sample
=>creating multiple files
  Syn:$touch file1 file2……… 
  Ex:$touch s1 s2 s3 s4 
Advantage:
     We can create multiple files without wasting disk space(zero size files) at once.
Deleting files:
  1]$rm:-- To delete a file.
   Syn:$rm filename 
   Ex: $rm sample 
        -- It deletes the file permanently
2]$rm -i filename
   Ex: $rm –i sample 
         rm: remove regular file `sample`? y                                                     
Multiple files deletion:
  3]Syn: $rm f1 f2……
   Ex:$rm s1 s2 s3 
           --It deletes files without asking for any conformation
       $rm a1 a2 a3        
Working with directories:
pwd: To display the present working directory.
    Syn:$pwd
1]To create a new directory.
        Syn:$mkdir dirname 
        Ex:$mkdir abc 
2] cd:--To change a directory.
   Syn:$cd dirname 
   Ex: $cd abc 
3]$cd .. 
     This is used to come out of current directory 
cd / : It will switch to the root directory
cd ~ : To switch to your home directory.
cd - : Switches from current path to previous path.

Creating multiple directories:
  Syn:$mkdir dir1 dir2…….

  Ex:$mkdir d1 d2 d3 d4 
Removing directory:
  => $rmdir:– This command is used to remove a directory, directory must be empty and should not contain any files & sub directories.
    Syn:$rmdir dirname 
    Ex:$rmdir abc 
=>$rm –r dirname 
         --It deletes recursively entire directory structure.
  Ex:$rm  -r abc
      $rm –ri abc[It asks for the confirmation before deleting the file]
=>$rm –rf dirname
rm -rf filename
It delete file with a conformation message yes or no. Deletes the file if you select yes option and no for not deleting the file. 
To come out of current working directory.
f = force delete
r = recursive delete
It deletes file at any cost even if the user doesn't have read/write permissions.
Coping a file:
 cp :-To copy a file.
   Syn:$cp sourcefile[must be existed file]  targetfile[existed or new file]
   Ex:$cp sample sample1
Coping multiple files into a directory:
    $cp file1 file2………direct name  
   Ex: $cp emp sample a1 abc [last file must be a directory]
Copying a directory: 
   Syn: $cp –R sourcedir targetdir
  EX: $cp –R  abc  xyz
$mv:- To rename a file [or] to move a file.
    Syn:$mv oldfile  newfile
    Ex:$mv a1 samle
Creating hiden files:
  $cat>.sample
    ----------
    ----------
Ctrl+d
$mv emp .emp  : to hide a file
$mv .emp emp  : to unhide a file
$mkdir .abc       : to create a hidden directory

Viewing list of files:
 1]$ ls   -> It lists all files & directories in the current directory.
2]$ ls -a   -> It lists all files & directories along with hidden files.
3]$ls -r   -> It lists all files & directories in reverse order/descending order.
4]$ls -R  -> It lists recursively all files & sub directories.
5]ls -t  -> It lists all files & sub directories based on date & time of creation.
6]$ls | more   -> It lists all files & sub directories page by page.
7]$ls -F    -> It lists all files & sub directories along with file type.
         File name     type           colour
        abc              ordinary        black
        abc/  dir        dark blue     dark blue
        emp@            link             sky blue
        emp*              exe             green
                              Zip             red
8]$ls –l  -> It lists all files & sub directories in long listed format i.e with 9 fields.

 1.File type
 2.File permission
 3.No.of links
 4.Owner name
 5.Group name
 6.File size in bytes
 7.Date
 8.Time
 9.File name

1]file type:
          -   Regular file
          d  directory file
           l   link file
          b  block  Special file
          c  character Special file

Viewing long files:
$more : It displays the given file contents page by page
  Syn:$more filename 
  Ex:$more sample 
        ------------
        ------------   page1
       ------------
        :
       -----------
      ------------  page2
       -----------
         :
       -----------
      ------------  page3
       -----------
          :q –quick termination

$less It displays the given file contents page by page like more but it allows backward movement as well as forward movement in the file.
  Syn:$less filename 
  Ex:$less sample 
        ------------
        ------------   page1
       ------------
          :
       -----------
      ------------  page2
       -----------
          :
       -----------
      ------------  page3 
       -----------
          :q –quick termination


$head It displays the first n lines from the file.
  Syn:$head –n filename
  Ex:$head -10 sample
   
$tail : It displays the last n lines from the file.
  Syn:$tail –n filename
  Ex:$tail -10 sample

$tail  -f It displays updated lines from the file.
     Syn:$tail -f filename
     Ex:$tail -f sample
            
         ---------- <lines that are getting updated>
         -----------
ctl+c
           
$wc : It counts no.of lines words and characters.
     Syn:$wc  filename
 Ex:$wc  sample
output:  1065  7588 49412 sample  [ Line count, Word Count, Character Count ]

$wc –l filename [displays line count]
$wc –w filename [displays word count]
$wc –c filename [displays character count]
$wc –lw filename [displays lines&words count]
$wc –lc filename [displays lines&characters count]
$wc –wc filename [displays words&characters count]

Comparing files:
1]$cmp: It compares two files character by character.
  Syn:$cmp file1 file2
    If the files are Same -- doesn’t return any value.
    If the files are Diff -- returns char position & line number.

2]$comm: It displays comman lines b/w 2 files.
  Syn:$comm file1 file2

3]$diff: It displays diff lines b/w 2 files.
  Syn:$diff  file1 file2

Zip files:

1]$gzip: This command is used to  zip the file. The output file extension would be .gz 
   Syn:$gzip filename
   Ex:$gzip sample
    Output:   sample.gz (File extension is .gz)
2]$zcat: This is used for to open zip file in readable format.
   $zcat sample.gz
3]$gunzip:  To unzip the file which is .gz format.
   Syn:$gunzip filename
   Ex:$gunzip sample.gz
                    Sample
4] $zip: This command is also used to zip a file but the file format would be .zip
    Syn:$zip options filename.zip filename .....
    Ex: $zip sample.zip sample
    Output: sample.zip (File extension is .zip)
    Ex: $zip -r sample sample1 sample2 sample2
    Output file: sample.zip ( sample.zip contains sample1, sample2 and sample3)
    
5] $unzip: This is command is used to unzip a file which is in .zip format
    Syn:$unzip filename
    Ex:$unzip sample.zip
    Output: sample
   
Administrator Commands:

Login: root
Password:*****
1  1]useradd – To create a new user
#useradd username
Ex: #useradd pavan ( pavan user is created, set the password for user using passwd command)     
2]passwd- To create a password
#passwd username
#passwd pavan
Enter new password : pavan123
Retype password : pavan123
3]userdelto delete a user
#userdel username
Ex: #userdel pavan
4]groupaddto create a new group
#groupadd groupname
Ex: #groupadd weblogic
5]groupdelto delete a new group
#groupdel groupname
6]Add an existing user to a Group
Syn: usermod -a -G groupname username
Ex:   usermod -a -G weblogic pavan  ( pavan will be added to weblogic group)
7] To view a users groups
Syn: id username
Ex: id pavan
uid=505(pavan) gid=506(pavan) groups=506(pavan),505(weblogic)
8] Change user primary Group
Syn: usermod -g groupname username
Ex: usermod -g weblogic pavan   ( pavan's primary group will be changed as weblogic)
id pavan
uid=505(pavan) gid=505(weblogic) groups=505(weblogic)
9] To view user group names
Syn: groups username
Ex: groups pavan
Output: pavan : weblogic jboss ( pavan user groups are : weblogc and jboss) 
10] To view all the groups in the system
Syn: groups
Ex: groups
Output: weblogic jboss
11] Add a new user and assign to the group at once
Syn: useradd -g groupname username
Ex:  useradd -g jboss kalyan

#init 0  : will shutdown the system
#init 6 : will restart the system 

Wild card characters [or]meta characters
( * ? - [] . )
1] *   it matches zero or more characters
2] ?   it matches any single character
3] –   it matches any single character in the given list
4] []  it matches any single character in the given range
5]  .   it matches any single character except the line character
Examples:
1] ls t* –it lists the file starting with ‘t’
2] ls *s- ending with ‘s’.
3] ls b*k- starting with’b’ ending with ‘k’.
4] ls a? –[?any single character.]
   [exactly 2 character . first letter’a’ second letter may be any.]
5]ls bhms?? -it list the file starting with b[or]h[or]m[or]s followed by any of characters [only 2 characters]
6]ls [abcdefgh]
 7]ls [b-k] [p-t] [d-m]*
8]rm * –delete all files in the current directory.
9]rm t* - delete all files starting with ‘t’.
10]cp abc/a1/a11 xyz/x1
Filter commands
grep[globally search a regular expression and print]
*It is used for to search a string regular expression in a given file or files.
Syn: grep pattern filename
Ex:
1] grep pavan sample – It will search pavan string in a sample file
2] grep hello sample – It will search hello string in a sample file
3] grep pavan a1 a2 a3 – It will search pavan string in a1,a2,a3 files
          a1:------
          a2:------
          a3:-----
     
It is used to search the string in multiple files.
4]grep pavan *  ->searches the string in all files which are under current dir.
            a1:------
            a3:--------
            a3:--------
            emp:-------
            sample:------

grep command options: 
1   1)grep -i pavan sample  -- ignore case sensitive
       2 ----
       5 ----
       7 ----
2   2)grep –c pavan sample --  Prints count of the lines
               3.
3)grep –n pavan sample -- Prints lines along with line numbers
              2:--------
              5:-------
              7:--------
3   4)grep -l pavan *  -- lists only file names
       a1
       a3
       emp
       sample
 5)grep-v “pavan soft”  sample –-- It prints the lines that are not matching

Regular Expression:
  Any String contains wild card characters known as regular expression or pattern
  The patterns are classified into 3 types.
1    1) Character pattern
2    2) Word pattern
3    3) Line pattern

The default pattern is character pattern
Examples:
1] grep “pav*” sample
2] grep “b[aeiou]ll” sample
3] grep “b..d” sample
4] grep “pavan” sample

Word pattern: \< \>
\< -- starting of the word
\> -- End of the word
5] grep “\<pavan\> sample – Searches the exact word pavan
6] grep “\<pavan” sample – Searches word starting with pavan
7] grep “pavan\>” sample – searches word ending with pavan
8] grep “\<[0-9][0-9][0-9][0-9]\>” sample

Line Pattern:
^ - Start of the line
$ - End of the line

1] grep “^d” sample – Searches line starting with d
2] grep “^the” sample – Searches the starting with the
3] grep “^\<the\>” sample – Line contains only the
4] grep “t$” sample  --- line ending with t
5] grep “[0-9]$” sample – Line ending with digit
6] grep “^[bkt]” sample – Line starting with b or k or t
7] grep “^unix$” sample – Line should contain only “unix”
8] grep “^.” Sample – It will display line starting with any character
9] grep “\$$” sample – Line ending with ‘$’
10] grep “^$” sample – Blank lines
11] grep –v “^$” sample – Prints all non empty lines

grep –v “^$” sample >temp
mv temp sample

fgrep: [faster grep]
It is used for to search multiple strings. But it doesn’t allow to search regular expressions ( wild card search */?/./[]/- is not allowed )

Ex: fgrep “pavan
      >unix
      >linux “ sample
It searches either pavan or unix or linux
egrep:[ Extended grep]
It is a combination of grep and fgrep
Ex: egrep “pavan
      >unix
      >linux “ Sample
egrep “^d” sample

sed:  It is used to search and replace the string
syn: sed “s/oldsting/newstring/g” filename
ex:
sed “s/unix/linux/g” sample
sed “s/UNIX/Linux/gi” sample
sed “s/^unix/linux/gi” sample
sed “s/^$/hellounix/g” sample
sed “s/unix//gi” sample


tr: To translate a character or to replace a character       
>-redirecting o|p
<-redirecting i|p
$tr “a” “t” <sample
-where ever ’a’ is there it will replace with ‘t’
$tr “aciou” “AEIOU”  <sample.
$tr “[a-z]” “ [A-Z]”  <sample
   -the hole file translated into upper case
-$tr “[a-z A-Z]” “[A-Z a-z]”   <sample
- where ever lower case is there that will be translated into uppercase
-where ever  uppercase is there that will be translated into lowercase
$tr “[a-z]”  “[A-Z]”   <sample >temp
        $mv temp sample.
Flat file-normal text file
  Delimiter – filed separator.
  Default delimiter – tab key.
  In a file data enter with delimiter known as flat file the default delimiter is tab key delimiter filed separate.
Enter key means record separate
$cat>stud
101 Priya 515 unix Hyd
102 pavan 444 linux sec
Ctrd.
Custom delimiter flat file
          $cat  >emp
          101,priya,5000,10
          102,pavan,6000,12     Ctrld.
Fixed width flat file       
     In a file every record has the same length.
$cat > account
1111nnnnnntttttttbbbbbb
        1-5 –Ac no
        6-11 –Ac name
        12-15 -acc type
        16-20 –Balance
Cut command:
It is used for to retrieve required fields (or) character from a given file.
       Syn:  $cut –f 2, 4 stud.
                $cut –f 2-5 stud.
                $cut –c 1-10 stud.
                $cut –c 6-11, 16-20 account
                $ cut –d”,” –f 2, 4 emp.
Paste:     It is used for to join two or more files horizontally by using delimiter.
Cat>states
AP
TN
KR
Cat>cities
HYD
CHEN
BAN
     

Syn: $paste states cities
                AP HYD
                TN CHEN
                KR BAN
$ paste –d”,” states cities
Sort: It is used for to sort file contents.
         By default it sorts the file contents based on ascii value
-            The default mode is ascending order

$ sort filename
$ sort sample
     a
    de
    ha
    he
    te

(1)$ sort –r sample   - It displays the file in reverse order
(2)$ sort –u sample  - displays unique lines ( won't display duplicate lines )

(3)$ sort num
    file - num
            130
            150
            1
            230
            24
            25
            26
            27
            38
            678
output: 
1
100
130
150
230
24
25
26
27
38

678


(4)$ sort –n num    [ numeric comparsion ]
1
24
25
26
27
38
100
130
150
230
678
    
5] sort –nu num       [ numeric comparison ignore duplicates ]
15
23
125
225
456
1025

Uniq:
It displays unique lines in the given file but the file contents should be in sorted order
Syn: uniq filename
Ex:
1] uniq sample
o/p
aaa
bbb
ddd
hhh
ppp

2] uniq –u sample
It displays non duplicated lines
o/p
 bbb
 ppp

3] uniq –d sample
It displays only duplicated lines
o/p
 aaa
 ddd
 hhh

4] How to delete duplicate lines from a file.
 Note : File should be in sorted order first 
 uniq –u sample>temp
 mv temp sample
Piping:
Pipe(|): It is used to combine two or more commands . The output or result of the first command sequence is used as the input to the second command sequence.
who | wc –l   -- It counts no of users working in the server

1] Count total no of files in the current directory.
    ls | wc –l
2] Display all sub directories in the current directory
    ls -l | grep “^d”
3] Count total no of sub directories in the current directory
    ls –l | grep “^d” |wc –l
3] Display the lines from 11-30 from sample file
    head -30 sample | tail -20
4] Display unix students names & phone no’s in ascending order of name
grep unix stud | cut –f 2,3 | sort
Links

tee:
It is used for to write data to the screen and to the file
grep unix stud | cut –f 2,3 | sort | tee f1

File Permissions:

By Default below permissions will be assigned to the file or directory when you login with the user and create a file

Default file permissions  rw-|rw-|r--    [ 664 ]

Default Directory Permisions    rwx|rwx|r-x   [ 775 ]
                                                     u    g      o
chmod: It is used for to change file permissions
  Syn: chmod who/[+1-1=]/ permission file name.
   Who:
User (or) owner -  u   
   Others -   o           
   Group -   g                                                 
    All    -    a         
      
Permission

read-r 
write-w
execute-x

+   add permission
 -   deny permission    
=   assign permission          
                               
ls -l sample       
rw-rw-r--
(1)$ chmod g+x sample
        rw-rwx-r--
(2)$ chmod u+x ,g-w sample
        rwx-r-x- -
(3)$ chmod g=w sample
 rwx-w-r--
It will assign write permission only and removes other permission

octal code:
           read-4
           write-2
           execute-1
$ chmod 756   sample
           u    g   o  
         rwx-r-x-rw
$ chmod 643 sample
          rw-r---wx
$ chmod 755 sample
          rwx-r-x-r-x
chown: To change user/owner of the file
chown oracle sample
-rw-r--r-- 1 oracle root 0 Feb  1 02:37 sample
chgrp: To change the group of the file
-rw-r--r--. 1 oracle oracle 0 Feb  1 02:37 sample

Umask: To change default file permissions
The default umask is ‘002’
umask:
o/p 002
Default file permissions RF-> rw-rw-r—(644)
Default directory permission DF-> rwxrwxr-x(775)
umask 026
touch a1 a2 a3 (640)
mkdir d1 d2 d3 (751)
umask 042
touch x1 x2 x3 (624)

mkdir y1 y2 y3 (735)

Disk Related  Commands:

df:
This command is used to find disk space of the file System.
Syn: df  
[root@oraclelinux ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1       82438832 3456896  74771248   5%  /


tmpfs             890204       0              890204    0%   /dev/shm

df -h:
This command is used to find disk space of the file System in human readable format
[root@oraclelinux ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        79G  3.3G   72G   5%     /
tmpfs           870M     0    870M   0%    /dev/shm

df -k:
This command is used to find disk space of the file system in  Kilo bytes.
[root@oraclelinux ~]# df -kFilesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1       82438832 3456912  74771232   5% /

tmpfs             890204       0    890204   0% /dev/shm


df -kh
This command is used to find disk space of the file system Kilo bytes in human 
Syn:df -kh
[root@oraclelinux ~]# df -kh
It displays diskspace of all file systems

df -a:
Syn:df -a
[root@oraclelinux ~]# df -a

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1       82438832 3456904  74771240   5%    /
proc                   0                 0              0            -       /proc
sysfs                  0                 0              0            -        /sys
devpts                0                 0              0            -        /dev/pts
tmpfs             890204           0           890204   0%       /dev/shm
none                   0                0                0         -           /proc/sys/fs/binfmt_misc


df -i
This command is used to find disk space in Inodes ,I used, Ifree ,I use
Syn:df -i.


[root@oraclelinux ~]# df -i
Filesystem      Inodes  IUsed   IFree IUse% Mounted on
/dev/sda1      5242880 119160 5123720    3% /

tmpfs           222551      1  222550    1% /dev/shm

du:

This command is used to find disk usage of the file System.
Syn: du
[root@oraclelinux ~]# du
4       ./.local/share
8       ./.local
8       ./.gconfd
4       ./dir2
4       ./.kde
4       ./.gconf
4       ./dir1

140   

du -sh dir
It displays dir directory size in human readable format

[root@oraclelinux ~]# du -sh dir1
4.0K    dir1

du -sh 
It displays total size of the directory in human readable format

Syn: du -sh
[root@oraclelinux ~]# du -sh
140K    .

du -sh  *

Syn: du -sh  *
[root@oraclelinux ~]# du -sh *
4.0K    anaconda-ks.cfg
4.0K    dir1
4.0K    dir2
52K     install.log
12K     install.log.syslog
4.0K    number.txt
4.0K    sample1.txt



4.0K    sample.txt

du -s:
It display total size of the current directory
Syn: du -s
[root@oraclelinux ~]# du -s
140     .

du -a or du -all
It displays the space that each file is taking include Sub directories.
Syn: du -a
[root@oraclelinux ~]# du -all
52      ./install.log
4       ./.local/share
8       ./.local
4       ./.gconfd/saved_state
8       ./.gconfd
4       ./.bashrc
12      ./install.log.syslog
4       ./.bash_profile
4       ./dir2
4       ./.tcshrc
4       ./.cshrc
0       ./.kde/tmp-oraclelinux
4       ./.kde
4       ./.bash_logout
4       ./.gconf
140     .

Process Related Commands
ps:
This is used for to show current running process.
Syn: ps
[root@oraclelinux ~]# ps
  PID TTY          TIME CMD
15343 pts/2    00:00:00 bash
15575 pts/2    00:00:00 ps

ps -aux:

UID        PID  PPID  C STIME TTY          TIME   CMD

root         1         0      0   Jan30    ?          00:00:02  /sbin/init
root         1  0.0  0.0  19408  1528 ?        Ss   Jan30   0:02 /sbin/init
root         2  0.0  0.0      0     0 ?        S    Jan30   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    Jan30   0:00 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   Jan30   0:00 [kworker/0:0H]

root         7  0.0  0.0      0     0 ?        S<   Jan30   0:00 [kworker/u:0H]

UID - User Id
PID - Process Id
PPID - Parent Process Id
C -  CPU Usage and Scheduling 
STIME - Time when process Started
TTY - Terminal assosicated with the process
TIME -The amount of CPU time used by the process



CMD - Command

ps -ef     [ e=every process , f =full-format listing ]
[root@oraclelinux ~]# ps -ef
This command is used to see every process in the system using BSD Syntax
Syn:ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
It is used find the cpu usage, memory usage, uptime, load average and process states
Syn:Top
top - 18:42:13 up 1 day, 23:39,  3 users,  load average: 0.13, 0.04, 0.05
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 nohup:
This command will execute the process even if you logout from the System.
 Syn: nohup cmd &


top:

Tasks:  94 total,   1 running,  93 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.3%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1780408k total,   482940k used,  1297468k free,    93720k buffers
Swap: 20970492k total,        0k used, 20970492k free,   168220k cached

16178 root      20   0 98.1m 4736 3728 S  0.3  0.3   0:01.61 sshd
    1 root      20   0 19408 1528 1224 S  0.0  0.1   0:02.46 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.12 kthreadd
    3 root      20   0     0    0    0 S  0.0  0.0   0:00.08 ksoftirqd/0

    5 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 kworker/0:0H

PID - Process Id
USER - User Name of the process's owner
PR - Current Priority of the process
NI - Nice Value
VIRT - Virtual Memory
RES - Resident Memory - Current amount of the memory that resides in physical memory
SHR - Shared Memory 
S - Process State
%CPU - CPU Percentage
%MEM - Memory Percentage 
TIME - Number of system and user cpu seconds

COMMAND - Name of the command

Ex: nohup sleep 300 &

KILL

kill -9 pid

To kill multiple process

kill -9 pid1 pid2 pid3 pid4

[root@oraclelinux ~]# nohup sleep 600 &
[1] 16577
[root@oraclelinux ~]# ps -ef | grep sleep
root     16577 16555  0 19:15 pts/0    00:00:00 sleep 600
root     16579 16555  0 19:15 pts/0    00:00:00 grep sleep
[root@oraclelinux ~]# kill -9 16577
[root@oraclelinux ~]# ps -ef | grep sleep
root     16582 16555  0 19:15 pts/0    00:00:00 grep sleep
[1]+  Killed                  nohup sleep 600

Network Related Commands

netstat ( network statistics):
This command is used to display the all Ip- address and port number.
Syn: netstat
This is used to connect the remote servers.
 Syn:telnet ip-address port
ftp ( file transfer protocol )
This is used to transfer the files in one system to another system.
Syn: ftp Ip-address.
ping:
This is used to cheek the network connectivity from source system to destination system.
Syn:ping Ipaddress
C:\Users\pavan>ping 192.168.224.129
Pinging 192.168.224.129 with 32 bytes of data:
Ping statistics for 192.168.224.129:


Reply from 192.168.224.129: bytes=32 time<1ms TTL=64
Reply from 192.168.224.129: bytes=32 time<1ms TTL=64
Reply from 192.168.224.129: bytes=32 time<1ms TTL=64
Reply from 192.168.224.129: bytes=32 time<1ms TTL=64

    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms



Other Commands
scp:
This is used to secure copy the files in unix system.
Syn:scp your_username@remotehost:filename /dirpath
find:
This command is used to search files.
Syn:file <path> -name Filename
tar:
tar command is primary archive utility.
Syn:tar cmd
tar -cvf:
This is used to create the all files stored in one file (or) Backup file purpose , you can use this one.
syn:tar -cvf file.tar f1 f2 f3 f4.
tar -xvf:

This is used to extract the all files stored in one file.
Syn :tar -xvf file.tar files.

crontab
********
It is a job scheduling tool

crontab -l (it lists all crontab jobs)
crontab -e (edit cront jobs)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
        -i      (prompt before deleting user's crontab)

crotab -e

esc+i
-----
------
------

esc: w -save
   : q -quit with out save
   : wq -save and exit
   : wq! - over write


Field  Description Allowed Value
MIN  Minute field          0 to 59
HOUR        Hour field   0 to 23
DOM Day of Month      1-31
MON Month field 1-12
DOW Day Of Week       0-6
CMD Command   Any command to be executed.

Ex:1

30 08 10 06 * /home/ramesh/full-backup

30 – 30th Minute
08 – 08 AM
10 – 10th Day
06 – 6th Month (June)
* – Every day of the week
EX:2
00 11,16 * * * /home/ramesh/bin/incremental-backup

00 – 0th Minute (Top of the hour)
11,16 – 11 AM and 4 PM
* – Every day
* – Every month
* – Every day of the week

EX:3

00 09-18 * * * /home/ramesh/bin/check-db-status

00 – 0th Minute (Top of the hour)
09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
* – Every day
* – Every month
* – Every day of the week



vi editor
*********
Useful vi editor commands

vi filename
view filename

insertmode
commnad mode (default mode)
ex command mode

esc is used to shift from one mode to other mode.
  |k
h<- ->l
  j|

insert mode
-----------
esc+

i         Inserts text before current cursor location.
I        Inserts text at beginning of current line.
a        Inserts text after current cursor location.
A       Inserts text at end of current line.
o        Creates a new line for text entry below cursor location.
O       Creates a new line for text entry above cursor location.

command mode
------------

deletion

x        Deletes the character under the cursor location.
X       Deletes the character before the cursor location.
dw     Deletes from the current cursor location to the next word.
d^      Deletes from current cursor position to the beginning of the line.
d$      Deletes from current cursor position to the end of the line.
D       Deletes from the cursor position to the end of the current line.
dd      Deletes the line the cursor is on.
10dd

position:
---------
0 or | Positions cursor at beginning of line.
$        Positions cursor at end of line.
W      Positions cursor to the next word.
B       Positions cursor to previous word.
1G     Move to the first line of the file
nG
G       Move to the last line of the file

copy and paste:
---------------
yy      Copies the current line.
yw     Copies the current word from the character the lowercase w cursor is on until the end of the word.
p       Puts the copied text after the cursor.

set commands:
-------------
:set ic Ignores case when searching
:set ai Sets autoindent
:set noai      To unset autoindent.
:set nu         Displays lines with line numbers on the left side.
:set sw        Sets the width of a software tabstop. For example you would set a shift width of 4 with this command: :set sw=4
:set ws        If wrapscan is set, if the word is not found at the bottom of the file, it will try to search for it at the beginning.
:set wm       If this option has a value greater than zero, the editor will automatically "word wrap". For example, to set the wrap margin to two characters, you would type this: :set wm=2
:set ro         Changes file type to "read only"
:set term      Prints terminal type
:set bf          Discards control characters from input

replacing text:
---------------
:%s/search/replace/g

Word and Character Searching:
-----------------------------
The / command searches forwards (downwards) in the file.
The ? command searches backwards (upwards) in the file.


searching log in vi editor

view Adminserver.log
esc+shift+g
curser position will be at the end of the line
:se ic
?error
?running
?shutdown

How to delete files which are older than 15 days.

find . -type f -mtime +20 ! -name '*.log' >>list
ls -lrt $(<list)
rm -rf $(<list)



Interview Questions

<><><><><><><>

  find / -name sample.txt
2.Display the numbers in sorted order
   sort -nu
3.Display the updated lines in file
  tail -f filename
4.How to zip a Directory
  gzip -r /hom/bea/app
5.How to kill a prosess.
   kill -9 pid
6.Display all java process in unix.
    ps -ef |grep java
7.How to delete 10 lines in vi editor.
    10dd
8.How to search a string and replace tin  vi editor.
     :"%s/old string /new string/ gi"
9.How to search a string in vi editor from bottom to top.
     ?string name
10.How to insert a line above the current line in vi editor .
      esc+o
11.How to rename a file .
     mv file1 file2
12. copy the content one directory to another directory.
        cp -r dir1/file1 dir2/file2
13. How can delete blank lines in directory.
      grep -v "^$" sample > temp
      mv temp sample
14.How can replace astring in unix.
     sed "s/oldstring/newstring/g" filename
15.Syntax for zip and unzip afile.
     gzip filename
     guzip filename.gz
16.How can display top 10 disk usage files.
  du -sh *|sort -nr|head -10
17.How to retrieve a field in a file.
   cut -f 1,2 stud.
18.what is command search a string in a file.
     grep
19).How to go to the end of the line in Vi editor?
     G
20).Copy 10 line in vi editor?
      10yy
21).How to go to insert mode in vi editor?
      Esc+i
22).How to search a string in a file(filename=sample,string=weblogic)?
     grep weblogic sample
23).How to copy file from one unix to other unix system and syntax (file= sample.txt, target host=192.168.11.128,target file path=/home/bea)?
     scp -rp sample.txt username@192.168.11.128:/home/bea
24).Copy file from path to other tree structure is given below?
25).How to find out CPU utiligation?
      top
26).How to execute a script using nohup (script name=startweblogic.sh)?
      nohup ./startWeblogic.sh &
27).Which command is used to search and replace a string?
       sed
28).wich command is used to search a string in multiple files?
      fgrep

A) /weblogic
2)how to save and quit from vi Editor's?
a)wq
3)what are the Advantages of nohup command?
A)nohup will execute the process if you layout system.
Syn:nohup &.
4)Differance b/w the ping and tracert?
Traceroute takes advantage of TTL, time to live, to determine the address of each hop between souce and destination. Each router decrements TTL before forwarding it, this mechanism is designed to prevent packets from circulating endlessly. When a router is unable to decrement TTL it sends a message back to the originator. This provides the address of each router. With that knowledge a ping request is sent to each other. 

5)How to execute unix commands in vi?
A) :! unix command
6)tar dir1 dir2 dir3 and new_dir?
A)syn:tar -cvf new_dir.tar dir1 dir2 dir3.
7)How to display the ipaddress and portnumber?
A)netstat -anp
8)how to delete directory with recursion and force?
A)rm -rf filename.
9)How to Open a file with page to page ?
A)more filename
10)How to Hide a file (file name=tuxedo)?
A)mv tuxedo .tuxedo
11)How to do undo in vi Editor's?
A)u
12)How to goto 100 Line in Vi Editor's?
A)100L
13)How to display last 100 Line from a file?
A)tail -100 filename
14)How to Reterive the fields from a file?
A)cut
15)How to zip a directory?(/home/directory)?
A)gzip -r /home/directory
16)How to go to end of the line in Vi?
A)G
17)Display the directiory count in current dir?
A)ls -lrt !wc -l
18)How to display all files ending with "log"?
19)How to appeand data to the existing file?
A)cat>>filename
20)How to find out the diskspace of the fileSystem?
A)df -sh
21)write A syntax for the scp Commands?
A) Scp filename root@ipaddress:filename.
22)How to display the updated lines in file ?
A)tail -f filename.
23)How to display the ipaddress and portnumber?
A)netstat -anp
24)which command is used to connect to the remote server?
A)telenet ipaddress.
25)How to repalce and String in vi Editor's?
A) sed %s/oldstring/newstring/g
25)How to repalce and String in unix?
A) sed s/oldstring/newstring/g filename
26)How to display top 10 lines from a file?
A) head -10 filename.
27)syntax for tar and untar a file?
A)tar -cvf filename.tar file1 file2 file3.
  tar -Xvf filename.tar
28)How to display hiddean a file?
A)ls -a.
29)How to Delete a blank line from a filename?
A)grep "^v" sample >temp
  mv temp filename
30)How to display all cuurent running process?
A)ps -ef
31)syntax for zip and unzip file?
A)gzip filename.
  gunzip filename.gz