Find by context linux

How to find files containing specific text in Linux? Ubuntu, Debian, Mint, CentOS, Fedora and any Linux distro

Very often new users would dwell on Google trying to find the correct command to find files containing specific text. This is particularly important when you’re tying to follow a badly written guide of forum post that says something like replace 0 with 1 in this line which will fix PulseAudio configured for per-user sessions … (warning)

Now for an experienced user, no problem, you know exactly where to find a configuration file for PulseAudio. For a new Linux user, yeah tell me about it. I’ve been there when I started with Slackware back late nineties.

This guide shows a bunch of commands that you can use to find files containing specific text in Linux, namely Ubuntu, Debian, Mint, CentOS, Fedora and any Linux distro.

This guide will work for any Linux distributions, namely –

  1. Linux Mint
  2. Ubuntu
  3. Debian GNU/Linux
  4. Mageia / Mandriva
  5. Fedora
  6. openSUSE / SUSE Linux Enterprise
  7. Arch Linux
  8. CentOS / Red Hat Enterprise Linux
  9. PCLinuxOS
  10. Slackware Linux
  11. Puppy Linux
  12. Kali Linux (my distro )

Find files containing specific text using grep command

grep is a command-line utility for searching plain-text data 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 name comes from the ed command g/re/p (globally search a regular expression and print), which has the same effect: doing a global search with the regular expression and printing all matching lines.

To find files containing specific text, you are possibly better off using the grep command. The grep command can find and search a specific text from all files quickly.

grep command syntax

Syntax for grep command is simple:

grep "text string to search” directory-path
grep [option] "text string to search” directory-path
grep -r "text string to search” directory-path
grep -r -H "text string to search” directory-path
egrep -R "word-1|word-2” directory-path
egrep -w -R "word-1|word-2” directory-path

Find files containing specific text using grep command examples

In this example, we will search for ‘PULSEAUDIO_SYSTEM_START ‘ in all configuration files located in /etc directory.

Читайте также:  Linux mint значки приложения

Now there’s a small problem, depending on your Linux, BSD or Unix distro, Find command can be slightly different (in terms of Syntaxes). So I will outline all possible combinations, you can just try one at a time to determine which one best suites you.

Find files containing specific text when you know the location

If you know the exact location and directory you’re after, then use

root@kali:~# grep "PULSEAUDIO_SYSTEM_START" /etc/default/pulseaudio PULSEAUDIO_SYSTEM_START=1 root@kali:~#

How to find files containing specific text in Linux? When you know filename and location - 0 - blackMORE Ops

If you know the exact directory with the files containing that specific text, then 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 don’t know the location

If you don’t know the exact location of the file that contains the specific text you’re looking for, then you need to search all subdirectories recursively.

You can search for a text string all files under each directory, recursively with -r option:

root@kali:~# grep -r "PULSEAUDIO_SYSTEM_START" /etc/default/* /etc/default/pulseaudio:PULSEAUDIO_SYSTEM_START=1 root@kali:~#
root@kali:~# grep -R "PULSEAUDIO_SYSTEM_START" /etc/default/* /etc/default/pulseaudio:PULSEAUDIO_SYSTEM_START=1 root@kali:~#

How to find files containing specific text in Linux? Recusive search for a string - 5 - blackMORE Ops

Find files containing specific text with color output

Now what if you are searching through a massive file and there might be many outputs similar to what you’re looking for.. you might want 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 18 09:14:25 kali kernel: [1191164.780496] usb 1-1.4: new low-speed USB device number 21 using ehci-pci root@kali:~#

How to find files containing specific text in Linux? Color code output - 1 - blackMORE Ops

Find files containing specific text with filenames only

Now I want to display all files with colorer output with containing specific text and instead of showing the whole content of the files, I just want to display the filenames. So I use something like this:

root@kali:~# grep --col -r 'Linux version 3.14-kali1' /var/log/* | cut -d: -f1 /var/log/dmesg /var/log/dmesg.0 /var/log/installer/syslog root@kali:~#

How to find files containing specific text in Linux? Find specific text and color code output with filenames only - 2 - blackMORE Ops

Find files containing specific text and hide errors

When you’re using grep, depending on the commands used and permission you have on the system, you might see any of the following errors.

  1. Input/output error
  2. recursive directory loop
  3. No such file or directory
  4. No such device or address
  5. Permission denied

If you want to hide all errors or warning message spamming your output window(specifically useful when you’re trying to use grep on a script) generated by the grep command, append 2>/dev/null to grep command. This will send and hide 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 and ignore case

What if you’re not sure about the case of the text you’re after? You can use -i to ignore case (note that this takes significantly longer to find files containing specific text).

Читайте также:  Проверка прав файла линукс

Below example shows the difference between -i flag. First command didn’t find the text, second command did as we 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:~#

How to find files containing specific text in Linux? Ignore case - 3 - blackMORE Ops

Summary

In summary, I always prefer using grep command with -r and —col flag in Debian Linux as -r complains less about permissions, files, directory etc. and of course some 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:~#

How to find files containing specific text in Linux? Summary - 6 - blackMORE Ops

Other Linux distroes like Ubuntu, Mint, CentOS, Fedora, Redhat, Arch is no different to grep command. The commands shown above will help everyone and anyone trying to find files containing specific text in Linux.

Thanks for reading. If this helped you, please share.

Источник

How to find a text file which contains a specific word inside (not in its name)

I want to find a text file in my hard disk which contains a specific word. Prior to Ubuntu 12.4 I used to start in the dash an application, I think it was called «Search for file. «, whose icon was a magnifying glass.I can’t find that simple application any more.

7 Answers 7

You can use the grep command from terminal:

This command will find all occurrences of «word» in all the files under the current directory (or subdrectories).

if grep is called from a shell script, then the search keyword may not print highlighted, but the —color=auto flag can solve that.

sudo apt-get install gnome-search-tool 

enter image description here

Open Search for files select Select More Options and

As suggested here, use mate-search-tool instead which seems to offer identical functionality. Install via sudo apt install mate-utils .

Here’s an overview of different methods that one can use for searching files for specific strings of text, with a few options added specifically to work only with text files, and ignore binary/application files.

Читайте также:  Linux in ssd raids

One should note,however,that searching for word can get a little complex, because most line-matching tools will try to find a word anywhere on the line. If we’re talking about a word as string that could appear in the beginning or end of line, or alone on the line, or surrounded by spaces and/or punctuation — that’s when we’ll need regular expressions, and especially those that come from Perl. Here, for example, we can use -P in grep to make use of Perl regular expressions to surround it.

$ printf "A-well-a don't you know about the bird?\nWell, everybody knows that the bird is a word" | grep -noP '\bbird\b' 1:bird 2:bird 

Simple grep

  • -r for recursive search down from current directory
  • -I to ignore binary files
  • -H to output filename where match is found

Suitable for searching only.

find + grep

$ find -type f -exec grep -IH 'word' <> \; 
  • find does the recursive search part
  • -I option is to ignore binary files
  • -H to output filename where line is found
  • good approach for combining with other commands within subshell, like:
$ find -type f -exec sh -c 'grep -IHq "word" "$1" && echo "Found in $1"' sh <> \; 

Perl

#!/usr/bin/env perl use File::Find; use strict; use warnings; sub find_word< return unless -f; if (open(my $fh, $File::Find::name))< while(my $line = ) < if ($line =~ /\bword\b/)< printf "%s\n", $File::Find::name; close($fh); return; >> > > # this assumes we're going down from current working directory find( < wanted =>\&find_word, no_chdir => 1 >,".") 

poor-mans recursive grep in recursive bash script

This is the «bash way». Not ideal, probably no good reason to use this when you have grep or perl installed.

#!/usr/bin/env bash shopt -s globstar #set -x grep_line() < # note that this is simple pattern matching # If we wanted to search for whole words, we could use # word|word\ |\ word|\ word\ ) # although when we consider punctuation characters as well - it gets more # complex case "$1" in *word*) printf "%s\n" "$2";; esac >readlines() < # line count variable can be used to output on which line match occured #line_count=1 while IFS= read -r line; do grep_line "$line" "$filename" #line_count=$(($line_count+1)) done < "$1" >is_text_file() < # alternatively, mimetype command could be used # with *\ text\/* as pattern in case statement case "$(file -b --mime-type "$1")" in text\/*) return 0;; *) return 1;; esac >main() < for filename in ./**/* do if [ -f "$filename" ] && is_text_file "$filename" then readlines "$filename" fi done >main "$@" 

Источник

Оцените статью
Adblock
detector