Tao
Tao

Contents

Solution for "sudo cd" Command Not Working

Contents

When we run sudo cd /var/lib/xxx, it prompts “command not found”. Currently, there are two solutions:

  1. sudo -s
  2. sudo -D

The sudo -s option can be used to run a shell with privileges. Here’s the correct usage:

shell

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.

The -D option of sudo is used to run a command in a specified directory. Here’s the correct usage:

shell

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:

shell

sudo -D /var/www/html ls

This will run the ls command in the /var/www/html directory with root user privileges.