Assuming that the grep tool should be used, I’d like to search for the text string “800×600” throughout the entire file system.
I tried:
grep -r 800x600 /
but it doesn’t work.
What I believe my command should do is grep recursively through all files/folders under root for the text “800×600” and list the search results.
What am I doing wrong?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I normally use this style of command to run grep over a number of files:
find / -xdev -type f -print0 | xargs -0 grep -H "800x600"
What this actually does is make a list of every file on the system, and then for each file, execute grep with the given arguments and the name of each file.
The -xdev argument tells find that it must ignore other filesystems – this is good for avoiding special filesystems such as /proc. However it will also ignore normal filesystems too – so if, for example, your /home folder is on a different partition, it won’t be searched – you would need to say find / /home -xdev ....
-type f means search for files only, so directories, devices and other special files are ignored (it will still recurse into directories and execute grep on the files within – it just won’t execute grep on the directory itself, which wouldn’t work anyway). And the -H option to grep tells it to always print the filename in its output.
find accepts all sorts of options to filter the list of files. For example, -name '*.txt' processes only files ending in .txt. -size -2M means files that are smaller than 2 megabytes. -mtime -5 means files modified in the last five days. Join these together with -a for and and -o for or, and use '(' parentheses ')' to group expressions (in quotes to prevent the shell from interpreting them). So for example:
find / -xdev '(' -type f -a -name '*.txt' -a -size -2M -a -mtime -5 ')' -print0 | xargs -0 grep -H "800x600"
Take a look at man find to see the full list of possible filters.
Method 2
Normally you wouldn’t want to actually search EVERYTHING on the system. Linux uses file nodes for everything, so some “files” are not things you would want to search. For example /dev/sda is the physical block device for your first hard drive. You probably want to search the mounted file systems not the raw disk device. Also there is /dev/random which spits out random data every time you read it. Searching that doesn’t make a lot of sense. The /proc file system is also problematic in your case.
I would recomend one of two things.
-
Don’t search at root, only search the places that might be useful. Search
/homeor/usror/etcseparatly. The info you are looking for is likely of a specific type, so it’s likely to be in a specific folder anyway. Configuration settings should be in/etc. Your personal data files should be in/home. Limiting search to a major area like this will greatly reduce your problems with recursive greps. -
Exclude problematic areas using
--exclude-dirand a set of things you know you don’t needlike this:
grep -r --exclude-dir /proc --exclude-dir /dev --exclude-dir /tmp --exclude-dir /lost+found
Lastly, it’s not uncommon to run across a few ‘permission-denied’ errors when doing a big recursive grep. In the normal course of use there are files your user may not be able to read. As long as these are just a few odd files and not things like the raw device for your hard drives or the entire proc file system, it’s ok to just ignore the errors. In fact you can do this on the command line by sending all the errors into never never land:
grep -r search_string /path 2> /dev/null
Method 3
For simplicity, I would suggest ack-grep. Link shows many cases when ack-grep is a better option.
To use is, after install:
ack-grep pattern /
Method 4
Another way of looking at it is this way:
grep -r /* | grep "800x600"
Method 5
If you want to run search for a text only in files matching a specific name pattern (e.g. .txt files) you can do:
grep -rlw --include="*.txt" -e "text to search" path/to/dir
Omit the w option if you also want to find files where the search text is a substring of the file content.
Method 6
*then I get a /proc/sysrq-trigger: Input/output error
Your command is working. You are getting this error because you are trying to scan running processes for a string.
I recommend excluding system directories with
grep --exclude-dir={proc,sys} "800x600" /
Method 7
I use the following:
grep -sIr --exclude-dir={dev,proc,run,sys} "800x600" /
ssuppresses error messages like non-existent or unreadable filesIignores binary filesrenables recursive search- Sometimes
lto display only the filename
Method 8
simply right-
grep -r "800x600" /
-What is wrong in your current command is the quotes, “”. Always put the string argument to grep in quotes.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0