How to Find All Files that Containing Specific Text in Linux

This post will guide you how to find a specic string from a given files or directories at the shell prompt in Linux. How do I find all files containing particular text string in your Linux operating systems.

find all files that contain text string1

If you want to find all files in a given directory for a specific test string in the shell prompt, and you can use grep command to search files.

For example, if you want to search for a text string called “selinux” in all configuration files located in /etc/ directory, type the following command:

$ sudo grep "selinux" /etc/*.conf

outputs:

[root@mydevops opc]# sudo grep selinux /etc/*.conf
/etc/sos.conf:#disable = rpm, selinux, dovecot
/etc/updatedb.conf:PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs fuse.glusterfs ceph fuse.ceph"

If you want to search for a test string from all files under a given directory and its subdirectories, and you need to pass the -r or -R option to the grep command, type:

$ sudo grep -r "selinux" /etc/

Or

$ sudo grep -R "selinux" /etc/

Outputs:

[root@mydevops opc]# sudo grep -r "selinux" /etc/
/etc/libreport/events.d/abrt_event.conf: --only=auditd --only=selinux --only=lvm2 --only=sar \
/etc/prelink.conf.d/grub2.conf:# these have execstack, and break under selinux
/etc/selinux/targeted/modules/active/README.migrated:/etc/selinux/targeted/active
/etc/selinux/targeted/modules/active/README.migrated:WARNING: Do not remove this file or remove /etc/selinux/targeted/modules
/etc/selinux/targeted/booleans.subs_dist:allow_execheap selinuxuser_execheap
/etc/selinux/targeted/booleans.subs_dist:allow_execmod selinuxuser_execmod
/etc/selinux/targeted/booleans.subs_dist:allow_execstack selinuxuser_execstack
/etc/selinux/targeted/booleans.subs_dist:allow_user_mysql_connect selinuxuser_mysql_connect_enabled
/etc/selinux/targeted/booleans.subs_dist:allow_user_postgresql_connect selinuxuser_postgresql_connect_enabled
/etc/selinux/targeted/booleans.subs_dist:user_direct_dri selinuxuser_direct_dri_enabled
/etc/selinux/targeted/booleans.subs_dist:user_ping selinuxuser_ping
/etc/selinux/targeted/booleans.subs_dist:user_share_music selinuxuser_share_music
/etc/selinux/targeted/booleans.subs_dist:user_tcp_server selinuxuser_tcp_server
/etc/selinux/targeted/booleans.subs_dist:user_rw_noexattrfile selinuxuser_rw_noexattrfile
/etc/selinux/targeted/booleans.subs_dist:sepgsql_enable_users_ddl postgresql_selinux_users_ddl
/etc/selinux/targeted/booleans.subs_dist:sepgsql_transmit_client_label postgresql_selinux_transmit_client_label
/etc/selinux/targeted/booleans.subs_dist:sepgsql_unconfined_dbadm postgresql_selinux_unconfined_dbadm
/etc/selinux/targeted/contexts/dbus_contexts: 
/etc/selinux/targeted/contexts/dbus_contexts: 
......

If you only want to display the filenames when you search for a test string from a given directory with grep command. and you need to pass the -H option to grep command to print filenames. and you still need to use cut command to print the first field of the outputs of grep command. type:

$ sudo grep -r -H "selinux" /etc/ | cut -d: -f1

outputs:

[root@mydevops opc]# sudo grep -r -H "selinux" /etc/ | cut -d: -f1
/etc/libreport/events.d/abrt_event.conf
/etc/prelink.conf.d/grub2.conf
/etc/selinux/targeted/modules/active/README.migrated
/etc/selinux/targeted/modules/active/README.migrated
/etc/selinux/targeted/booleans.subs_dist
/etc/selinux/targeted/booleans.subs_dist
/etc/selinux/targeted/booleans.subs_dist
/etc/selinux/targeted/booleans.subs_dist
/etc/selinux/targeted/booleans.subs_dist
/etc/selinux/targeted/booleans.subs_dist
/etc/selinux/targeted/booleans.subs_dist
/etc/selinux/targeted/booleans.subs_dist
/etc/selinux/targeted/booleans.subs_dist
/etc/selinux/targeted/booleans.subs_dist
/etc/selinux/targeted/booleans.subs_dist
/etc/selinux/targeted/booleans.subs_dist
/etc/selinux/targeted/booleans.subs_dist
/etc/selinux/targeted/contexts/dbus_contexts
/etc/selinux/targeted/contexts/dbus_contexts
/etc/selinux/targeted/contexts/files/file_contexts
......

If you want to search for two or more text strings from a given directory, and you can use the egrep command, type:

$ sudo egrep 'test1|test2' /directory

If you want to Ignore case distinctions in both the PATTERN and the input files when searching for a text string, and you need to pass the -i option to the grep command, type:

$ sudo grep -i "test" /directory

 

 

You might also like:

Sidebar



back to top