Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A shell script to edit text files as root in the GUI UNIX
I often need to edit special (i.e. system-owned) files, and until recently, the best method I could come up with was to find the file, give myself Read/Write access, edit it, then restore the previous rights. This becomes a little tiresome through the Get Info window, so I came up with a better solution. I use SubEthaEdit, but it should work with just about any editor (I do use pico when I am telnetting in from another computer, for example).

Add the following into your ~/.profile (bash) or wherever it belongs under another shell:
function edit
{
    if test `echo $TERM_PROGRAM` == 'Apple_Terminal' ; then 
      open -a 'SubEthaEdit.app' "$*" &
    else
      pico "$*"
    fi
}

function sedit
{
    if test `echo $TERM_PROGRAM` == 'Apple_Terminal' ; then
      sudo ~/Applications/SubEthaEdit.app/Contents/MacOS/SubEthaEdit "$*" &
    else
      sudo pico "$*"
    fi
}
Now, start a new shell session, and use edit to edit files normally, and sedit to edit them as a super-user. You will need to be in the sudoers group (or whatever it's called) -- being an admin should suffice. I also set the background colours so that editing normally is blue, and sediting is reddy/orange, so I don't forget I am a super-user.
    •    
  • Currently 2.75 / 5
  You rated: 2 / 5 (4 votes cast)
 
[16,944 views]  

A shell script to edit text files as root in the GUI | 19 comments | Create New Account
Click here to return to the 'A shell script to edit text files as root in the GUI' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A shell script to edit text files as root in the GUI
Authored by: rammjet on Aug 31, '04 10:16:11AM

Use BBEdit. Go to File menu -> Open hidden...
Find and open the file. Edit it. Save file. Authentication box will appear. If you are admin, enter your password. File will be saved.



[ Reply to This | # ]
A shell script to edit text files as root in the GUI
Authored by: klktrk on Aug 31, '04 12:52:36PM

Not always. Try doing that with a 600 perm'd file owned by root. (Error code -5000).



[ Reply to This | # ]
A shell script to edit text files as root in the GUI
Authored by: bawbb on Sep 01, '04 12:08:11AM

Ah, maybe I am missing something, or maybe it is because I have an UNIX background and not an OS9 one but..


sudo vi filename


seems to work fine and is quite simple!



[ Reply to This | # ]
A shell script to edit text files as root in the GUI
Authored by: klktrk on Sep 01, '04 01:17:00AM

I was responding to someone who was recommending using BBEdit, not vi, or emacs, or pico.



[ Reply to This | # ]
A shell script to edit text files as root in the GUI
Authored by: jmontana on Aug 31, '04 10:37:15AM
This hint assumes your editor (SubEthaEdit in this case) resides in the Applications folder within your home folder. If your editor is in the main Applications folder, you'll want to remove the leading ~ character so
sudo ~/Applications/SubEthaEdit.app/Contents/MacOS/SubEthaEdit "$*" &
becomes
sudo /Applications/SubEthaEdit.app/Contents/MacOS/SubEthaEdit "$*" &


[ Reply to This | # ]
A shell script to edit text files as root in the GUI
Authored by: daniel_steffen on Aug 31, '04 10:37:53AM

or use '/usr/libexec/authopen', c.f. 'man authopen'



[ Reply to This | # ]
A shell script to edit text files as root in the GUI
Authored by: sjonke on Sep 01, '04 10:23:06AM

Can you give an example of how you use authopen to open a file in, say TextEdit, with sudo privileges? I can't figure it out from the man page.

---
--- What?



[ Reply to This | # ]
A shell script to edit text files as root in the GUI
Authored by: fds on Aug 31, '04 10:51:36AM
sudo command &

will not work properly if sudo still has to ask for your password. Use

sudo -b command

instead.

[ Reply to This | # ]

Pseudo
Authored by: alani on Aug 31, '04 12:56:18PM

Pseudo is a good GUI utility that runs applications as Root. $15 does seem a bit steep, though, considering how easy it is to approximate the functionality in the terminal.
http://personalpages.tds.net/~brian_hill/pseudo.html

Also, by the way, SubEthaEdit doesn't (yet) have a good facility for navigating hidden directories and files.

To open a hidden directory (e.g., /etc ), in the open/save dialog, press shift-cmd-G (I think this works in any app's open/save dialog). To open a hidden file, you're stuck using a shell command (e.g., "open -a SubEthaEdit .htaccess" ). Thanks to SEE's developers for that information.



[ Reply to This | # ]
A shell script to edit text files as root in the GUI
Authored by: davidbodonnell on Aug 31, '04 02:23:32PM

Why not just use

sudo open -a $APPLICATION $FILE

e.g.,

sudo open -a BBEdit /var/log/foo.log

[ Reply to This | # ]
A shell script to edit text files as root in the GUI
Authored by: aqsalter on Sep 01, '04 12:40:29AM

This does not work because you are running "open" as root, but BBEdit still runs as yourself.
The suggestion above about "authopen" may do the trick. (or the current hint also works of course ;)



[ Reply to This | # ]
A shell script to edit text files as root in the GUI
Authored by: davidbodonnell on Sep 01, '04 05:14:10PM

Ah, I see where the problem lies: most of the files I wanted to look at were owned by root:admin, and I am in the admin group. Of course, if you open a file that is outside your group, BBEdit (the application I chose to test) will just ask for your admin password when you Save it and will proceed to save away.

Personally, if I am going to drop to the CLI to edit files, I'll just stick with emacs :)



[ Reply to This | # ]
A shell script to edit text files as root in the GUI
Authored by: geordie on Aug 31, '04 03:21:15PM

The bbedit command line tool deals with this automatically. Yet another reason why BBEdit doesn't suck.



[ Reply to This | # ]
Smultron
Authored by: gatorparrots on Aug 31, '04 06:13:59PM
http://smultron.sourceforge.net/

"Smultron is an open-source text editor written in Cocoa for Mac OS X Panther with many of the features that you might need. It features easy selection of the opened documents, line numbers, support for syntax colouring for many different languages, support for text encodings, code snippets, a toolbar, a status bar, HTML preview, multi-document find and replace with regular expressions, possibility to show invisible characters, authenticated saves, command-line utility, .Mac synchronisation of preferences and many more features."

[ Reply to This | # ]

Smultron
Authored by: dontlikehippies on Aug 31, '04 07:51:06PM

I love Smultron, but couldn't get it to take the filename as an argument... anybody else have any luck?



[ Reply to This | # ]
Smultron
Authored by: sapporo on Sep 01, '04 04:57:07AM

Like BBEdit, Smultron comes with a command line utility called smultron. To pass filenames as arguments, make sure to use the command line utility, and not Smultron.app.

I use this all the time, and it works. Too bad it only takes one argument, so you can't open several files at once (AFAIK). BTW, if you pass a filename that doesn't exist, smultron will create the file first and then open it.



[ Reply to This | # ]
A shell script to edit text files as root in the GUI
Authored by: jeremyp on Sep 01, '04 10:21:49AM
Or you could just do

sudo vi a-protected-file

Personally, I don't like it to be too convenient to edit system files. They're protected for good reason.

[ Reply to This | # ]
philosophy of hidden files
Authored by: alani on Sep 01, '04 01:23:58PM

Well, many hidden files are kind of important and can break things, but other things are kept invisible so non-technical users don't get overwhelmed by the multitude of configuration files and directories in their home directories.

Other files need to be invisible to remote, read-only uers (things like .htaccess), and the system is not very sensitive to them (as opposed to things related to the file system, for example).

Often, I find that the administrative work I do involves several files needing coordinated edits (e.g., if I want to do something with my mail system, I have two Postfix config files, global and local Procmail configs, and many SpamAssassin config files).

That's when I'm really happy that I can use Pseudo to run BBEditLite as Root, then BBEditLite's "Open hidden" command to open the files I need.

But your point is well taken in consideration of the system files.



[ Reply to This | # ]
A shell script to edit text files as root in the GUI
Authored by: rkchang on Sep 01, '04 09:20:30PM

I've had no problem. Did you install the command line utility? It's not installed by default, but you can easily do so under the Help menu.

---
"I have seen the evils of procrastination, and I vow to change my ways tomorrow."



[ Reply to This | # ]