I'm trying to use grep to remove the comments from a file, smb.conf to be specific, and bash is giving me an error. I found this question How to remove all comments from a file? that gives the syntax for grep to do this, but it doesn't overwrite the file, it just prints it to the terminal. I would like to effectively overwrite the file. This is what's going on:

 $ sudo grep -o '^[^#]*' /etc/samba/smb.conf >> /etc/samba/smb.conf
-bash: /etc/samba/smb.conf: Permission denied

I have done something like this before, but then I was following an online guide and I vaguely remember having to start up bash before I could grep. Obviously, I can't find that guide again.

How do you use grep to strip out the comments from a file?

I'm working on a Raspberry Pi B+ running Raspbian Jessie.

  • 1
    you're getting the permissions error because the elevated sudo permissions only last until the >> redirection, at which point it is your user shell. redirect it to a file you can write to, then sudo mv it; or sudo vi the file and pipe its contents to the grep
    – Jeff Schaller
    Feb 9, 2016 at 0:54
  • In any case, reading and writing to the same file would not work as intended. sed is a better approach (using -i) Feb 9, 2016 at 0:55
  • If one of the 3 answers below worked for you, please accept one of them using the check-mark. Thank you!
    – Jeff Schaller
    Feb 11, 2016 at 17:21

3 Answers 3


sed is one option that can "directly" edit a file (by making a temporary copy of it). This command will tell sed to edit /etc/samba/smb.conf -i in place, and to -n not print lines by default.

sudo sed -i -n '/^[^#]/ p' /etc/samba/smb.conf

The lines we tell it to 'p' print are the ones that match your grep regex, which I modified only slightly to remove the extraneous trailing *. Here, the regex says "match lines that start with anything other than an octothorpe".


The pattern should be specific, to avoid deleting lines containing a '#':

sudo sed -r -i 's/^[[:space:]]*#.*$//' /etc/samba/smb.conf

The question did not actually ask to delete blank lines; this command leaves them (as well as blanked lines resulting from the substitution).

Like the other solutions, this relies on the -i option, which is non-standard (see POSIX), as well as -r (also non-standard). The use of the options depends on the platform, of course.

  • Nice addition of the (optional) leading space.
    – Jeff Schaller
    Feb 9, 2016 at 1:28
  • That's usually legal (a few things, such as terminfo, do not like leading blanks for comments). Feb 9, 2016 at 1:30
sed -i -e 's/#.*$//' -e '/^$/d' inputFile

in addition to removing all comments, it removes empty lines as well. I know you asked it with grep, but I thought I'd suggest this one. Functionally it is the same or even better by removing empty lines, compacting the size of the file, which I am assuming is your target.

EDIT: yes replace # with ; and you should be good. At least on my CentOS 6 box it worked. You might need to put a \ in front of the ; if it complains about a invalid character or something.

  • That worked. If I want to remove some stuff commented out with an ;, I just change the # in the above sed command? Feb 9, 2016 at 1:01
  • Mel types faster than I do! The answer to your question is 'yes'
    – Jeff Schaller
    Feb 9, 2016 at 1:02
  • You would not need to escape the ; here, as we're inside quote marks. Only thing I'd suggest is that there's no need to $ anchor after a .* The * is greedy and will gobble to EOL by itself.
    – Jeff Schaller
    Feb 9, 2016 at 1:26
  • Normally, I wouldn't escape either. As I indicated, the command works without any problem with either # or ; as the comment delimeters, on my CentOS 6 server. But since user indicated his target platform is raspbian, I thought the shell interpretation might be different as I haven't had the pleasure of dealing with any RasPi server yet.
    – MelBurslan
    Feb 9, 2016 at 1:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.