Ripgrep (rg) is a popular command-line search tool based on the popular GNU grep tool. The Access is denied. (os error 5) is a common error message that appears when trying to read a file or directory that ripgrep does not have permission to access.

This can be annoying when doing a system wide search when many system directories or other protected directories may be searched, littering the results with many “access is denied” error messages. Luckily the messages can be suppressed.

Example output of “access is denied” error:

rg: Windows\SysWOW64\config: Access is denied. (os error 5)
rg: Windows\SysWOW64\Configuration: Access is denied. (os error 5)
rg: Windows\System32\config: Access is denied. (os error 5)
rg: Windows\System32\Configuration: Access is denied. (os error 5)

Solution: --no-messages to Suppress Error Messages

When doing the query add --no-messages flag. The --no-messages flag suppresses the “access is denied” message from appearing the the results. --no-messages specifically suppressed failed opening and reading of files. Error messages regarding search syntax pattern are still shown. This solution is the easiest, as you don’t have to fiddle around with excluding and including directories.

Example:

rg <query> --no-messages

Alternative: Exclude Directories

Alternatively, avoid searching that particular directory or files. This can be done by the --glob argument. --glob or simply -g can include or exclude directories. In this case we want to exclude system directories that most of the access is denied messages will be generated from. This method is a little slower than just using --no-messages as you have to figure out which directories to exclude, which may not include all the directories that errors may occur from.

In this example, while searching for the word “hello”, I exclude Windows directory, and Program Files and Program Files (x86) directories, which often I see access denied error messages. The ! excludes the directories:

rg hello -g '!{Windows*,Program Files*}'

Alternative: Run in Admin Mode

Final alternative is run the shell in elevated permissions, such as running shell in admin mode. This will allow you to access most of originally denied files. This might not be available to the user, and generally staying out of the admin console is better practice unless necessary.

Conclusion

We discussed three methods for getting rid of the “access is denied” error messages in ripgrep. They were --no-messages to suppress error messages, --glob to exclude directories, and running in admin mode.