Frequently Asked Question

What are ls, cd and pwd doing under the hood?

Each of these wraps a small set of system calls. ls opens the named directory with openat, calls getdents64 to read out its entries, and then stats each one to fetch type, permissions, owner, size, and timestamps for the long format. pwd asks the kernel via getcwd(3) what the current working directory is, the kernel stores it as part of the process's task structure, so this is essentially free.

cd is the odd one out: it is not a separate program at all but a shell builtin. It has to be, because changing the working directory of a child process would not affect the parent shell. Internally bash calls chdir(2), which asks the kernel to update the directory pointer in its own task structure. From then on every relative path the shell hands to other programs is resolved against the new directory. That is why cd only ever works inside the shell that ran it.

Video

Further reading and video