2

In unix like systems what is the difference between a home directory and working directory ?

  • Your home directory is where you go to rest between work sessions. Your working directory is where you are right now. Because unix systems are multitasking, each process has its own working directory; processes can change directories, too. – Gilles 'SO- stop being evil' Nov 29 '12 at 22:53
2

A home directory is where most terminal emulators start when you open a shell. The working directory is where you are right now. You can usually go directly to the home directory with the command cd and you can find out what the working directory is with pwd.

  • The "A home directory is where you start when you open a shell" part is wrong in general. It's only true for shells that are started from a process that happens to have the working directory set to the home directory (as is often the case for shells started by terminal emulators started by your window manager, or login shells, but doesn't have to). – Stéphane Chazelas Sep 28 '17 at 10:22
  • @StéphaneChazelas: Thank you, I reworded the answer slightly. My intention was clarity at the expense of pedantry for users with the OP's perceived experience. – dotancohen Sep 28 '17 at 10:50
6

The home directory is a directory associated with a user name in the system's user account database. It's the 6th field in the entry returned by getent passwd some-user.

When you log in, the HOME environment variable is initialized from that value and applications use that variable or query the user database to know what your home directory is.

That directory is usually owned and writable by the corresponding user, and it's typically where applications store the user settings and where the user stores his files.

In shells, ~ expands to your home directory, and ~user to the home directory of user user.

The kernel has no notion of what a user name or home directory is. The home directory is entirely a user-space concept.

On the other hand, the working directory is an attribute of each process. It can be changed with the chdir(2) system call (and the cd command in a shell) and queried using getcwd() or pwd in a shell. The current working directory is the base for findinh a file using a relative path. A relative path (as in "foo/bar.txt") is relative to the current working directory of a process.

The working directory is inherited upon a fork and preserved upon execution of a command. For instance, ls lists the content of its working directory which is the same as the working directory of the shell that called it.

When a user logs in, the working directory of the first process that is run under his name is set to his home directory, and as a result, unless anything changes it, every process started under that login session will have the home directory as the current working directory.

shells don't start in the home directory, they start wherever the current directory was when they were executed, though shells started by a terminal emulator itself started by a window manager itself started by the login manager will likely start in the home directory since there's no reason why the window manager or terminal emulator would change their working directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.