In unix like systems what is the difference between a
home directory and
working directory ?
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
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.
~ expands to your home directory, and
~user to the home directory of 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
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.