Linux find include files

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.

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.

Читайте также:  Linux ping dont fragment

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.

Источник

Find all files with name containing string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

I have been searching for a command that will return files from the current directory which contain a string in the filename. I have seen locate and find commands that can find files beginning with something first_word* or ending with something *.jpg . How can I return a list of files which contain a string in the filename? For example, if 2012-06-04-touch-multiple-files-in-linux.markdown was a file in the current directory. How could I return this file and others containing the string touch ? Using a command such as find ‘/touch/’

8 Answers 8

find . -maxdepth 1 -name «*string*» -print

It will find all files in the current directory (delete maxdepth 1 if you want it recursive) containing «string» and will print it on the screen.

If you want to avoid file containing ‘:’, you can type:

find . -maxdepth 1 -name «*string*» ! -name «*:*» -print

If you want to use grep (but I think it’s not necessary as far as you don’t want to check file content) you can use:

But, I repeat, find is a better and cleaner solution for your task.

@Dru, if you want it ‘shorter’ you can avoid -print as this is the default behaviour and . as this is the default folder where it checks.

Awesome. I see myself using this a lot. I will take your -print and . removal suggestions, make it a command, and try to pass *string* in as a command line argument.

find . -name «*string*» Works great too. Removing . throws an error on my end. Thanks again @Zagorax.

Just an observation, the above command complained about the position of -maxdepth argument better to move it before -name as @Sunil Dias mentioned

I have find *.jpg -name «*from*» -print which works for a given directory. How can I make search recursively? I’ve tried -maxdepth .

-R means recurse. If you would rather not go into the subdirectories, then skip it.

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

-i means «ignore case». You might find this worth a try as well.

Great. I noticed that some file contents follow a : . Is there anyway to withhold that? Using an option perhaps?

That seems to only produce the contents of the files. You essentially answered my question though, I can try to do some digging for withholding the contents.

Ah. you only need the file names? Run : grep -R «touch» . | cut -d «:» -f 1 (sorry must have misread you).

Thanks @carlspring this is interesting. grep either returns files with contents and filenames containing touch or contents containing touch , I’m not sure which is the case, yet. Of the list of files returned, half contain touch in the title and the other half conatains touch in the body, not the title. Just realized this.

The -maxdepth option should be before the -name option, like below.,

find . -maxdepth 1 -name "string" -print 
find $HOME -name "hello.c" -print 

This will search the whole $HOME (i.e. /home/username/ ) system for any files named “hello.c” and display their pathnames:

/Users/user/Downloads/hello.c /Users/user/hello.c 

However, it will not match HELLO.C or HellO.C . To match is case insensitive pass the -iname option as follows:

find $HOME -iname "hello.c" -print 
/Users/user/Downloads/hello.c /Users/user/Downloads/Y/Hello.C /Users/user/Downloads/Z/HELLO.c /Users/user/hello.c 

Pass the -type f option to only search for files:

find /dir/to/search -type f -iname "fooBar.conf.sample" -print find $HOME -type f -iname "fooBar.conf.sample" -print 

The -iname works either on GNU or BSD (including OS X) version find command. If your version of find command does not supports -iname , try the following syntax using grep command:

find $HOME | grep -i "hello.c" find $HOME -name "*" -print | grep -i "hello.c" 
find $HOME -name '[hH][eE][lL][lL][oO].[cC]' -print 
/Users/user/Downloads/Z/HELLO.C /Users/user/Downloads/Z/HEllO.c /Users/user/Downloads/hello.c /Users/user/hello.c 

If the string is at the beginning of the name, you can do this

$ compgen -f .bash .bashrc .bash_profile .bash_prompt 

compgen is not an appropriate hammer for this nail. This little-used tool is designed to list available commands, and as such, it lists files in the current directory (which could be scripts) and it can neither recurse nor look past the beginning of a file name nor search file contents, making it mostly useless.

An alternative to the many solutions already provided is making use of the glob ** . When you use bash with the option globstar ( shopt -s globstar ) or you make use of zsh , you can just use the glob ** for this.

does a recursive directory search for files named bar (potentially including the file bar in the current directory). Remark that this cannot be combined with other forms of globbing within the same path segment; in that case, the * operators revert to their usual effect.

Note that there is a subtle difference between zsh and bash here. While bash will traverse soft-links to directories, zsh will not. For this you have to use the glob ***/ in zsh .

Источник

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