11

Say I have a folder open in Nautilus that's in my /home/user/temp directory. I'd like to move a folder from there to my /opt directory (it's a program). Is there a way I can elevate the copy command up to a sudo so that I can copy the folder without having to start a new instance of Nautilus from the command line?

enzotib
  • 93,831
jcollum
  • 1,032

6 Answers6

6

As far as I can say, based on what I know and have experienced:

sudo is used for command line applications/commands and gksudo is useful when you try to run a program using the Run Application dialog window by pressing Alt+F2.

I have read that gksudo is just the graphic version of sudo.

Anyway, you can both drop sudo nautilus and/or gksu nautilus in a terminal for the purpose to do what you wish, having the same effect both commands. But if you want to omit the terminal and you wish to run it directly with the "Run Application" dialog window, just press Alt+F2 and write gksu nautilus, after which you will be prompted to enter your password, then the nautilus file browser will open with root privileges.

Additionally, you can achieve to open files/folders as root with a single click by using the "Open as Administrator" option via right click. Which in any case will open a new nautilus instance for the given folder and will open files as root, this may also open/run applications as root but I haven't tested it yet.

enter image description here

You can get the "Open as Administration" option in your contextual menu by installing nautilus-gksu via command line: sudo apt-get install nautilus-gksu or by using synaptic as shown in the next image:

enter image description here

Good luck!

  • It looks like I have to add something to get the "Open as" command. Do you recall what it was? I'm in Ub 11. – jcollum Jan 25 '12 at 18:53
  • nautilus-gksu, via synaptic (image added) or command line (also provided in the answer) – Geppettvs D'Constanzo Jan 25 '12 at 19:13
  • Excellent, thanks for understanding my question instead of saying "sudo mv". Thorough too. – jcollum Jan 25 '12 at 19:14
  • 3
    Here is why we should use gksu nautilus not sudo: http://askubuntu.com/q/11760/ – Takkat Jan 25 '12 at 20:03
  • Thank you for the edits. But the last edition wasn't what I wrote. Please avoid editing my text if it has already been accepted. It was reading a different thing. Thank you anyway. – Geppettvs D'Constanzo Jan 26 '12 at 00:34
  • 2
    Did nautilus-gksu move? Disappear? I can't find it in USC or apt-get. – jcollum Apr 25 '13 at 20:40
  • It seems to be a bug starting from 13.04, there are several questions about it over here, like this:http://askubuntu.com/a/284717/9598 and in some other support/bug report sites: https://answers.launchpad.net/ubuntu/+source/gksu/+question/227275. If the solutions provided in such sites won't work for you I suggest you to open a new question including as much details as you can in order to receive the proper support. Good luck! – Geppettvs D'Constanzo Apr 25 '13 at 21:49
  • 2
    @jcollum nautilus-gksu no longer exists in the repository as of Ubuntu 12.04. – IQAndreas Jul 15 '13 at 02:08
  • both nautilus and nemo mess up the config file when run with sudo --- next time you open nautilus it will complain about not being able to access "/home/username/.local/share/gvfs-metadata/home". This answer explains this: http://askubuntu.com/a/11766/41363 – Ayrat Oct 03 '16 at 08:16
5

you need to run Nautilus as root

type this in the terminal

gksu nautilus

now you can move using the GUI.

or use this command

sudo mv -r /home/user/temp/<foldername>/ /opt/
sarvesh.lad
  • 2,524
  • 1
    So the answer is "You can't without restarting Nautilus"? – jcollum Jan 25 '12 at 18:30
  • you don't need to restart nautilus, just open another instance with the gksu command. But if you mean "open nautilus with my user and then got root privileges in that instance to move files" maybe that's what you need http://www.upubuntu.com/2011/12/how-to-open-files-folders-as-root-from.html – zurdo Jan 25 '12 at 18:46
  • You will need to open a new nautilus window, but to make things easier you copy the location from the bar, and run the command: gksu nautilus "/path/to/dir/pasted/here" (the quotes around the path are important) – IQAndreas Jul 15 '13 at 02:07
1

Here's the nautilus-script I use to open an admin (root) Nautilus window:

#!/bin/bash
# This Nautilus script opens the current nautilus window in admin mode.
# Requires: perl, liburi-perl

ERROR_NEED_PERL="This script requires the liburi-perl package. Install it and try again."
GKSUDO_MESSAGE="Enter your password to open an admin window on: "
ERROR_BROKEN_LINK="Broken link."

## Check for liburi-perl (and hence perl)
let PERLOK=$(dpkg-query -W --showformat='${Status}\n' liburi-perl|grep "install ok installed")
if [ "" == "$PERLOK" ]; then
   zenity --error --text "$ERROR_NEED_PERL"
   exit 1
fi

let LEN_NSSFP=${#NAUTILUS_SCRIPT_SELECTED_FILE_PATHS}-1
[ $LEN_NSSFP -lt 0 ] && let LEN_NSSFP=0
let LEN_NSSU=${#NAUTILUS_SCRIPT_SELECTED_URIS}-1
[ $LEN_NSSU -lt 0 ] && let LEN_NSSU=0

## if clicking happens on the Desktop (or a file or folder on it),
## $1 will be a path (i.e. with "/" in it); otherwise (in a folder
## window) $1 will be just a file or folder name (without path).
## Note that selecting the home desktop namespace extension yields
## a $# of zero but NAUTILUS_SCRIPT_SELECTED_FILE_PATHS pointing to the
## target (in the computer (computer:///) and trash (trash:///) desktop
## namespace extension cases, ...PATHS is empty).

## Initially, we stripped the file:// prefix from NAUTILUS...CURRENT_URI,
## yielding the full path, and then retrofit spaces, like this:
#OBJECT="`echo -n $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"
## However, this fails if any special characters other than spaces are in the path,
## such as accented letters, etc. We need to convert not just spaces, but any
## UTF-8 embedded in there...The URI<->path conversion requires perl (and liburi-perl):
OBJECT=$( echo "$NAUTILUS_SCRIPT_CURRENT_URI" | perl -MURI -le 'print URI->new(<>)->dir' )
## ->file is to be used for file URIs instead of ->dir, which is for directory URIs

CONTEXT="$OBJECT"
## Add the selection to the path, if defined and unique
if [ $# -eq 1 ] ; then
   ## If a single Desktop object, override
   if echo $1 | grep -q "/" ; then ## Desktop (or object on desktop)
      OBJECT="$1"
      CONTEXT=""
   else ## $1 is a simple file or folder name, without a path
      ## The container could be root (/)
      OBJECT="${OBJECT%/}/$1"
   fi
# elif [ $# -eq 0 -a -n "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ] ; then
elif [ $# -eq 0 ] ; then
   ## desktop name space extension selected?
   if [ -n "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ] ; then ## Home
      OBJECT="${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS:0:LEN_NSSFP}"
   elif [ -n "$NAUTILUS_SCRIPT_SELECTED_URIS" ] ; then ## Computer, Trash
      ## These typically map to root (/)
#     OBJECT="`echo ${NAUTILUS_SCRIPT_SELECTED_URIS:0:LEN_NSSU} | cut -d'/' -f3- | sed 's/%20/ /g'`"
      OBJECT="${NAUTILUS_SCRIPT_SELECTED_URIS:0:LEN_NSSU}"
      OBJECT=$( echo "$OBJECT" | perl -MURI -le 'print URI->new(<>)->dir' )
   fi
   CONTEXT=""
fi
## Note that a desktop shortcut (.desktop file) does not trip -h
if [ -h "$OBJECT" ] ; then ## symbolic link
   ## readlink has no "follow symlinks as far as they exist" option
   OBJECT=`readlink -e "$OBJECT"`
   if [ -z "$OBJECT" ] ; then
      zenity --error --text "$ERROR_BROKEN_LINK"
      exit 1
   fi
fi

# zenity --info --text "\$OBJECT is « $OBJECT »"
if [ -f "$OBJECT" ] ; then
   ## Regular file
   DIR=`dirname "$OBJECT"`
else
   ## Directory (or no object)
   DIR="$OBJECT"
fi

## If DIR is empty, gnome-open opens in the default (home) directory (i.e. "~") anyway
#if [ -z "$DIR" ] ; then
#   DIR=~
#fi

## At this point, the test [ ! "$CONTEXT" = "$DIR" ] serves to indicate
## that the target directory is not matched to the one the script was
## invoked from (if any).

gksudo --message "$GKSUDO_MESSAGE$DIR" gnome-open "$DIR"

exit $?
Urhixidur
  • 233
0

Another dandy solution is to launch another copy of Nautilus with root (sudo) privileges from a command line:

gksudo xdg-open <path> &

The closing & means the command runs as a background job; hence the use of gksudo (using sudo would mean an invisible prompt that you can't respond to). xdg-open takes care of launching an explorer window (nautilus or other).

You may need to install the xdg-utils and gksu packages beforehand.

You may get a bunch of warnings and Gtk-CRITICAL and Glib-GObject-CRITICAL messages upon closing the elevated Nautilus window, but these are harmless as far as I can tell. I'd love to get rid of them, if anyone knows how.

Urhixidur
  • 233
-1
sudo mv /home/user/temp/[Filename] /opt

Instead of [Filename], type the name of your file without brackets [].

Tyler
  • 37
-2

Another easy way is

sudo gnome-open foldername

Or I would recommend installing nemo file manager. It has 'open as root' in its right click context menu