[How To] find files containing specific text on Linux
How to find files containing specific text on Linux
We can find files containing specific text on Linux in the following ways.
The content of the article
grep command
# grep -rnw '/path/to/look/inside' -e 'text'
- i – ignore case (optional in your case).
- R – recursive.
- l – “show the file name, not the result itself”.
- / – starting at the root of your machine.
Example:
# grep -rnw '/var/log/' -e ' 2.805245' /var/log/kern.log:1449:Jun 22 13:17:45 gharutyunyan-virtual-machine kernel: [ 2.805245] scsi host20: ahci /var/log/syslog.1:2696:Jun 22 13:17:45 gharutyunyan-virtual-machine kernel: [ 2.805245] scsi host20: ahci /var/log/dmesg:1428:[ 2.805245] kernel: scsi host20: ahci
ask command
ack 'text'
Example:
# ack ' 2.805245' kern.log 1449:Jun 22 13:17:45 gharutyunyan-virtual-machine kernel: [ 2.805245] scsi host20: ahci syslog.1 2696:Jun 22 13:17:45 gharutyunyan-virtual-machine kernel: [ 2.805245] scsi host20: ahci dmesg 1428:[ 2.805245] kernel: scsi host20: ahci
[adinserter block=”6″]
find command
find /path/to/look/inside -type f -exec grep -l "text" {} \;
Example:
# find /var/log/ -type f -exec grep -l " 2.805245" {} \;
/var/log/kern.log
/var/log/syslog.1
/var/log/dmesg
RipGrep command
RipGrep command works very fast
rg 'text' /path/to/look/inside -l
Example:
# rg " 2.805245" /var/log/ -l /var/log/kern.log /var/log/syslog.1 /var/log/dmesg
ag command
The Silver Searcher command
ag 'text' /path/to/look/inside -l
Example:
# ag ' 2.805245' /var/log/ -l /var/log/kern.log /var/log/syslog.1 /var/log/dmesg
Video
Test Environment
Ubuntu 20.04 LTS (Focal Fossa)
VMware Workstation 15.1.0