Why no "sudo cd" - command not found

Typing as normal user on cli “sudo cd” or “sudo /home/user” returns a “sudo: cd: command not found”.
“cd …” or “cd /” with no sudo is possible. Jumping through the whole tree.
Why no command “cd” with sudo?
With “ls” it is possible.

It is because “cd” is only a shell builtin for Clear Linux. You can execute type -a cd to see that and other binaries and how they are treated. If it were in “/usr/bin” it would probably work. You can read more at Why Won’t sudo cd work

For any Linux distro. There is no /usr/bin/cd. It does not exist anywhere.

The whole concept of working directory is a shell-internal thing. Therefore, outside the shell, and in programs like sudo, it has no meaning, since sudo only inherits it from the shell, but only for as long as it runs, which is until the command finishes.

1 Like

I have a Fedora install on a machine. It has /usr/bin/cd and sudo cd does work, but I don’t think this is the common case by any means.

How… could it even do anything? As soon as the sudo cd command finishes, the current working directory is reset to what it was before the command. It literally would accomplish nothing.

Yup. It doesn’t do anything useful on Fedora either. It is a confusing command because new users might think they could sudo cd into a directory only root has permission on. If you chmod 700 a directory and try to cd into it you can’t unless you are root. On that Fedora system if you sudo cd /rootowneddir then it doesn’t give an error about cd missing and just returns you to the prompt. On Clear Linux it says cd: command not found

I have a secondary install Fedora Silverblue ostree. sudo cd does nothing. It doesn’t throw an error and doesn’t change directory. This way CL is more honest. CL tells us it doesn’t work.

Interesting, but not particularly useful :smiley: :

1 Like

cd cannot be an external command anywhere, even in Windows. If you fork your process, set the child directory, and then exit, then what’s the point? Maybe the only way would be to create a child every single time you do cd, effictively starting a new shell session… But I don’t think it would be a good idea

the test command exists for testing whether e.g. a directly exists and has the right permissions. Scripts depending on /usr/bin/cd are … mostly just a shortcut for lazy developers :slight_smile: