0

I want to find files in Linux, in a specific directory without looking into its subdirectories, which contains in their text a specific string. From this answer, I tried this after removing the r of recursion:

grep -nw '/path/to/somewhere/' -e "pattern"

But it not working. I tried also this with skipping directory optin:

grep -rnw -d skip '/path/to/somewhere/' -e "pattern"

I tried to exclude any directory that is different from the current directory, but also no way:

grep -rnw -exclude-dir '[^.]' '../../path/to/somewhere/' -e "pattern"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
54l3d
  • 3,913
  • 4
  • 32
  • 58
  • `find . -type f -name '*pattern*'` – Fredrik Pihl Dec 16 '16 at 09:58
  • These will look into all files in the path for `pattern` I guess that is not what you are trying to do. `find` or `ls ... | grep` will be helpful – nu11p01n73R Dec 16 '16 at 09:59
  • if you still want to use grep try `grep /path/to/somewhere/* -d skip -le "pattern"` – bansi Dec 16 '16 at 10:06
  • May be the question was confusing, i hope its clear now, the patten should match the content not the filename ! – 54l3d Dec 16 '16 at 10:12
  • 1
    Possible duplicate of [bash script - find file containing text](http://stackoverflow.com/questions/6153152/bash-script-find-file-containing-text) – Inian Dec 16 '16 at 10:16

3 Answers3

2

May be the question was confusing, i hope its clear now, the patten should match the content not the filename !

Your question was not clear, and in the original answer I have shown how to find filenames matching a pattern. If you only want to search for files with content matching a pattern, it's as simple as

grep 'pattern' directory/*

(the shell globbing is used).

You can still use find to filter out the files before passing to grep:

find 'directory' -maxdepth 1 -mindepth 1 -type f \
  -exec grep --with-filename 'pattern' {} +

Original Answer

Grep is not appropriate tool for searching for filenames, since you need to generate a list of files before passing to Grep. Even if you get the desired results with a command like ls | grep pattern, you will have to append another pipe in order to process the files (I guess, you will most likely need to process them somehow, sooner or later).

Use find instead, as it has its own powerful pattern matching features.

Example:

find 'directory' -maxdepth 1 -mindepth 1 -regex '.*pattern'

Use -iregex for case insensitive version of -regex. Also read about -name, -iname, -path, and -ipath options.


It is possible to run a command (or script) for the files being processed with -exec action, e.g.:

find 'directory' -maxdepth 1 -mindepth 1 -type f \
  -regex '.*pattern' -exec sed -i.bak -r 's/\btwo\b/2/g' {} +
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
1

Using find:

find '/path/to/somewhere' -maxdepth 1 -type f -exec grep -H 'pattern' {} \;
Phylogenesis
  • 7,775
  • 19
  • 27
0

If the pattern must be a regular expression:

ls -A /path/to/dir | grep -E PATTERN
user1934428
  • 19,864
  • 7
  • 42
  • 87