Friday, March 15, 2013

List file name containing perticular text in Linux


In Linux Shell to find a particular text string in all the files from the current directory recursively, use find command
 find . -exec grep -l "World" {} \;
This command searches through all directories from the current directory recursively for the files that contain the string “World”.

Friday, March 8, 2013

How to get Linux version information

There are various ways we can get Linux version information  and commands below are commonly used in various Linux distribution.

1. by simply viewing file /proc/version using cat.


[root@localhost /]# cat /proc/version
Linux version 2.6.18-164.el5 (mockbuild@x86-002.build.bos.redhat.com) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)) #1 SMP Tue Aug 18 15:51:54 EDT 2009


2. by using "uname" command.

[root@localhost /]# uname -a
Linux localhost.localdomain 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux

3. by using "lsb_release" commands.

[root@localhost /]# lsb_release -a
LSB Version:    :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: RedHatEnterpriseServer
Description:    Red Hat Enterprise Linux Server release 5.4 (Tikanga)
Release:        5.4
Codename:       Tikanga

4. by using "dmesg" command (display kernal message buffer) .

[root@localhost /]# dmesg |grep "Linux version"
Linux version 2.6.18-164.el5 (mockbuild@x86-002.build.bos.redhat.com) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)) #1 SMP Tue Aug 18 15:51:54 EDT 2009

this command is very useful to get various configuration information from Linux system.

Removing blank lines from file using sed

We can remove blank lines from a file in shell scripting through various command awk,sed etc. using sed command it is very simple..

sed '/^$/d' myFile > outputfile

'^$' character here represent blank line and 'd' stand for deleting.
Note that sed command do not directly make changes to original file, it just display the output on screen, output can be redirected to new file as shown using '> outputfile'.

same can be achived using grep command as below.

grep -v '^$' myFile > outputfile