Well, what exactly are you trying to do? Even if you could do 'sudo cd
directory', simply executing that command will result in NOTHING
HAPPENING with respect to your next command. Lets dig in to it at the
end of the message, but for now trust me.
Oh, and the shortest answer, the conclusion/solution, is at the end, so
if your eyes start to glaze over as you read the below, jump to the bottom.
First, see what the permissions on <path> is/are. Try 'ls -ld <path>'.
If you get Permission denied, and path includes '/', then somewhere
along the path (ha) your permissions don't let you do a directory
listing. If you see something like this:
drwxrwxrw- 6 rusty rusty 4096 Dec 19 16:22 <path>
and you are not rusty nor in group rusty, then you can't do a directory
listing of <path>
(That (missing!) 'x' there is the key to your (not) being able to see
the contents. It enables 'search' for directories, which is what you
need to be able to see in to, or cd into, the directory)
If any directory on the <path> does not have x set for you, then you
can't see in to it and get permission denied.
Now, why are both 'sudo cd foo' and 'sudo chdir foo' totally useless?
(even if you COULD do it! Which you can't! See below the below for
news about 'chdir')
Well, when you type 'sudo' and are successful (to perhaps muddy the
water a bit - which is to say you have permission to run a command as
root using sudo), the shell starts a NEW shell, running as root, which
executes your command and then, when that command completes exits back
to the shell which started it. So, let's say your shell's PID is 123.
You type sudo cd foo, the shell starts a NEW shell,say PID 321, which
does the cd and then exits back to the shell that started it, PID 123.
Where the 'current working directory is still wherever you were before
you typed 'cd'!
A lot of typing and process running to do absolutely nothing!
However, 'chdir' is not a command at all. Try it:
=== shell interaction begins
Tue Dec 20 23:19:09 RustyC ~/class $ chdir foo
Command 'chdir' not found, did you mean:
command 'hdir' from deb hfsutils (3.2.6-14)
command 'cdir' from deb cdtool (2.1.8-release-7)
Try: sudo apt install <deb name>
Tue Dec 20 23:19:35 RustyC ~/class $
=== shell interaction ends
(cdtool has a 'cdir', but it 'lists information about the currently
loaded audio compact disc', doing nothing w.r.t. current working dir)
Nope, all you get is 'cd', which as they say, is a shell builtin. Now,
WHY is it a shell builtin? Because your 'current working directory' is
*set* and *maintained* by the shell. Changing directory inside a program
will not affect the 'current working directory' of the shell that
started the program (unless there happens to be a hook in bash you can
call from your program, which is possible, I suppose, but I've never
heard of it. And, no chdir(2) does not, I believe, affect the shell
which started the process which called chdir).
Now, let's keep beating this dead horse, shall we?
Back to the original question, and the meta-question above it, which is
sort of 'how should I debug this sort of thing?'
The first huge hint is 'permission denied'. This means that the user
executing the command does not have (file system) permission to do
whatever it is they tried to do (I'm speaking here of when a command is
doing something w.r.t. a file. Doing 'sudo ' and getting 'Permission
denied' does not have anything (really) to do with file permissions, so
isn't involved here).
So, how do you debug this? The first thing you should do is a long
directory listing of the file that complained (always use 'ls -ld' for
this). If that gets permission denied, then remove the last 'thing' in
the path and re-do the ls -ld. Inspect the permissions (the rwxrwxrwx
things) and the owner/group. If you are the owner, then look at the
first 3 permission bits. If you are NOT the owner, but you are IN the
group, look at the next 3. If you are neither the owner nor IN the
group that owns the file, then look at the last 3. To change in to a
directory you need execute permissions (you can, in fact, 'cd' into a
directory that you do not have read permissions to! But, of course, you
can't see anything in it! Try it. 'mkdir foo;chmod 100 foo;cd foo;ls -l').
So, after much ado, we have finally come to the conclusion that some
directory in 'path' does not have the 'x' permission for you set.
Fix that, you be good.
Rusty (yes, I do sometimes teach a class on Linux, why do you ask? ;-)
On 12/20/22 18:27, Keith Smith via PLUG-discuss wrote:
>
>
> Hi,
>
> Not sure what is going on here:
>
> cd <path> results in message "-bash: cd: <path> : Permission denied".
>
> Then I add sudo : sudo cd <path> results in message : "sudo: cd:
> command not found".
>
> Any help is much appreciated!!
>
> Keith
> ---------------------------------------------------
> PLUG-discuss mailing list: PLUG-discuss@lists.phxlinux.org
> To subscribe, unsubscribe, or to change your mail settings:
> https://lists.phxlinux.org/mailman/listinfo/plug-discuss
---------------------------------------------------
PLUG-discuss mailing list:
PLUG-discuss@lists.phxlinux.org
To subscribe, unsubscribe, or to change your mail settings:
https://lists.phxlinux.org/mailman/listinfo/plug-discuss