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:
- Open the text file, preferably in TextEdit.app.
- Edit the text.
- 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.
Best Answer
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.