Find the answer to your Linux question:
Results 1 to 4 of 4
ok well i am just trying to learn the linux filesystem in the shell so i have been pretty much just been running Code: cd /randomdirectory then running Code: ls ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie champ_weller's Avatar
    Join Date
    Sep 2011
    Location
    ohio
    Posts
    102

    sudo and cd


    ok well i am just trying to learn the linux filesystem in the shell so i have been pretty much just been running
    Code:
    cd /randomdirectory
    then running
    Code:
    ls -l
    to see whats in the directory well i tried looking in the /proc and /root directory and i get permission denied.
    so i try
    Code:
    sudo cd /root
    and i get command not found how can i swith to the directory?

    oh btw i am running ubuntu 11.04 and i only have one user installed on the maching so su doesnt switch me to root.

  2. #2
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,567
    First you switch to a root level of privileges
    Code:
    sudo -i
    (that's a small eye) and then you do your cd.

    Also Take the Linux Filesystem Tour | TuxRadar Linux
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    The Fifth Continent

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    "cd" is not really a program: instead, it is an action taken by a shell. The reason that sudo complains is that sudo only understands programs:
    Code:
    [alex@niamh ~]$ which cd
    which: no cd in (/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/bin/core_perl)
    elija's suggestion works because "sudo -i" launches a new shell as the user: it's essentially the same as running "sudo bash". This creates a shell that runs as that user, so you can cd as that user:
    Code:
    [alex@niamh ~]$ whoami
    alex
    [alex@niamh ~]$ sudo -i
    [root@niamh ~]# whoami
    root
    I personally prefer to avoid this (it falls into the same pitfalls that the su command does) because it can be a security risk.

    Instead, you can try something like "sudo ls /root", and therefore use sudo in its generally intended way.

  4. #4
    Administrator jayd512's Avatar
    Join Date
    Feb 2008
    Location
    Kentucky
    Posts
    5,023
    There's another option for you, as well.
    You can use su -c to perform root functions one at a time.
    Code:
    su -c 'ls -l /root
    Enter your admin password, good to go. And, it avoids the security risk that Cabhan mentioned, as it doesn't save the password for any amount off time.
    Jay

    New users, read this first.
    New Member FAQ
    Registered Linux User #463940
    I do not respond to private messages asking for Linux help. Please keep it on the public boards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •