Solution for "sudo cd" Command Not Working
When we run
sudo cd /var/lib/xxx
, it prompts “command not found”. Currently, there are two solutions:
- sudo -s
- sudo -D
sudo -s
The sudo -s
option can be used to run a shell with privileges. Here’s the correct usage:
sudo -s
cd /path/to/directory
Using sudo -s
command initiates a shell session with privileges. Once inside the privileged shell, you can use the cd
command to switch to the desired directory.
Replace /path/to/directory
with the actual path of the directory you want to navigate to.
sudo -D
The -D
option of sudo
is used to run a command in a specified directory. Here’s the correct usage:
sudo -D /path/to/directory command
In this command, replace /path/to/directory
with the actual path of the directory where you want to run the command, and replace command
with the command you want to execute in that directory.
For example, if you want to run the ls
command with root user privileges in the /var/www/html
directory, you can use:
sudo -D /var/www/html ls
This will run the ls
command in the /var/www/html
directory with root user privileges.