1

How can I access and edit a text file found in a folder owned by another user?

I am in the original user account setup for this macOS Mojave installation, and have the system admin password. So I perform su or sudo as needed.

I do not want to change the permissions on the containing folder nor the text file. So this Question does not apply.

I just want to:

  1. Open the text file, preferably in TextEdit.app.
  2. Edit the text.
  3. Save the file.

Bonus: Be able to navigate the folder hierarchy and see file listings using the Finder or via cd/ls command-line tools in the console (Terminal.app).


I tried sudo cd /path/to/protected_folder, and entered admin password as prompted, without error. But then ls shows me my starting directory rather than the /path/to/protected_folder folder contents.

I tried sudo open /path/to/protected_folder which does bring the Finder to the front, but with the protected folder showing and selected, but not opened.

1

If you know the path to the file that you want to edit, you can use:

sudo -e /path/to/file/name/here.txt

and it will allow you to edit it with your $EDITOR of choice.

If you want to use the Open dialog box, you might think this will work:

sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit

But that fails with an error:

"Illegal instruction: 4"

However, BBEdit will work:

sudo /Applications/BBEdit.app/Contents/MacOS/BBEdit

And you can use BBEdit for free.

Oh, and both methods keep the original ownership of the file and permissions intact.

  • This answer needs more explanation. Basically you can edit with sudo and a terminal based editor. A gui one like texted it won't work with sudo. – Mark Oct 26 '18 at 19:28
  • Um… a) BBEdit is not a terminal-based editor, and b) GUI apps can and do work with sudo. Just like I demonstrated in my answer. The fact that TextEdit doesn't is an exceptional case, which is why I mentioned it specifically. – TJ Luoma Oct 27 '18 at 0:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .