Current Working Directory (in the Terminal)

Share

What is the "Current Working Directory" in the Terminal?

The current working directory (abbreviated CWD), also called present working directory (abbreviated PWD), refers to which folder (directory) you're currently working in while using the terminal.

More technically, the current working directory is an environment variable containing the filepath of a directory and the standard way for programs to know from which folder the user is executing the program from. This isn't limited to terminal commands. On Windows, for example, while creating a shortcut you can select what folder to run an application from (the "Start in" field). It's the same concept.

How the Current Working Directory Affects the Behavior of Programs?

Depending on the value of current working directory, the behavior of a program may change.

For example, the ls command on Linux lists the files of the current working directory.

The current working directory is used to resolve relative filepaths passed as arguments in the command-line, e.g. cat ./file.txt refers to the file file.txt in the current working directory. The command mkdir folder will create a directory called "folder" in the current working directory.

The command pwd prints the current working directory, so it really depends on its value.

Most basic terminal commands depend on the current working directory. Consequently, normally you'll have a shell prompt that displays it. On Linux, most users often work on files relative to their home directory, consequently an abbreviation of its filepath is used for convenience: the tilde (~), e.g. ~/Downloads.

Some applications and games on Windows may load files located inside the same folder the .exe is contained, and these programs may be programmed to load these files based on the value of the current working directory.

For example, if you have a C:\game\game.exe, it may load C:\game\menu.png by concatenating the current working directory with menu.png, so if the current working directory is C:\game, it will find the menu.png file, but the "Start in" field in its shortcut is set to a different folder that doesn't have a menu.png, the game won't find the file, and it will probably crash.

Written by Noel Santos.

About the Author

I'm a self-taught Brazilian programmer graduated in IT from a FATEC. In a world of increasingly complex and essential computers, I decided to use my technical expertise in hardware, desktop applications, and web technologies to create an informative resource to make PC's easier to understand.

View Comments