How to use grep on all files non-recursively in a directory?

John Red

I want to search for a string of text in all files in a directory (and not its subdirectories; I know the -r option does that, but that is not what I want).

  1. Running

    grep "string" /path/to/dir
    

    is supposed to be able to do this, I've read, but it gives me the error:

    grep: dir: Is a directory

  2. Next, I tried running grep on multiple files.

    grep "string" .bashrc .bash_aliases works perfectly.

    grep "string" .bash* works as intended too.

    grep "string" * gives me the errors:

    grep: data: Is a directory
    grep: Desktop: Is a directory
    grep: Documents: Is a directory
    grep: Downloads: Is a directory
    ...
    

Only the errors are printed, I don't get the matching lines. I tried using the -s option, but to no avail.

So, my questions:

  1. Why am I not being able to use grep on a directory, as in (1), when I should be able to? I've seen that done in plenty examples on the Internet.
    Edit: When I say "using grep on a directory", I mean "search in all the files in that directory excluding its subdirectories". I believe that this is what grep does when you pass a directory to it in place of a file. Am I incorrect?

  2. Please give me an explanation on the workings of grep that would explain the behavior of commands in (2).
    Edit: Let me be more specific. Why does using wildcards to specify multiple files to search in for work with .bash* and not with * or even ./*?

  3. How can I search all the files in a directory (and not its subdirectories) using grep?

wjandrea

In Bash, a glob will not expand into hidden files, so if you want to search all the files in a directory, you need to specify hidden files .* and non-hidden *.

To avoid the "Is a directory" errors, you could use -d skip, but on my system I also get an error grep: .gvfs: Permission denied, so I suggest using -s, which hides all error messages.

So the command you are looking for is:

grep -s "string" * .*

If you are searching files in another dir:

grep -s "string" /path/to/dir/{*,.*}

Another option is to use the dotglob shell option, which will make a glob include hidden files.

shopt -s dotglob
grep -s "string" *

For files in another dir:

grep -s "string" /path/to/dir/*

† Someone mentioned that I shouldn't get this error. They may be right - I did some reading but couldn't make heads or tails of it myself.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use grep on all files non-recursively in a directory?

From Dev

How do I use grep to find all the zip files in a directory?

From Dev

How do I use grep to find all the zip files in a directory?

From Dev

Recursively setting all files in a directory as non-executable

From Dev

How can I recursively grep particular files in a directory?

From Dev

How to copy all files in directory recursively and unzip compressed files on fly

From Dev

How do i checkout files in a directory non recursively in CVS

From Dev

How do i checkout files in a directory non recursively in CVS

From Dev

How to use grep to find *.part files in a directory

From Dev

how to search for a line match from all files recursively found in a directory

From Dev

How to search and remove ^M in all files in a directory recursively?

From Dev

How do I copy all files recursively to a flat directory in Ruby?

From Dev

How can I move all files in subdirectories recursively to a single directory?

From Dev

How can I move all files in subdirectories recursively to a single directory?

From Dev

How to recursively list all files of desired file type in a specified directory?

From Dev

How to find all files that contain the + sign on a directory recursively?

From Dev

How do I gunzip all files recursively in a target directory?

From Dev

Search all xml files recursively in directory for a specific tag and grep the tag's value

From Dev

How do I use grep to search the current directory for all files having a given string and then move these files to a new folder?

From Dev

Recursively list all files in directory (Unix)

From Dev

Recursively apply a command to modify all files in a directory

From Dev

Recursively list all files in directory (Unix)

From Dev

Remove all but N files from directory recursively

From Dev

How can I recursively copy all pdf files in a directory (and it's subdirectories) into a single output directory?

From Dev

Command line to search all files in all text files in directory recursively

From Dev

How to recursively search a directory for all files including hidden files in hidden directories, with PowerShell?

From Dev

How to diff all modifications of all checkout files under a directory recursively with Clearcase?

From Dev

How to diff all modifications of all checkout files under a directory recursively with Clearcase?

From Dev

Recursively do something with all files in a given directory including subdirectory files

Related Related

  1. 1

    How to use grep on all files non-recursively in a directory?

  2. 2

    How do I use grep to find all the zip files in a directory?

  3. 3

    How do I use grep to find all the zip files in a directory?

  4. 4

    Recursively setting all files in a directory as non-executable

  5. 5

    How can I recursively grep particular files in a directory?

  6. 6

    How to copy all files in directory recursively and unzip compressed files on fly

  7. 7

    How do i checkout files in a directory non recursively in CVS

  8. 8

    How do i checkout files in a directory non recursively in CVS

  9. 9

    How to use grep to find *.part files in a directory

  10. 10

    how to search for a line match from all files recursively found in a directory

  11. 11

    How to search and remove ^M in all files in a directory recursively?

  12. 12

    How do I copy all files recursively to a flat directory in Ruby?

  13. 13

    How can I move all files in subdirectories recursively to a single directory?

  14. 14

    How can I move all files in subdirectories recursively to a single directory?

  15. 15

    How to recursively list all files of desired file type in a specified directory?

  16. 16

    How to find all files that contain the + sign on a directory recursively?

  17. 17

    How do I gunzip all files recursively in a target directory?

  18. 18

    Search all xml files recursively in directory for a specific tag and grep the tag's value

  19. 19

    How do I use grep to search the current directory for all files having a given string and then move these files to a new folder?

  20. 20

    Recursively list all files in directory (Unix)

  21. 21

    Recursively apply a command to modify all files in a directory

  22. 22

    Recursively list all files in directory (Unix)

  23. 23

    Remove all but N files from directory recursively

  24. 24

    How can I recursively copy all pdf files in a directory (and it's subdirectories) into a single output directory?

  25. 25

    Command line to search all files in all text files in directory recursively

  26. 26

    How to recursively search a directory for all files including hidden files in hidden directories, with PowerShell?

  27. 27

    How to diff all modifications of all checkout files under a directory recursively with Clearcase?

  28. 28

    How to diff all modifications of all checkout files under a directory recursively with Clearcase?

  29. 29

    Recursively do something with all files in a given directory including subdirectory files

HotTag

Archive