Live data from Hacker News

How does `cd` work?

blog.safia.rocks

81–90 of 93 posts

Re: How does `cd` work?

#81

Interesting how the author was looking at "cd" obviously sourced from FreeBSD project (perhaps found on a Mac computer running XNU kernel) and then, to investigate further, she consulted the source code for the Linux kernel to learn about "the" chdir syscall. Would the title "How does 'cd' work in MacOS ?" be better for readers wishing to learn, or worse? I wonder. The blog posts from her friend at jvns.ca have been…

I find it sad that people that are so curious about how things work end up buying Macs, and therefore unwillingly contributing to less openness and "studyability".

Re: How does `cd` work?

#82

Interesting how the author was looking at "cd" obviously sourced from FreeBSD project (perhaps found on a Mac computer running XNU kernel) and then, to investigate further, she consulted the source code for the Linux kernel to learn about "the" chdir syscall. Would the title "How does 'cd' work in MacOS ?" be better for readers wishing to learn, or worse? I wonder. The blog posts from her friend at jvns.ca have been…

I find it sad that people that are so curious about how things work end up buying Macs, and therefore unwillingly contributing to less openness and "studyability".

It doesn't stop them using Linux - it does make it more of a pain though.

Re: How does `cd` work?

#83

Random piece of info about cd: cd.. (no space between "cd" and "..") works on Windows

Starting in Windows 95, you could stack periods to go back further than one directory. "cd....." instead of "cd ..\..\..\.."

Re: How does `cd` work?

#84
post #55

Not one character in the article is actually devoted to how `cd` works...it's just the story of how the author found out where to find the code for it. An article much more worthy of the title "How does `cd` work?" would maybe, you know, actually go through the code that makes `cd` work.

I don't know if the "Update" by Julia Evans was in the article when you posted this. But I'd point to that as a piece of useful insight about how it works at the syscall level.

>SO!! If you had a /usr/bin/cd program that ran chdir, that would be fine, but when you started it it would change its own working directory and exit which is not very helpful. It wouldn’t change the working directory of you (the parent process)

I am a little confused. What is the parent process and why does that matter here?

Re: How does `cd` work?

#85

Earlier quoted context omitted.

I don't know if the "Update" by Julia Evans was in the article when you posted this. But I'd point to that as a piece of useful insight about how it works at the syscall level.

>SO!! If you had a /usr/bin/cd program that ran chdir, that would be fine, but when you started it it would change its own working directory and exit which is not very helpful. It wouldn’t change the working directory of you (the parent process) I am a little confused. What is the parent process and why does that matter here?

The parent is the shell that called the theoretical /user/bin/cd. It means that a cd program would get launched by the shell, change its own working directory and then terminate, which is completely useless.

What you want cd to do is change the caller's (or parent's) working directory, or in this case the shell's working directory.

There are hacks to do it that you can find elsewhere in this thread (process injection and debug APIs to name two) but frankly the shell changing its own working directory via a builtin is a much simpler and more reliable solution that is also cross-platform.

Re: How does `cd` work?

#86

Earlier quoted context omitted.

>SO!! If you had a /usr/bin/cd program that ran chdir, that would be fine, but when you started it it would change its own working directory and exit which is not very helpful. It wouldn’t change the working directory of you (the parent process) I am a little confused. What is the parent process and why does that matter here?

The parent is the shell that called the theoretical /user/bin/cd. It means that a cd program would get launched by the shell, change its own working directory and then terminate, which is completely useless. What you want cd to do is change the caller's (or parent's) working directory, or in this case the shell's working directory. There are hacks to do it that you can find elsewhere in this thread (process injection…

Ah. This makes sense. Thanks. I will look into the hacks.

Re: How does `cd` work?

#87
> I started, as I usually do, by searching for the term “chdir” using the GitHub search bar.

That's not bad, but I'd also suggest checking man pages for syscalls. `man 2 chdir` will give the some documentation on both Mac and Linux OSes and call out the specs that are relevant to the call. (Why `man 2`? That searches section 2 of the man pages which is dedicated to syscalls. How on earth would someone know that? `man man` of course. :-) )

On linux, an interesting thing is that you can inspect a process's current working directory (IE the last thing they chdir()ed to) by looking in /proc//cwd/. It presents itself as a symlink to the actual current directory as stored in the kernel.

Re: How does `cd` work?

#88

Earlier quoted context omitted.

I don't know if the "Update" by Julia Evans was in the article when you posted this. But I'd point to that as a piece of useful insight about how it works at the syscall level.

>SO!! If you had a /usr/bin/cd program that ran chdir, that would be fine, but when you started it it would change its own working directory and exit which is not very helpful. It wouldn’t change the working directory of you (the parent process) I am a little confused. What is the parent process and why does that matter here?

When a process X creates another process Y, what happens is X becomes the parent of Y and Y becomes a child of X. All processes are created this way. They form a tree structure with PID 1 as the root. The very first process is spawned by Linux itself. Processes are isolated by default and thus cannot modify each other's state.

If we have a shell and we use it to start up a separate chdir program, we get two processes: shell (parent) and chdir (child). The kernel isolates them from each other, so they both have completely distinct working directories. The chdir executes the relevant system call, changes its own working directory and exits. The shell is not affected by the system calls performed by its children, so the chdir process effectively does nothing.

By implementing chdir as a built-in function, the shell is able to change the state of its own process.

Re: How does `cd` work?

#89
post #73

Earlier quoted context omitted.

It's been like that since the DOS days, IIRC. You can simulate that behavior on bash with an alias such as this one: alias cd..="cd .."

I believe that you also don't need the space when you specify a path: cd\foo\x The cmd parser in Windows is the most bizarre piece of software in common use. I don't believe there is a single person that actually understands how it works completely.

I think that Rex Conn and I could give it a fair go. (-:

Re: How does `cd` work?

#90
post #68

Okay, but now I want to know how it works on something other than (foo)Nix, because that's not an OS space I care about. What about how it works in CP/M or foo-DOS? I've never used a *Nix computer, but I know that I used 'cd' going back to the time of using a TRS-80 CoCo. I know it's a low-level system call in most on-disk operating systems to change directories. But, for instance, how does it translate physical addr…

What you want is a question and answer WWW site.

* https://superuser.com/a/380231/38062

* https://unix.stackexchange.com/a/251215/5132

... and so on.

Post reply on HN