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" 
5
  • find . -type f -name '*pattern*' Commented Dec 16, 2016 at 9: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 Commented Dec 16, 2016 at 9:59
  • if you still want to use grep try grep /path/to/somewhere/* -d skip -le "pattern" Commented Dec 16, 2016 at 10:06
  • May be the question was confusing, i hope its clear now, the patten should match the content not the filename ! Commented Dec 16, 2016 at 10:12
  • 1
    Possible duplicate of bash script - find file containing text Commented Dec 16, 2016 at 10:16

3 Answers 3

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' {} + 
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, grep 'pattern' * is more than enough.
Although it shows error for subdirectories, but its acceptable answer, thx man !
1

Using find:

find '/path/to/somewhere' -maxdepth 1 -type f -exec grep -H 'pattern' {} \; 

Comments

0

If the pattern must be a regular expression:

ls -A /path/to/dir | grep -E PATTERN 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.