Doing sudo stuff with tramp15 Feb 2015
Somehow, I accumulated a few files owned by root that should have been
owned by me. Below, I'll describe a solution to get rid of them based
Step one: open the directory with sudo privileges
I'll obviously need the sudo privilege to change the owner of a file owned by root. Might as well start with that.
Thankfully, I do have a shortcut:
(defun sudired () (interactive) (require 'tramp) (let ((dir (expand-file-name default-directory))) (if (string-match "^/sudo:" dir) (user-error "Already in sudo") (dired (concat "/sudo::" dir))))) (define-key dired-mode-map "!" 'sudired)
The function above will open the current directory in sudo mode. I decided to bind it to !, since the default & seems strictly better than !. The function will ask you for the password once. Afterwards, you can open other directories without having to enter the password.
So now, while in sudo mode of the directory in question:
- Call M-x
find-dired. It's not bound by default, and I don't bind it myself since it's quite situational (I do bind
find-name-diredto F in
- It prompts you for the directory. I just press RET, since the default one is what I need.
- Next, it prompts for
findargs. These are the arguments to the UNIX command
find. Here, I need to pass
- The command has finished and I can see the results. I can select
some of them by pressing m (
dired-mark) a few times, or mark them all at once with t (
- Finally, call O (
dired-do-chown) on the marked files and enter
Done. I'm sure that the admin types can cook up some
chown combo to do the same thing,
find-dired approach is both simpler and more flexible, since I can confirm and edit the list
of files being changed.