Just CLI Things: Changing Directories (cd) in Linux/Unix as Superuser
I always try to do sudo cd /some/restricted/dir
and it doesn’t work and I always forget why.
sudo cd
won’t work because thecd
command is built into the shell. So you are saying become root and then run this command. You become root and then the command after sudo is searched for but there is nocd
command to find.
The method to use is to switch to the user that owns the directory. Permission
700
is meant as “owner can read, write and execute”.
So if root owns the directory
sudo -i
, password and thencd {dir}
is the only correct method. If someone else owns the directory you can still use the 1st method but can also change to that user withsu {username}
and then usecd
as that user.
In summary: cd
isn’t a “command”, so you can’t pass it to sudo
. Instead, use su
to become an appropriate user and then use the shell to change directories.