The command also allows to use regular expresions in the pattern. rnw '/path/to/somewhere/' -e "pattern" *Īnother alternative to grep is ack, this command will let you search entire file system or the path you want.Īck could be easy to use than grep: ack 'pattern'Īck will search in the current path where it was executed.
#Grep search all files for text code#
Search all c source code files, filename with. git when searching in a git repo with grep). exclude, -include, -exclude-dir or -include-dir are very useful when you want to exclude directories (for example excluding. * /path/to/somewhere/ is the directory, if you want to search the whole filesystem use / (you will need to use sudo). * pattern is the text you are looking inside files. Using grep is very easy, just try the command: grep -rnw '/path/to/somewhere/' -e "pattern" * Have you ever struggle to make it work the command find and it doesn't work? Here we explain the proper way of finding text in multiple files.
#Grep search all files for text how to#
The -0 option (or -null on some systems) corresponds to the -print0 option of find.In this tutorial we are gong to explain how to find a text string on a Linux system on a specific directory or just the whole filesystem. So, replacing the find command above with the locate almost-equivalent: $ locate '*.txt' -0 | xargs -0 fgrep texthere The database does only contain files in directories accessible by all users, so if you have removed the read-permissions on your own directories, your files won't be there. The locate command doesn't search the file hierarchy but instead uses a database (which is usually updated daily, so not all files may be there). but I would generally advise against that because people have a tendency to mess up their systems by forgetting they are root.ĪNOTHER SOLUTION would be to use the locate command to locate all the. Or you could just sudo -s to get a root shell and run the thing from there. Or you could try to craft another -perm flag for find (and also get it to ignore directories that you can't enter), but that would be too time consuming and result in a ridiculously long command line, so I won't do that here. You can either run find and fgrep with sudo prepended to them: $ sudo find. Obviously there will be files that you aren't able to read. So, if you have a plain text string and you want to look for it in every single file on the whole system: $ find / -type f \! -perm -o=x -name "*.txt" -print0 | xargs -0 fgrep texthere This is a utility which is exactly equivalent to grep -F that you should use if your search string is a plain text string with no regular expressions in it. It's because you're not using those two option that I believe you get those "no such file or directory" errors. And the corresponding option to xargs for receiving these as nul-delimited file names is -0. This ( -print0) means each file name will be delimited by a nul character ( \0) rather than a space character.
To be able to pass these properly between find and grep we do $ find / -type f \! -perm -o=x -name "*.txt" -print0 | xargs -0 grep texthere
Some file names might have spaces in them, or other wonky character that we usually don't want in file names. One is a safety thing, and the other may possibly improve speed a tiny bit. Now, there's a couple of more things we can do. I'm also specifying that I want files ( -type f) that are not executable ( \! -perm -o=x) (the ! needs to be escaped so that your shell doesn't do funny things with it).
with / here, because that's where I think you are (correct me if I'm wrong). Since it looks like you're only interested in plain text files, we can exclude any other type of file (executables): $ find / -type f \! -perm -o=x -name "*.txt" | xargs grep texthere You can use find to narrow down the amount of files to look at.Īt the moment, you have $ find. It's just an awful lot of files to search. It will take a long time no matter what you do. If you want to search absolutely all files on the whole system, than what you're doing is pretty much right. But since you say it takes an awfully long time, I'm leaning more towards the first assumption (the root directory). If you're getting errors about permissions (you don't say you do), then I'm guessing you're standing in either the root directory ( /) or in some path where you don't have permission to read all files, such as in /etc or in /var.