If you want to search a particular string in file and
want to display the file name. You can refer following Linux commands.
1. Using grep command recursively search
a string.
“grep”
command is very useful command to search
the string in files under directory.
# grep –r “string” /path/of/directory
If you want to ignore case sensitiveness use “-i”
option with grep command.
# grep –ir “string”
/path/of/directory
Example:
[root@paragtesting etc]#
grep -ir "nameserver" /etc
/etc/jwhois.conf: # Catch ARIN nameserver records
/etc/rc1.d/K87named: if [ -z "$named_c_option" ]
&& [ -r ${ROOTDIR}/etc/named.caching-nameserver.conf ]; then
/etc/rc1.d/K87named: named_conf='/etc/named.caching-nameserver.conf';
/etc/cron.daily/0logwatch: print " (saves a
nameserver address-to-name lookup).\n";
/etc/rc4.d/K87named: if [ -z "$named_c_option" ]
&& [ -r ${ROOTDIR}/etc/named.caching-nameserver.conf ]; then
/etc/rc4.d/K87named:
named_conf='/etc/named.caching-nameserver.conf';
/etc/rc0.d/K87named: if [ -z "$named_c_option" ]
&& [ -r ${ROOTDIR}/etc/named.caching-nameserver.conf ]; then
/etc/rc0.d/K87named: named_conf='/etc/named.caching-nameserver.conf';
/etc/init.d/named: if [ -z "$named_c_option" ]
&& [ -r ${ROOTDIR}/etc/named.caching-nameserver.conf ]; then
/etc/init.d/named:
named_conf='/etc/named.caching-nameserver.conf';
/etc/services:nameserver 42/tcp name # IEN 116
/etc/services:nameserver 42/udp name # IEN 116
The
above command will display the filename and lines in files which contain
“string”. If you want to display only filename which contain those string use
“-l” option with grep command.
#grep –lir “string”
/path/of/directory
Example:
[root@paragtesting
etc]# grep -lr "nameserver" /etc
/etc/jwhois.conf
/etc/rc1.d/K87named
/etc/selinux/targeted/modules/active/file_contexts
/etc/selinux/targeted/modules/active/base.linked
/etc/selinux/targeted/modules/active/base.pp
/etc/selinux/targeted/modules/active/file_contexts.template
/etc/selinux/targeted/contexts/files/file_contexts
/etc/cron.daily/0logwatch
/etc/rc4.d/K87named
/etc/rc0.d/K87named
/etc/init.d/named
/etc/services
2. Find Command: Recursively search all files for a string.
“find” command is
recommend because of speed and ability to deal with filenames that contain
spaces. You can use following command with using find to search all files for a
particular string in Linux system.
i)
find
/path/of/dir -type f -exec grep -l
"string" {} \;
Example:
[root@arogyadbt etc]#
find /etc -type f -exec grep -l "prefer-life-time" {} \;
/etc/dhcp6s.conf
ii)
find /path/of/dir
-type f | xargs grep -l
"string"
Example:
[root@arogyadbt etc]#
find /etc -type f | xargs grep -l
"prefer-life-time"
/etc/dhcp6s.conf
iii)
find /path/of/dir -type f -print0 | xargs -0 grep –I
“string”
Example:
[root@arogyadbt etc]#
find /etc -type f -print0 | xargs -0 grep -i "prefer-life-time"
/etc/dhcp6s.conf:# prefer-life-time 130;
/etc/dhcp6s.conf:# prefer-life-time 90;
/etc/dhcp6s.conf:# prefer-life-time 100;
/etc/dhcp6s.conf:# prefer-life-time 60;
waah kya baat hai cheete!!!!
ReplyDeletechaa gayi tusi.... NASA ka document nikaal ke lagata hai!!!US return sir G....:P
ReplyDeletemera blog bhi check karlo :p
ReplyDeletewww.gadgetearth.com
Kaa baat kaaaa baat kaaaaaa baat.........confidential data ;)
ReplyDeleteIt is not confidential. It is Linux command open source.
Delete