20

Occasionally I want to cd into a directory where my user does not have permission, so I resort to sudo.

The obvious command sudo cd somedir doesn't work:

$ sudo mkdir test
$ sudo chmod go-rxw test
$ ls -l
drwx------ 2 root     root  [...snip...] test
$ cd test
-bash: cd: test: Permission denied
$ sudo cd test
sudo: cd: command not found

Using sudo su works:

$ sudo su
# cd test

Is it possible to make this into a one-liner? (Not a big deal, just idle curiosity :)

The variations I tried didn't work:

$ sudo "cd test"
sudo: cd: command not found
$ sudo -i cd test
-bash: line 0: cd: test: No such file or directory
$ sudo -s cd test

The last one doesn't give an error, but it cd's within a new shell that exits by the end of the line, so it doesn't actually take me anywhere.

Can someone enlighten me as to why this happens? Why is sudo cd not found, when for example sudo ls ... works fine?

2
  • BTW, sudo -i is preferred over sudo su, otherwise the user's env vars will be carried over.
    – Sparhawk
    Apr 10, 2014 at 4:54
  • @Sparhawk The user may not want an environment that simulates an initial login shell (sudo -i). But then it should still be unnecessary to use sudo su, as there is sudo -s. Running a root shell but not simulating an initial login shell is a common enough operation, and is considered reasonable enough, that sudo supports it officially with the -s option. Note also that running sudo -i followed by cd dir, when dir is a relative path that was originally correct, usually fails, as unlike sudo -s, sudo -i changes the current directory to the target user's home directory. Dec 11, 2017 at 3:12

2 Answers 2

20

Theoretically, the problem is that if you don't have execute rights to a directory, you shouldn't be able to read the contents of the directory. Now suppose you could do what you wanted:

user@desktop:/$ sudo cd restricted-dir
user@desktop:/restricted-dir$ ls
file1 file2

As you can see, you entered the directory using sudo privileges, then, when sudo returns, you become a user again, and you are in a directory where you normally shouldn't be.

Technically, the problem is the following.

sudo cd restricted-dir

cd is a shell built-in, not a command.

sudo cd -i restricted-dir

You are probably in /root, but it would have the same problem as with the next one.

sudo cd -s restricted-dir

You open a new root shell, cd into the directory, then exit the root shell, and return to where you began.

All in all, the only solution is to open a root shell and keep it open while you are in that directory.

2
  • Technically, cd is a "shell builtin command" (according to man bash).
    – Sparhawk
    Apr 10, 2014 at 4:51
  • If this was the case @petersohn, then why would shell promt cd: command not found, It shows no permission issue. And I don't think shell builtin is the particular reason either, because sudo echo works fine, while echo is also shell builtin Jul 28, 2016 at 4:30
5
sudo sh -c "cd restricted-dir; some-other-command"

i.e.

sudo sh -c "cd /root/restricted-dir; ls -l"

the key piece is "sh -c" which I use quite often to run a chain of commands in the same shell context/process.

1
  • 1
    Using && instead of ; will prevent the command from being executed if the change directory operation fails. In other words: sudo sh -c "cd restricted-dir && some-other-command" Aug 21, 2017 at 8:51

Not the answer you're looking for? Browse other questions tagged .