Permission Denied Grep

I am completing a school assignment and it's pretty much based off of a murder case. I downloaded the custom files and I am currently searching for keywords within files. The question I am answering right now is: "Look for a file containing the text “apt. no.” to find that info".

I am fairly new to Linux and I am currently getting Permission Denied whenever I am entering the following command:

grep -inr apt.no

Any help on how to fix this would be greatly appreciated. The file I am looking for contains apt.no text in the file.

bbcharlieca@myvm:~$ grep -inr apt.no.
.viminfo:92:|2,1,1635317358,47,"apt.no"
.viminfo:92:|2,1,1635317355,47,"apt.no"
grep: nano.save: Permission denied
bbcharlieca@myvm:~$ █

enter image description here

10

1 Answer

The -r flag to grep instructs it to search every file in your current directory and in all subdirectories. (You can find out about -r, -i-, and -n by reading the documentation, man grep.)

The command matched apt.no. in the file .viminfo, and printed the matching lines. Notice in your text you say you want to search for apt.no but in your screenshot (ugh; much better to copy and paste as text) you actually typed apt.no. with a trailing dot. Furthermore, your instructions seem to want you to search for apt. no. (with a space). Be aware that with grep, the dot is a wildcard character that will match any character, and if you're searching for text that contains a space it must* be placed inside quotes. Perhaps you meant to run this command, which looks for the literal string apt. no. without any wildcard matching?

grep -rF 'apt. no.'

Your grep also attempted to search the file nano.save, but this appears either not to be owned by you, or else you've removed your read permissions from it. If the file is under your own home directory you could probably safely remove this file (rm nano.save). If it's in a shared area you're either going to have to live with the error message, explicitly exclude it from grep, or tell the shell to discard all error messages from the command:

grep -Fr 'apt. no.' # Live with it
grep -Fr --exclude='nano.save' 'apt. no.' # Exclude named file
grep -Fr 'apt. no.' 2>/dev/null # Discard *all* error messages

If you have root permissions you could search with those permissions, but jumping up to root level should done with care; there are almost no restrictions whatsoever if you are running as root:

sudo grep -Fr 'apt. no.' # Run as root

* If not placed in quotes you need to escape every occurrence of a special character, such as a space. I'm not going to do that here.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like