Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
    
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 Nov 29 '12 at 22:53
add comment

2 Answers

up vote 2 down vote accepted

A home directory is where you 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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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