Saturday, December 14, 2013

shellscript to sort numbers in ascending, descending order with the arguments supplied.

shellscript to sort numbers in ascending, descending order with the arguments supplied.

cat sort.sh
if [ $# -eq 0 ]
then
echo "Enter valid Argument "
exit
fi
for i in $*
do
echo $i >> tmpfile
done
echo "Your sorted data are below"
sort -nr tmpfile
echo "Script Ends"

output:

sh sort.sh  3 4 5 6 7
Your sorted data are below
7
6
5
4
3

Script Ends

Ascending order use sort -n

sh sort.sh  3 4 5 6 7

Your sorted data are below
3
4
5
6
7
Script Ends




bash: locate: command not found

bash: locate: command not found

1. yum install mlocate*
2.  locate filename

    locate: can not open `/var/lib/mlocate/mlocate.db': No such file or directory

3. updatedb

4. locate filename.will display the file location.






Wednesday, November 6, 2013

Configure Logs for LVM in linux

 Log all LVM commands

[root@test ~]# cp /etc/lvm/lvm.conf /etc/lvm/lvm.conf_backup
[root@test ~]# vi /etc/lvm/lvm.conf


log {

    # Controls the messages sent to stdout or stderr.
    # There are three levels of verbosity, 3 being the most verbose.
    verbose = 0

    # Should we send log messages through syslog?
    # 1 is yes; 0 is no.
    syslog = 1

    # Should we log error and debug messages to a file?
    # By default there is no log file.
    #file = "/var/log/lvm2.log"

    # Should we overwrite the log file each tisme the program is run?
    # By default we append.
    overwrite = 0

    # What level of log messages should we send to the log file and/or syslog?
    # There are 6 syslog-like log levels currently in use - 2 to 7 inclusive.
    # 7 is the most verbose (LOG_DEBUG).
    level = 0

   # Format of output messages
    # Whether or not (1 or 0) to indent messages according to their severity
    indent = 1

    # Whether or not (1 or 0) to display the command name on each line output
    command_names = 0

    # A prefix to use before the message text (but after the command name,
    # if selected).  Default is two spaces, so you can see/grep the severity
    # of each message.
    prefix = "  "

    # To make the messages look similar to the original LVM tools use:
    #   indent = 0
    #   command_names = 1
    #   prefix = " -- "

    # Set this if you want log messages during activation.
    # Don't use this in low memory situations (can deadlock).
    # activation = 0
}

2 modifications to be done

Uncomment the line # file = “/var/log/lvm2.log

Change the level = 0 to a value between 2 and 7.

What level of log messages should we send to the log file and/or syslog?

    # There are 6 syslog-like log levels currently in use - 2 to 7 inclusive.
    # 7 is the most verbose (LOG_DEBUG).

Enter level=7

save and exit.

Just test it with the lvm command and view the logs

This is not the right syntax, just showed here for example.

[root@test ~]# lvcreate VG1
  Please specify either size or extents
  Run `lvcreate --help' for more information.

[root@test ~]# tail -f /var/log/lvm2.log
activate/activate.c:471   Getting target version for striped
ioctl/libdm-iface.c:1687   dm versions   OF   [16384] (*1)
lvcreate.c:363   Please specify either size or extents
lvcreate.c:768   <backtrace>
lvmcmdline.c:623   Run `lvcreate --help' for more information.
lvmcmdline.c:1123   Completed: lvcreate VG1
cache/lvmcache.c:1602   Wiping internal VG cache
metadata/vg.c:74   Freeing VG #orphans_lvm1 at 0x1f09d90.
metadata/vg.c:74   Freeing VG #orphans_pool at 0x1f0e600.
metadata/vg.c:74   Freeing VG #orphans_lvm2 at 0x1f13110.



Restore removed LVM partition

[root@test ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              20G   18G  698M  97% /
/dev/mapper/VG1-backup
                       49G  197M   46G   1% /backup
/dev/mapper/VG1-downloads
                       50G   52M   47G   1% /downloads
/dev/mapper/VG1-home   49G  181M   46G   1% /home
tmpfs                 979M     0  979M   0% /dev/shm
/dev/mapper/VG1-public
                       59G  641M   55G   2% /public

[root@test ~]# lvremove /dev/VG1/public
  Can't remove open logical volume "public"

[root@test ~]# dmsetup info -c /dev/VG1/public
Name             Maj Min Stat Open Targ Event  UUID                                                        
VG1-public       253   7 L--w    1    2      0 LVM-8FQ0SbqlR7hlFHpG2rnxUQ2WvafgEa1yMvxZcKGIIj6Rippe9VdxO4MptcgKaO1O

Device mapper maintains entry for logical drives, so first we need to delete it from there. Then it can't restrict us while removing logical volumes which are in open status.
rom above table you can see Open = 1 which means the logical volume testlv is open status, so we need to delete entry for this logical volume from dmsetup table then only we can delete any logical volume with open status.  let's do it

[root@test /]# dmsetup remove --force /dev/VG1/backup
device-mapper: remove ioctl failed: Device or resource busy
Command failed

[root@test ~]# umount /public/
[root@test ~]# lvremove /dev/VG1/public
Do you really want to remove active logical volume public? [y/n]: y
  Logical volume "public" successfully removed
[root@test ~]# lvs
  LV        VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  backup    VG1  -wi-ao  50.00G
  downloads VG1  -wi-ao  54.00G
  home      VG1  -wi-ao  50.00G
  images    VG1  -wi-a-  50.00G
  stripe    VG1  -wi-a- 128.00M
[root@test ~]# vgcfgrestore --list VG1                        
                                                                   
  File:         /etc/lvm/archive/VG1_00011-1959345004.vg          
  VG name:      VG1                                                
  Description:  Created *before* executing 'lvremove /dev/VG1/snap1'
  Backup Time:  Wed Sep 11 20:23:22 2013                          

 
  File:         /etc/lvm/archive/VG1_00012-1817284231.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvcreate -s --name backup-snap --size 1G /dev/VG1/backup'
  Backup Time:  Wed Sep 11 20:48:21 2013                                                          

 
  File:         /etc/lvm/archive/VG1_00013-1680048821.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/backup-snap'
  Backup Time:  Mon Sep 16 09:13:22 2013                                

 
  File:         /etc/lvm/archive/VG1_00014-2084989037.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/dbba'
  Backup Time:  Mon Sep 16 09:13:47 2013                        

 
  File:         /etc/lvm/archive/VG1_00015-1654653445.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/public_snap'
  Backup Time:  Mon Sep 16 09:14:14 2013                                

 
  File:         /etc/lvm/archive/VG1_00016-1108210628.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/test1'
  Backup Time:  Mon Sep 16 09:14:25 2013                          

 
  File:         /etc/lvm/archive/VG1_00017-319369726.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/home1'
  Backup Time:  Mon Sep 16 09:14:35 2013                          

 
  File:         /etc/lvm/archive/VG1_00018-1088715738.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/home2'
  Backup Time:  Mon Sep 16 09:14:45 2013                          

 
  File:         /etc/lvm/archive/VG1_00019-950166769.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/dbbackup'
  Backup Time:  Mon Sep 16 09:15:03 2013                            

 
  File:         /etc/lvm/archive/VG1_00020-722332065.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvresize -L -1G /dev/VG1/public'
  Backup Time:  Mon Sep 16 09:20:38 2013                                  


  File:         /etc/lvm/archive/VG1_00021-227400998.vg
  VG name:      VG1
  Description:  Created *before* executing 'lvresize -L +1G /dev/VG1/public'
  Backup Time:  Mon Sep 16 09:21:18 2013


  File:         /etc/lvm/archive/VG1_00022-271935578.vg
  VG name:      VG1
  Description:  Created *before* executing 'vgsplit VG1 test /dev/sda5'
  Backup Time:  Mon Sep 16 10:01:04 2013


  File:         /etc/lvm/archive/VG1_00023-196107041.vg
  VG name:      VG1
  Description:  Created *before* executing 'lvremove /dev/VG1/public'
  Backup Time:  Tue Sep 17 09:35:07 2013


  File:         /etc/lvm/backup/VG1
  VG name:      VG1
  Description:  Created *after* executing 'lvremove /dev/VG1/public'
  Backup Time:  Tue Sep 17 09:35:07 2013

[root@test ~]# vgcfgrestore -f /etc/lvm/archive/VG1_00023-196107041.vg
  Please specify a *single* volume group to restore.

You also need to specify the volume group name

[root@test ~]# vgcfgrestore -f  /etc/lvm/archive/VG1_00023-196107041.vg VG1
  Restored volume group VG1
[root@test ~]# lvs
  LV        VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  backup    VG1  -wi-ao  50.00G
  downloads VG1  -wi-ao  54.00G
  home      VG1  -wi-ao  50.00G
  images    VG1  -wi-a-  50.00G
  public    VG1  -wi---  60.00G
  stripe    VG1  -wi-a- 128.00M

Logical volume public is present now.

lvdisplay

  --- Logical volume ---                                    
  LV Name                /dev/VG1/public                    
  VG Name                VG1                                
  LV UUID                MvxZcK-GIIj-6Rip-pe9V-dxO4-Mptc-gKaO1O
  LV Write Access        read/write                          
  LV Status              NOT available                      
  LV Size                60.00 GB                            
  Current LE             1920                                
  Segments               2                                  
  Allocation             inherit                            
  Read ahead sectors     auto            

The status is not available. Make it active

[root@test ~]# lvchange -a y /dev/VG1/public

[root@test ~]# lvdisplay
--- Logical volume ---                                    
  LV Name                /dev/VG1/public                    
  VG Name                VG1                                
  LV UUID                MvxZcK-GIIj-6Rip-pe9V-dxO4-Mptc-gKaO1O
  LV Write Access        read/write                          
  LV Status              available                          
  # open                 0                                  
  LV Size                60.00 GB                            
  Current LE             1920                                
  Segments               2                                  
  Allocation             inherit                            
  Read ahead sectors     auto                                
  - currently set to     256                                
  Block device           253:1  


Now it is in available state, mount and see the data is present or not.

[root@test ~]# mount /dev/VG1/public /public

[root@test ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              20G   18G  699M  97% /
/dev/mapper/VG1-backup
                       49G  197M   46G   1% /backup
/dev/mapper/VG1-downloads
                       50G   52M   47G   1% /downloads
/dev/mapper/VG1-home   49G  181M   46G   1% /home
tmpfs                 979M     0  979M   0% /dev/shm
/dev/mapper/VG1-public
                       59G  641M   55G   2% /public
[root@test public]# ls
testdir
[root@test public]# cd testdir/
[root@test testdir]# ls
1    11  14  17  2   22  25  28  30  33  36  39  41  44  47  5   52  55  58  60  63  66  69  71  74  77  8   82  85  88  90  93  96  99
10   12  15  18  20  23  26  29  31  34  37  4   42  45  48  50  53  56  59  61  64  67  7   72  75  78  80  83  86  89  91  94  97
100  13  16  19  21  24  27  3   32  35  38  40  43  46  49  51  54  57  6   62  65  68  70  73  76  79  81  84  87  9   92  95  98

the data are present.




Configure Swapiness in Linux

Setting swapiness means that virtual swap memory is not used until the physical memory gets full. To avoid this you can set swapiness value.

Linux system comes with a default of 60, meaning that the swap file will be used fairly often if the memory usage is around half of my RAM.


You can check your system's swappiness value by running:

[root@test ~]# cat /proc/sys/vm/swappiness
60

So If i have 4 GB of RAM, and I like to turn that down to 10 or 15. The swap file will then only be used when my RAM usage is around 85 or 90 percent.

To change the system swappiness value

vim /etc/sysctl.conf as root. Then, change or add this line to the file:

vm.swappiness = 10

Reboot for the change to take effect

You can also change the value while your system is still running

sysctl vm.swappiness=10

You can also clear your swap by running swapoff -a and then swapon -a as root instead of rebooting to achieve the same effect.

To calculate your swap Formula

free -m (total) / 100 = A

A * 10


[root@test ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3950       2262       1687          0        407        952
-/+ buffers/cache:        903       3047
Swap:         1953          0       1953

    so total is 3950 / 100 = 39.5 * 10 = 395

It means that when 10 % 395 MB of ram left, then it start using swapiness.

Saturday, October 5, 2013

RPM Usage in Linux

1.How to install RPM in linux ?


 #rpm –ivh packagename  // To install a RPM we need to use -i option.

2. How to remove a RPM ?


 #rpm -e packagename  // To remove a RPM file we need to use -e option.

3.How to Upgrade a RPM ?


 #rpm -Uvh packagename // To upgrade a RPM we need to use -U option.

4.How to check installed RPM?

 #rpm -qa packagename   // To list or find a installed RPM we need to use -q option.

5. How to List all files of an installed RPM package

 #rpm -ql packagename   // To list all files of installed rpm we need to use -ql ( query list) option.

6.How to Install a RPM Package Without Dependencies

 #rpm -ivh --nodeps packagename // To install a RPM without any dependencies we need to use --nodeps option.

7.How to check dependencies of RPM Package before Installing

 #rpm -qpR packagename   // To check the dependeny for a RPM we need to use -qpR option.


  -q : Query a package
  -p : List capabilities this package provides.
  -R: List capabilities on which this package depends.

8. How to Check an RPM Signature for a Package?


  #rpm --checksig  packagename.rpm    // We need to use the option --checksig for signature verification.

9. How to List Recently Installed RPM Packages


  #rpm -qa --last   // To list recently installed RPM we need to use --last option

10. How to Query a file that belongs which RPM Package


  #rpm -qf /etc/passwd         // To find a file owned by which package

11.Get the Information of RPM Package Before Installing


  #rpm -qip packagename

12. How to Verify a RPM Package


  #rpm -Vp  // To verify the installed RPM

13.How to List all Imported RPM GPG keys


  #rpm -qa gpg-pubkey*


14. How to Remove an RPM Package Without Dependencies


  #rpm -ev --nodeps packagename // same --nodeps option we need to use



Backup datbases and tables in Mysql

Backup all databases uncompressed from the command line (not from within MySQL):

backup: mysqldump -u root -pmypass --all-databases > alldatabases.sql

restore full: mysql -u username -pmypass < alldatabases.sql (no space in between -p and mypass)

restore single: mysql -u username -pmypass mydb < mydb.sql (no space in between -p and mypass)

Backup all databases compressed from the command line (not from within MySQL):

With bzip2: mysqldump --all-databases | bzip2 -c > databasebackup.sql.bz2 (use bunzip2 to uncompress)

With gzip: mysqldump --all-databases | gzip > databasebackup.sql.gz (use gunzip to uncompress)


Mini script to backup all databases and tables using gzip:

#!/bin/sh
date=`date -I`
mysqldump --all-databases | gzip > /var/backup/dbbackup-$date.sql.gz

Backup a specific database only:
mysqldump -u username -pmypass databasename > backupfile.sql

Backup database structure only
mysqldump --no-data --databases databasename > structurebackup.sql


Backup a specific database and specific tables within that database only:
mysqldump --add-drop-table -u username -pmypass databasename table_1 table_2 > databasebackup.sql

The syntax for the command to issue is:
mysqldump -u [username] -p[password] [databasename] [table1 table2 ....] > backupfilename.sql

Backing up only the database structure of specific databases, not the actual data:
mysqldump --no-data --databases db1 db2 db3 > structurebackup.sql

Reset Mysql root password

                                                               
                                                                   
Reset root password in Mysql

Method 1:                                          

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=password('') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


[root@test ~]# mysqladmin -u root -p'global!23' password 'linklinux'

[root@test ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.1.69 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

[root@gai-1347 ~]# mysqladmin -u root -p'linklinux' password ''

Empty password


Method 2:

Stop mysql

[root@test ~]# mysqld_safe --skip-grant-tables &
[1] 8747                                          
130810 11:38:36 mysqld_safe Logging to '/var/log/mysqld.log'.
130810 11:38:36 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

[root@test ~]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1                          
Server version: 5.1.69 Source distribution            

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=password('linklinux') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> \q
Bye


MySQL Administrative commands

                                                                   
                                                             
                                                                   
 MySQL Administration

This is just  basics of maintaining a database including creation of databases, creation of users, granting of priveleges to users, changing passwords, and other activities from within the mysql CLI front end to MySQL.
Entering and Exiting the mysql Program


[sai@test ~]$ mysql
ERROR 1045: Access denied for user: 'sai@localhost' (Using password: NO)


OOPs! User sai is apparently password protected, so we must run mysql to query for a password, and then type in the password.

[sai@test ~]$ mysql -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 41 to server version: 4.0.18

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> quit
Bye



In the preceding, you ran mysql with the -p option so that it would query for the password, you typed in the password, and then you exited the mysql program by typing quit.

By the way, if the account has no password (this is sometimes an installation default), you would just press Enter when prompted for the password.

Perhaps if you want to log in as root instead of sai. This would probably be the case if you wanted to add a database or any other high responsibility action.

Here's how you log in as root:

[sai@test ~]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 42 to server version: 4.0.18

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> quit
Bye


The -u root tells mysql to log you in as root user, and as previously discussed, the -p tells mysql to query for a password.

In mysql, when you see a prompt like this:

mysql>

you are in the mysql program. For the rest of this section, most examples will begin and end in mysql.


As a preventive measure

There may be more than one root account, and you must password protect all of them.

mysql> use mysql
Database changed
mysql> select host, user, password from user;
+-----------------------+-------+------------------+
| host                  | user  | password         |
+-----------------------+-------+------------------+
| localhost             | root  | 2aa0e9e91dbd7cf2 |
| localhost.localdomain | root  | 2aa0e9e91dbd7cf2 |
| localhost             |       |                  |
| localhost.localdomain |       |                  |
| localhost             | sai | 2aa0e9e91dbd7cf2 |
+-----------------------+-------+------------------+
5 rows in set (0.00 sec)

mysql>


As you can see, there's a root account at localhost and another to localhost.localdomain. Both must be password protected. In reality, all  accounts should be password protected.

Exploring Your MySQL Installation

From within mysql you can find quite a bit of information concerning your installation.

As what user are you logged into mysql? What databases exist? What tables exist in a database? What is the structure of a table? What users exist?

Because different operations require different priveleges, to save time we'll perform all these actions logged into mysql  as root.

Let's start with finding out your username within mysql:

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql>


Now let's list all the users authorized to log into mysql:

mysql> use mysql
Database changed
mysql> select host, user, password from user;
+-----------------------+-------+------------------+
| host                  | user  | password         |
+-----------------------+-------+------------------+
| localhost             | root  | 2aa0e9e91dbd7cf2 |
| localhost.localdomain | root  | 2aa0e9e91dbd7cf2 |
| localhost             |       |                  |
| localhost.localdomain |       |                  |
| localhost             | sai | 2aa0e9e91dbd7cf2 |
+-----------------------+-------+------------------+
5 rows in set (0.00 sec)

mysql>


Notice there are two root logons -- one at localhost and one at localhost.localdomain.

They can have different passwords, or one of them can even have no password. Therefore, You'd Better check to make sure they're both password protected.

DO NOT forget the password(s). If you lose all administrative passwords, you lose control of your mysql installation.



User Maintenance

Before discussing user maintenance, it's necessary to understand what properties exist for each user. To do that, we'll use mysql -e option to execute SQL commands and push the output to stdout.

Watch this:

[sai@test ~]$ mysql -u root -p -e "use mysql;describe user;" | cut -f 1
Enter password:
Field
Host
User
password
Select_priv
Insert_priv
Update_priv
Delete_priv
Create_priv
Drop_priv
Reload_priv
Shutdown_priv
Process_priv
File_priv
Grant_priv
References_priv
Index_priv
Alter_priv
Show_db_priv
Super_priv
Create_tmp_table_priv
Lock_tables_priv
Execute_priv
Repl_slave_priv
Repl_client_priv
ssl_type
ssl_cipher
x509_issuer
x509_subject
max_questions
max_updates
max_connections



The word "Field" is a heading, not a piece of information about a user. After that, the first three fields are the user's host, username and password.

 The password is encoded. If you hadn't included the cut command, you'd have seen that the same username can exist in multiple hosts, even if both hosts refer to the same physical machine.

That's why it's vital to MAKE SURE TO password protect ALL users. The next several fields after password are priveleges that can be granted to the user, or not.
There's also a way to grant ALL privileges to a user. But from a security point of view this is very dangerous, as ordinary looking users can be turned into backdoors.

I'd suggest always granting and revoking specific privileges. Here is a list of the privileges that MySQL users can have, and what those privileges allow them to do:

USER FIELD    PRIVILEGE FUNCTION

Select_priv Select Ability to use the select command. In other words, ability to read.

Insert_priv Insert Ability to insert new data -- insert a new row.

Update_priv Update Ability to change existing data -- change contents of a row.

Delete_priv Delete Ability to delete rows of existing data.

Create_priv Create Ability to create a new table.

Drop_priv Drop Ability to drop a table.

Reload_priv Reload

Shutdown_priv Shutdown

Process_priv Process

File_priv File

Grant_priv Grant Ability to grant and revoke privileges to others.

References_priv References

Index_priv Index Ability to create new indexes or drop indexes.

Alter_priv Alter Ability to change the structure of a table.

Show_db_priv


Super_priv


Create_tmp_table_priv Ability to create temporary tables.

Lock_tables_priv Ability to lock tables.

Execute_priv


Repl_slave_priv


Repl_client_priv



The root user, or any user given sufficient privileges, can create new users with the grant command:

mysql> grant select on test2.* to myuid@localhost identified by 'mypassword';
Query OK, 0 rows affected (0.00 sec)

mysql> select host, user, password from mysql.user;
+-----------------------+-------+------------------+
| host                  | user  | password         |
+-----------------------+-------+------------------+
| localhost             | root  | 7939f45f28d0aa41 |
| localhost.localdomain | root  | 2aa0e9e91dbd7cf2 |
| localhost             |       |                  |
| localhost.localdomain |       |                  |
| localhost             | sai   | 2aa0e9e91dbd7cf2 |
| localhost             | myuid | 162eebfb6477e5d3 |
+-----------------------+-------+------------------+
6 rows in set (0.00 sec)

mysql>


In the preceding, we grant one privilege, select, on every table in the test2 database (test2.*), to user myuid at host localhost (myuid@localhost), giving that user the password "mypassword". We then query table user in the mysql database (mysql.user) in  order to see whether user myuid has been created. Indeed he has.

Granting select privilege is insufficient for any user using any app that modies data. Let's give myuid more privileges in the test2 database:

mysql> grant Insert, Update, Delete, Create on test2.* to myuid@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql>


Now user myuid can not only select, but can insert rows, update rows, delete rows, and create tables and databases. Conspicuously missing is the ability to drop tables and databases -- that can be more dangerous, in the hands of a careless but not malicious user, than some of the other abilities.

Privileges granted by the grant command are not kept in the mysql.user table, but instead in the mysql.db table. You can see results by issuing this command from the operating system:

[sai@test ~]$ mysql -u root -p -e 'select * from mysql.db where host="localhost" and user="myuid";' > temp.txt
Enter password:


The results look like this:

Host Db User Select_priv Insert_priv Update_priv Delete_priv Create_priv Drop_priv Grant_priv References_priv Index_priv Alter_priv Create_tmp_table_priv Lock_tables_priv
localhost test2 myuid Y Y Y Y Y N N N N N N N


You can revoke privileges like this:

mysql> revoke Delete, Create on test2.* from myuid@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql>


If you redo the select on mysql.db, you'll see that those two privileges are now marked N.

To actually delete a user, you use a SQL statement to delete him from the mysql.users table after revoking all his privileges.


When deleting the user from mysql.user, if you forget the where clause, or any of its tests, especially the test on column user, you will delete too many users -- possibly all users, in which case you'll have no way to operate the database. BE VERY CAREFUL!

Now that you understand the potential landmines, here's how you delete a user:

mysql> revoke all on test2.* from myuid@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> delete from mysql.user where user='myuid' and host='localhost';
Query OK, 1 row affected (0.00 sec)

mysql>

Exploring Your Databases

You can refer to a table in two ways: By name after connecting to its database with a use statement, and with the databasename.tablename syntax.
The former is much more common in applications, but the latter is often used in database administration, especially when you must access the system database (mysql) in order to perform work on a different database.

The first item for exploration is to find all databases:


mysql> show databases;
+-------------------+
| Database          |
+-------------------+
| depot_development |
| depot_production  |
| depot_test        |
| mysql             |
| test              |
| test2             |
+-------------------+
6 rows in set (0.00 sec)

mysql>


In the preceding, you went into the mysql program, determined what databases existed. Now let's explore the test database:

mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| dogs           |
| people         |
+----------------+
2 rows in set (0.00 sec)

mysql>


So the database test has two tables, dogs and people. Let's examine the columns in each of those tables:

mysql> show columns from dogs;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| owner | varchar(20) |      | PRI |         |       |
| name  | varchar(20) |      | PRI |         |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> show columns from people;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| lname | varchar(20) |      | PRI |         |       |
| fname | varchar(20) |      |     |         |       |
| mname | varchar(16) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql>


Another way to get the same information is with the describe command:

mysql> describe dogs;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| owner | varchar(20) |      | PRI |         |       |
| name  | varchar(20) |      | PRI |         |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> describe people;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| lname | varchar(20) |      | PRI |         |       |
| fname | varchar(20) |      |     |         |       |
| mname | varchar(16) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> \q

Monday, September 23, 2013

Date command with examples

Date command is most essentially used in scripting for date wise backups and in other scenarios.                                                                
                                                                     
                                                                     
                                             
    date +"%Y/%m/%d %H:%M"

is correct and 

    date +%Y/%m/%d %H:%M  

is not.
Please note that if you date conversion template contains spaces you need to put it in single or double quotes

If  you are OK with two year year representation you can use %y instead of %Y. There is also a macro %D that provides date in  mm/dd/yy format, so we can rewrite the previous example as

     date +"%D %H:%M"

Here  are most important build-in macros:

        %% a literal %
        %a locale's abbreviated weekday name (Sun..Sat)
        %A locale's full weekday name, variable length (Sunday..Saturday) %b locale's abbreviated month name (Jan..Dec)
        %B locale's full month name, variable length (January..December)
        %c locale's date and time (Sat Nov 04 12:02:33 EST 1989)
        %d day of month (01..31)
        %D date (mm/dd/yy)
        %e day of month, blank padded ( 1..31)
        %H hour (00..23)
        %j day of year (001..366)
        %m month (01..12)
        %M minute (00..59)
        %n a newline
        %p locale's AM or PM
        %r time, 12-hour (hh:mm:ss [AP]M)
        %S second (00..60)
        %t a horizontal tab
        %T time, 24-hour (hh:mm:ss)
        %U week number of year with Sunday as first day of week (00..53)
        %V week number of year with Monday as first day of week (01..52)
        %w day of week (0..6); 0 represents Sunday
        %W week number of year with Monday as first day of week (00..53)
        %x locale's date representation (mm/dd/yy)
        %X locale's time representation (%H:%M:%S)
        %y last two digits of year (00..99)
        %Y -- ccyy year

Date also can work not with the current time, but with time of modification of the referenced file

    date -r /etc/passwd +"%y%m%d"

It also can serve as data calculator.
Various options for past days (GNU date only):

date +"%Y%m%d" -d sunday # GNU date
20060709

date +"%Y%m%d" -d last-sunday # GNU date
20060702

date +"%Y%m%d" -d last-week # GNU date
date -v -1m +"%Y%m%d" # BSD date
20060627

date +"%Y%m%d" -d last-month # GNU date
date -v -1w +"%Y%m%d" # BSD date
20060604

date +"%Y%m%d" -d last-year # GNU date
date -v -1y +"%Y%m%d" # BSD date
20050704

date +"%Y%m%d" -d next-week # GNU date
date -v 1w +"%Y%m%d" # BSD date
20060711

date +"%Y%m%d" -d next-month # GNU date
date -v 1m +"%Y%m%d" # BSD date
20060804

date +"%Y%m%d" -d next-year # GNU date
date -v 1y +"%Y%m%d" # BSD date
20070704

To show the time in seconds since 1970-01-01 (Unix epoch):

date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"
1240596879

To convert Unix epoch time (seconds since 1970-01-01) to a human readable format:

date -d "UTC 1970-01-01 1240596879 secs"
Fri Apr 24 13:14:39 CDT 2009

Or:

date -ud @1000000000
Sun Sep  9 01:46:40 UTC 2001

    Examples

date "+%-m/%-d/%y" 
7/4/06

date "+%Y%m%d"
20060704

    $ date  +"%b %d, %Y"
    Mar 14, 2010

    date -d yesterday +"%Y/%m/%d"
    2010/03/13

Yesterday assigned to variable

DATE=$(date -d yesterday +"%Y%m%d")
echo $DATE
20060704

To assign the time to a variable

START=`date '+%r'`
echo $START
03:06:02 PM
sleep 5
echo $START
03:06:02 PM

To show the time in a different timezone, the TZ environment variable is read, Timezone types is found in /usr/share/zoneinfo

OLDTZ=$TZ
export TZ=GMT; echo "GMT:               `date +\"%F %R (%Z)\"`"
GMT:               2008-10-31 12:30 (GMT)
export TZ=Europe/Stockholm; echo "Stockholm:    `date +\"%F %R (%Z)\"`"
Stockholm:    2008-10-31 13:30 (CET)
export TZ=Asia/Kuala_Lumpur; echo "Kuala Lumpur: `date +\"%F %R (%Z)\"`"
Kuala Lumpur:        2008-10-31 20:30 (MYT)
export TZ=US/Central; echo "Dallas:             `date +\"%F %R (%Z)\"`"
Dallas:             2008-10-31 07:30 (CDT)
export TZ=$OLDTZ

Using date for offset calculation:

date +"%Y%m%d" -d sunday
20060709

date +"%Y%m%d" -d last-sunday
20060702

date +"%Y%m%d" -d last-week
20060627

date +"%Y%m%d" -d last-month
20060604

date +"%Y%m%d" -d last-year
20050704

date +"%Y%m%d" -d next-week
20060711

date +"%Y%m%d" -d next-month
20060804

date +"%Y%m%d" -d next-year
20070704

Tuesday, September 3, 2013

Cron Format in linux.

Cron Format

Cron is a simple and powerful way to perform various actions in a timely manner. It also plays a main role in automating tasks.

cron format consists of five fields separated by white spaces:

<Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week>


Cron format:

* * * * *     followed by command to be executed.
| | | | |
| | | | |
| | | | +----   Day of the Week   (range: 1-7, 1 standing for Monday)
| | | +------  Month of the Year (range: 1-12)
| | +-------- Day of the Month  (range: 1-31)

| +---------- Hour              (range: 0-23)
+------------ Minute            (range: 0-59)


Any of these 5 fields may be an asterisk (*). This would mean the entire range of possible values, i.e. each minute, each hour, etc.

Any field may contain a list of values separated by commas, (e.g. 1,3,7) or a range of values (two integers separated by a hyphen, e.g. 1-5).

After the asterisk or values, you can use character / to do repeated action with certain intervals.

For example, you can write "0-23/2" in Hour field to specify that some action should be performed every two hours (it will have the same effect as "0,2,4,6,8,10,12,14,16,18,20,22"); value "*/4" in Minute field means that the action should be performed every 4 minutes, "1-30/3" means the same as "1,4,7,10,13,16,19,22,25,28".

In Month and Day of Week fields, you can use names of months or days of weeks abbreviated to first three letters ("Jan,Feb,...,Dec" or "Mon,Tue,...,Sun") instead of their numeric values.

Examples:

* * * * *                              Each minute


59 23 31 12 5                      One minute  before the end of year if the last day of the year is Friday.

(or)

59 23 31 DEC Fri                Same as above


00 12 13 7 *                        Every  year, on July 13th at 12:00


0,15,30,45 0,6,12,18 1,15,31 * 1-5  At 00:00, 00:15, 00:30, 00:45, 06:00, 06:15, 06:30,06:45, 12:00,                                                                12:15, 12:30, 12:45, 18:00, 18:15,18:30, 18:45, on 1st, 15th or  31st                                                          of each  month, but not on weekends


*/15 */6 1,15,31 * 1-5                    Same as above


0 12 * * 1-5 *                             At midday on weekdays


* * * 1,3,5,7,9,11 *                        Each minute in January,  March,  May, July, September, and November


1,2,3,5,20-25,30-35,59 23 31 12 *  On the  last day of year, at 23:01, 23:02, 23:03, 23:05,
                                                         23:20, 23:21, 23:22, 23:23, 23:24, 23:25, 23:30,
                                                          23:31, 23:32, 23:33, 23:34, 23:35, 23:59


0 9 1-7 * 1                                    First Monday of each month, at 9 a.m.



0 0 1 * *                                      At midnight, on the first day of each month


* 0-11 * * *                                Each minute before midday


* * * 1,2,3 *                               Each minute in January, February or March



* * * Jan,Feb,Mar *                      Same as above


0 0 * * *                                    Daily at midnight


0 0 * * 3                                     Each Wednesday at midnight


You can also use special strings like below


@reboot   At startup.

@yearly   Run once a year

0 0 1 1 *   (Same as above)

@annually (same as @yearly)

@monthly   Run once a month

0 0 1 * *     (same as above)

@weekly  Run once a week

0 0 * * 0     (same as above)

@daily         Run once a day

0 0 * * *      (same as above)

@midnight    (same as @daily)

@hourly         Run once an hour

0 * * * *        (same as above)


you can use

crontab -e to edit and add your cronjobs.

crontab -l to list the cronjobs.

Before pressing the enter button , ensure that you haven't entered crontab -r as it would delete all the cronjobs.

To check the users cronjobs, use the below syntax.

crontab -u username -l

Install postgresql in linux

Download postgresql software

wget http://ftp.postgresql.org/pub/source/v9.2.3/postgresql-9.2.3.tar.bz2                            
tar -jxvf postgresql-9.2.3.tar.bz2                                                                  
cd postgresql-9.2.3    
       
./configure --without-readline
 make
 make install

[root@machine1 postgresql-9.2.3]# useradd postgres                                                          
[root@machine1 postgresql-9.2.3]# mkdir /usr/local/
bin/     etc/     games/   include/ lib/     lib64/   libexec/ pgsql/   sbin/    share/   src/  
[root@machine1 postgresql-9.2.3]# mkdir /usr/local/pgsql/data
[root@machine1 postgresql-9.2.3]# chown postgres /usr/local/pgsql/data
[root@machine1 postgresql-9.2.3]# /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
initdb: cannot be run as root                                                      
Please log in (using, e.g., "su") as the (unprivileged) user that will              
own the server process.                                                            
[root@machine1 postgresql-9.2.3]# su - postgres
[postgres@machine1 ~]$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.                                

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".  

fixing permissions on existing directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok                                    
selecting default max_connections ... 100                          
selecting default shared_buffers ... 32MB                          
creating configuration files ... ok                                
creating template1 database in /usr/local/pgsql/data/base/1 ... ok
initializing pg_authid ... ok                                      
initializing dependencies ... ok                                  
creating system views ... ok                                      
loading system objects' descriptions ... ok                        
creating collations ... ok                                        
creating conversions ... ok                                        
creating dictionaries ... ok                                      
setting privileges on built-in objects ... ok                      
creating information schema ... ok                                
loading PL/pgSQL server-side language ... ok                      
vacuuming database template1 ... ok                                
copying template1 to template0 ... ok                              
copying template1 to postgres ... ok                              

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
or
    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

[postgres@machine1 ~]$ /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &
[1] 26097
[postgres@machine1 ~]$ /usr/local/pgsql/bin/createdb test
/usr/local/pgsql/bin/createdb: symbol lookup error: /usr/local/pgsql/bin/createdb: undefined symbol: PQconnectdbParams
[postgres@machine1 ~]$  LD_LIBRARY_PATH=/usr/local/pgsql/lib
[postgres@machine1 ~]$ export  LD_LIBRARY_PATH=/usr/local/pgsql/lib
[postgres@machine1 ~]$ /usr/local/pgsql/bin/createdb test
[postgres@machine1 ~]$

create database

[postgres@machine1 ~]$ /usr/local/pgsql/bin/psql test
psql (9.2.3)
Type "help" for help.

test=#
test=#
test=#

change postgres password

[postgres@machine1 ~]$ /usr/local/pgsql/bin/psql test
psql (9.2.3)
Type "help" for help.

test=# ALTER USER postgres WITH ENCRYPTED PASSWORD 'postgres';
ALTER ROLE
test=# \q

Edit the postgre configuration file and add the below lines

[root@machine1 postgresql-9.2.3]# vi /usr/local/pgsql/data/pg_hba.conf

#host    all             all             127.0.0.1/32            trust
to
host    all             all             127.0.0.1/32            md5


vi /usr/local/pgsql/data/postgresql.conf

# - Connection Settings -

#listen_addresses = 'localhost'         # what IP address(es) to listen on;
to
listen_addresses = '*'                  # what IP address(es) to listen on;

uncomment
password_encryption = on



The first time you run "service postgresql start", it asks you to initialize a database, run:

service postgresql initdb


To start the postgres service while starting the server, follow the below steps

[root@machine1 ~]# cp -rf /usr/local/src/postgresql-9.2.3/contrib/start-scripts/linux /etc/init.d/postgres
[root@machine1 ~]# chmod 755 /etc/init.d/postgres
[root@machine1 ~]# /etc/init.d/postgres start
Starting PostgreSQL: ok
[root@machine1 ~]# /etc/init.d/postgres restart
Restarting PostgreSQL: ok


To have PostgreSQL start automatically when the computer boots add
symbolic links from the correct /etc/rc*.d/ directories to
/etc/init.d/postgres. If the normal runlevel is 3 then you really only
need to add it to rc3.d:

[root@machine1 ~]# ln -s /etc/init.d/postgres /etc/rc3.d/S85postgres
[root@machine1 ~]# ll /etc/rc3.d/S85postgres
lrwxrwxrwx 1 root root 20 Jun 28 16:25 /etc/rc3.d/S85postgres -> /etc/init.d/postgres

We now successfully installed the postgresql, if you would like to change the port number, do the changes in  the below configuration file

/usr/local/pgsql/data/postgresql.conf





Monday, August 5, 2013

Advanced examples with sed command,

Sed - Stream Editor is a powerful command to modify the files in linux. Sed command is one of most useful commands used in shell scripting. Some of the sed examples



[root@machine2 ~]# cat test.txt
Linux  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.                                                                        

Search and replace certain patterns in a file

Replaces first occurance of the pattern in file                                                                        

[root@machine2 ~]# sed 's/Linux/unix/1' test.txt                                                            
unix  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.                                                                        

Replaces second occurance of pattern in the file

[root@machine2 ~]# sed 's/Linux/unix/2' test.txt                                                            
Linux  is a Unix-like computer operating system. unix is assembled under the model of free and open source software development and distribution.                                                                        

Even this syntax replaces the first occurance.

[root@machine2 ~]# sed 's/Linux/unix/' test.txt                                                            
unix  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.
                                           
Replaces all the matching pattern in the file.
                           
[root@machine2 ~]# sed 's/Linux/unix/g' test.txt                                                            
unix  is a Unix-like computer operating system. unix is assembled under the model of free and open source software development and distribution.

[root@machine2 ~]# cat test.txt
Linux  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                

Replaces the matching pattern from the second occurances to the last.

[root@machine2 ~]# sed 's/Linux/unix/2g' test.txt                                                          
Linux  is a Unix-like computer operating system. unix is assembled under the model of free and open source software development and distribution.Love to work with unix.                                                  

[root@machine2 ~]# cat > test1.txt                                                                          
http://google.com                                                                                          
http://yahoo.com                                                                                            

[root@machine2 ~]# cat test1.txt                                                                            
http://google.com                                                                                          
http://yahoo.com                                                                                            

Replacing a url. Here \ is the delimiter

[root@machine2 ~]# sed 's/http:\/\//www/' test1.txt                                                        
wwwgoogle.com                                                                                              
wwwyahoo.com                          
                                                                   

[root@machine2 ~]# sed 's/http:\/\//www./' test1.txt                                                        
www.google.com                                                                                              
www.yahoo.com                                                                                              

We can also use _ as a delimiter

[root@machine2 ~]# sed 's_http://_www._' test1.txt                                                        
www.google.com                                                                                            
www.yahoo.com                                                                                            

Here | is the delimiter.

[root@machine2 ~]# sed 's|http:|_www.|' test1.txt                                                        
_www.//google.com                                                                                        
_www.//yahoo.com                                                                                        

[root@machine2 ~]# sed 's|http:|www.|' test1.txt                                                        
www.//google.com                                                                                        
www.//yahoo.com                                                                                        

[root@machine2 ~]# sed 's|http://|www.|' test1.txt                                                        
www.google.com                                                                                            
www.yahoo.com                                                                                            

[root@machine2 ~]# cat test.txt                                                                          
Linux  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                

Searches for a specific pattern and add some extra characters, symbols using &

[root@machine2 ~]# sed 's/Linux/{&}/' test.txt                                                              
{Linux}  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                              

[root@machine2 ~]# sed 's/Linux/,&/' test.txt
,Linux  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                              

[root@machine2 ~]# sed 's/Linux/&,/' test.txt                                                              
Linux,  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                              

To include the same patter once again use &&

only in First occurance.

[root@machine2 ~]# sed 's/Linux/{&&}/' test.txt
{LinuxLinux}  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.

Includes the extra characters to all the matching pattern.

[root@machine2 ~]# sed 's/Linux/{&}/g' test.txt
{Linux}  is a Unix-like computer operating system. {Linux} is assembled under the model of free and open source software development and distribution.Love to work with {Linux}.

Search and replaces the matching pattern and prints line double.

[root@machine2 ~]# sed 's/Linux/unix/p' test.txt
unix  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.
unix  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.

Search, replace and prints the line.

[root@machine2 ~]# sed -n 's/Linux/unix/p' test.txt
unix  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.

Executing multiple sed commands in single line using | symbol and -e option

[root@machine2 ~]# sed  's/Linux/LINUXOS/' test.txt | sed 's/Unix/UnixOS/'
LINUXOS  is a UnixOS-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.

[root@machine2 ~]# sed -e 's/Linux/LINUXOperatingSystem/' -e 's/Unix/UnixOperatingSystem/' test.txt
LINUXOperatingSystem  is a UnixOperatingSystem-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.


[root@machine2 ~]# cat test.txt                                                                            
Linux  is a Unix-like computer operating system.                                                            
Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                
Learning Linux is very easy.

Replaces word Linux with unix in the second line of the file
                                                                             
[root@machine2 ~]# sed '2 s/Linux/unix/' test.txt
Linux  is a Unix-like computer operating system.
unix is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                  
Learning Linux is very easy.                                                                                

Replaces word Linux with unix in the first and second line of the file. We can also specify the line range.

[root@machine2 ~]# sed '1,2 s/Linux/unix/' test.txt                                                        
unix  is a Unix-like computer operating system.                                                            
unix is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                  
Learning Linux is very easy.                                                                                

Replaces word Linux with unix from first to last line of the file

[root@machine2 ~]# sed '1,$ s/Linux/unix/' test.txt
unix  is a Unix-like computer operating system.  
unix is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                  
Learning unix is very easy.                                                                                

Search for a specific pattern and replaces it.
                                             
[root@machine2 ~]# sed '/Linux/ s/Linux/centos/' test.txt                                                  
centos  is a Unix-like computer operating system.                                                          
centos is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                
Learning centos is very easy.                                                                              

Here, Search for the occurances a matching pattern and replaces it with the given word.

[root@machine2 ~]# sed '/Linux/ s/Unix/Fedora/' test.txt                                                  
Linux  is a Fedora-like computer operating system.                                                        
Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                
Learning Linux is very easy.    

In above searches for Unix and replaces with Fedora.


Prints the line twice in a file.

[root@machine2 ~]# sed 'p' test.txt                        
Linux  is a Unix-like computer operating system.            
Linux  is a Unix-like computer operating system.            
Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                
Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                
Learning Linux is very easy.                                                                                
Learning Linux is very easy.  

Like grep command fetches and prints the line where the matching string found.
                                                                           
[root@machine2 ~]# sed -n '/free/ p' test.txt
Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                

Prints the line other than the matching string line.

[root@machine2 ~]# sed -n '/free/ !p' test.txt                                                              
Linux  is a Unix-like computer operating system.                                                            
Learning Linux is very easy.                                                                                

Add a new line after the matching pattern.

[root@machine2 ~]# sed '/Learning/ a "Adding a new line" ' test.txt                                        
Linux  is a Unix-like computer operating system.                                                            
Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                
Learning Linux is very easy.                                                                                
"Adding a new line"

Add a new line before the matching pattern.

[root@machine2 ~]# sed '/Learning/ i "Adding a new line" ' test.txt
Linux  is a Unix-like computer operating system.
Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.
"Adding a new line"
Learning Linux is very easy.

Change the line where the matching string found.

[root@machine2 ~]# sed '/Learning/ c "chaning with a new line" ' test.txt
Linux  is a Unix-like computer operating system.
Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.
"chaning with a new line"

[root@machine2 ~]# sed '/Learning/ c "changing with a new line" ' test.txt
Linux  is a Unix-like computer operating system.
Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.
"changing with a new line"

Transfrom with the specific letters like tr command.

[root@machine2 ~]# sed 'y/L/l' test.txt
sed: -e expression #1, char 5: unterminated `y' command

[root@machine2 ~]# sed 'y/L/l/' test.txt
linux  is a Unix-like computer operating system.
linux is assembled under the model of free and open source software development and distribution.love to work with linux.
learning linux is very easy