Very oftentimes novel users would dwell on Google trying to discovery the right ascendancy to discovery files containing specific text. This is specially of import when you’re tying to follow a badly written guide of forum postal service that says something similar supervene upon 0 alongside 1 inward this business which volition prepare PulseAudio configured for per-user sessions … (warning)
PULSEAUDIO_SYSTEM_START=0
Now for an experienced user, no problem, you lot know precisely where to discovery a configuration file for PulseAudio. For a novel Linux user, yes state me almost it. I’ve been at that spot when I started alongside Slackware dorsum slow nineties.
This guide shows a bunch of commands that you lot tin laissez passer notice piece of employment to discovery files containing specific text inward Linux, namely Ubuntu, Debian, Mint, CentOS, Fedora together with whatsoever Linux distro.
This guide volition piece of employment for whatsoever Linux distributions, namely –
- Linux Mint
- Ubuntu
- Debian GNU/Linux
- Mageia / Mandriva
- Fedora
- openSUSE / SUSE Linux Enterprise
- Arch Linux
- CentOS / Red Hat Enterprise Linux
- PCLinuxOS
- Slackware Linux
- Puppy Linux
- Kali Linux (my distro
)
Find files containing specific text using grep command
grep is a command-line utility for searching plain-text information sets for lines matching a regular expression. Grep was originally developed for the Unix operating system, but is available today for all Unix-like systems. Its advert comes from the ed ascendancy g/re/p (globally search a regular aspect together with print), which has the same effect: doing a global search alongside the regular aspect together with printing all matching lines.
To discovery files containing specific text, you lot are perchance improve off using the grep command. The grep ascendancy tin laissez passer notice discovery together with search a specific text from all files quickly.
grep ascendancy syntax
Syntax for grep ascendancy is simple:
grep "text string to search” directory-path
OR
grep [option] "text string to search” directory-path
OR
grep -r "text string to search” directory-path
OR
grep -r -H "text string to search” directory-path
OR
egrep -R "word-1|word-2” directory-path
OR
egrep -w -R "word-1|word-2” directory-path
Find files containing specific text using grep ascendancy examples
In this example, nosotros volition search for 'PULSEAUDIO_SYSTEM_START‘ inward all configuration files located inward /etc directory.
Now there’s a minor problem, depending on your Linux, BSD or Unix distro, Find ascendancy tin laissez passer notice survive slightly dissimilar (in price of Syntaxes). So I volition outline all possible combinations, you lot tin laissez passer notice merely endeavour 1 at a fourth dimension to produce upwards one's heed which 1 best suites you.
Find files containing specific text when you lot know the location
If you lot know the exact place together with directory you’re after, together with thence use
root@kali: # grep "PULSEAUDIO_SYSTEM_START" /etc/default/pulseaudio
PULSEAUDIO_SYSTEM_START=1
root@kali: #
If you lot know the exact directory alongside the files containing that specific text, together with thence use
root@kali: # grep "PULSEAUDIO_SYSTEM_START" /etc/default/*
grep: /etc/default/kdm.d: Is a directory
/etc/default/pulseaudio:PULSEAUDIO_SYSTEM_START=1
root@kali: #
Find files containing specific text when you lot don’t know the location
If you lot don’t know the exact place of the file that contains the specific text you’re looking for, together with thence you lot request to search all subdirectories recursively.
You tin laissez passer notice search for a text string all files nether each directory, recursively with -r option:
root@kali: # grep -r "PULSEAUDIO_SYSTEM_START" /etc/default/*
/etc/default/pulseaudio:PULSEAUDIO_SYSTEM_START=1
root@kali: #
OR
root@kali: # grep -R "PULSEAUDIO_SYSTEM_START" /etc/default/*
/etc/default/pulseaudio:PULSEAUDIO_SYSTEM_START=1
root@kali: #
Find files containing specific text alongside color output
Now what if you lot are searching through a massive file together with at that spot powerfulness survive many outputs similar to what you’re looking for.. you lot powerfulness desire to use --col flag to colorcode your output which searching files containing specific strings.
root@kali: # grep --col 'usb 1-1.4' /var/log/messages
Aug xviii 09:14:25 kali kernel: [1191164.780496] usb 1-1.4: novel low-speed USB device position out 21 using ehci-pci
root@kali: #
Find files containing specific text alongside filenames only
Now I desire to display all files alongside colorer output alongside containing specific text together with instead of showing the whole content of the files, I merely desire to display the filenames. So I piece of employment something similar this:
root@kali: # grep --col -r 'Linux version 3.14-kali1' /var/log/* | cutting -d: -f1
/var/log/dmesg
/var/log/dmesg.0
/var/log/installer/syslog
root@kali: #
Find files containing specific text together with cover errors
When you’re using grep, depending on the commands used together with permission you lot induce got on the system, you lot powerfulness encounter whatsoever of the next errors.
- Input/output error
- recursive directory loop
- No such file or directory
- No such device or address
- Permission denied
If you lot desire to cover all errors or alert message spamming your output window(specifically useful when you’re trying to piece of employment grep on a script) generated past times the grep command, append 2>/dev/null to grep command. This volition shipping together with cover unwanted output to /dev/null device:
root@kali: # grep -R "PULSEAUDIO_SYSTEM_START" /etc/* 2>/dev/null
/etc/default/pulseaudio:PULSEAUDIO_SYSTEM_START=1
/etc/init.d/pulseaudio:PULSEAUDIO_SYSTEM_START=0
/etc/init.d/pulseaudio:if [ "$PULSEAUDIO_SYSTEM_START" != "1" ]; then
/etc/rc0.d/K01pulseaudio:PULSEAUDIO_SYSTEM_START=0
/etc/rc0.d/K01pulseaudio:if [ "$PULSEAUDIO_SYSTEM_START" != "1" ]; then
/etc/rc1.d/K01pulseaudio:PULSEAUDIO_SYSTEM_START=0
/etc/rc1.d/K01pulseaudio:if [ "$PULSEAUDIO_SYSTEM_START" != "1" ]; then
/etc/rc2.d/S20pulseaudio:PULSEAUDIO_SYSTEM_START=0
/etc/rc2.d/S20pulseaudio:if [ "$PULSEAUDIO_SYSTEM_START" != "1" ]; then
/etc/rc3.d/S20pulseaudio:PULSEAUDIO_SYSTEM_START=0
/etc/rc3.d/S20pulseaudio:if [ "$PULSEAUDIO_SYSTEM_START" != "1" ]; then
/etc/rc4.d/S20pulseaudio:PULSEAUDIO_SYSTEM_START=0
/etc/rc4.d/S20pulseaudio:if [ "$PULSEAUDIO_SYSTEM_START" != "1" ]; then
/etc/rc5.d/S20pulseaudio:PULSEAUDIO_SYSTEM_START=0
/etc/rc5.d/S20pulseaudio:if [ "$PULSEAUDIO_SYSTEM_START" != "1" ]; then
/etc/rc6.d/K01pulseaudio:PULSEAUDIO_SYSTEM_START=0
/etc/rc6.d/K01pulseaudio:if [ "$PULSEAUDIO_SYSTEM_START" != "1" ]; then
root@kali: #
Find files containing specific text together with ignore case
What if you’re non certain almost the instance of the text you’re after? You tin laissez passer notice piece of employment -i to ignore instance (note that this takes significantly longer to discovery files containing specific text).
Below instance shows the divergence betwixt -i flag. First ascendancy didn’t discovery the text, bit ascendancy did equally nosotros used -i flag to ignore case.
root@kali: # grep -r "pulseaudio_system_start" /etc/default/*
root@kali: #
root@kali: # grep -i -r "pulseaudio_system_start" /etc/default/*
/etc/default/pulseaudio:PULSEAUDIO_SYSTEM_START=1
root@kali: #
Summary
In summary, I ever prefer using grep ascendancy with -r and --col flag inward Debian Linux equally -r complains less almost permissions, files, directory etc. together with of course of report around color helps on the eyes when you’re browsing through many lines.
root@kali: # grep --col -r "PULSEAUDIO_SYSTEM_START" /etc/*
/etc/default/pulseaudio:PULSEAUDIO_SYSTEM_START=1
/etc/init.d/pulseaudio:PULSEAUDIO_SYSTEM_START=0
/etc/init.d/pulseaudio:if [ "$PULSEAUDIO_SYSTEM_START" != "1" ]; then
root@kali: #
Other Linux distroes similar Ubuntu, Mint, CentOS, Fedora, Redhat, Arch is no dissimilar to grep command. The commands shown higher upwards volition assistance everyone together with anyone trying to discovery files containing specific text inward Linux.
Thanks for reading. If this helped you, delight share.






