Navigating Directories with cd
The current directory, also called the working directory, is the folder whose files commands can reach by short name.
The Question Every Command Asks
Open a command window on any computer and the first thing you see is a prompt waiting for input. On its own, the prompt tells you very little. Yet every command you type afterward is interpreted against a location on the disk. Before you can use a command-line tool well, you need to know which location it is looking at.
This article answers one question: when you type a command and press return, which folder is the computer working in? The pattern of work is always the same. You type a command, the computer carries it out, any results appear in the window, and then a fresh prompt is shown for the next command. Because the window keeps going this way, the location you are working in carries forward from one command to the next. That carried-over location is the subject of the rest of this article.
What do you think happens?
When you open a brand-new command window, which directory is the current directory at the start?
Reveal answer
Your home directory, your personal folder.
When a command window first opens, the current directory is set to your home directory, the personal folder where that user's own files are stored. Your first location is therefore your own space, not an arbitrary system folder.
The Current Directory as a Lookup Context
The current directory, also called the working directory, is the folder whose files commands can reach by short name.
Consider a command that refers to a file by a short name such as notes.txt. That name alone does not identify one file on the whole machine, because many folders may each hold a file with that name. The command line resolves short names by looking in one designated folder. That folder is the current directory, and whatever short names you type are understood as names inside it.
The current directory is therefore a kind of invisible context attached to your session. It does not appear as a separate file or setting that you open. It is simply the answer to the question every command implicitly asks: where should I look for the things you named? Because of this, the same short name can refer to different files depending on which folder you are standing in when you run the command.
Reading the Prompt and Listing Files
Many command prompts make the current directory easy to spot, because the directory name is often displayed as part of the prompt itself. Reading the prompt is therefore the quickest way to check where you are before you run anything that depends on location. The exact text of a prompt varies between systems, so learn to recognize the folder name inside yours.
To see what the current directory holds, list its files. On Windows, the command is dir. On Linux and macOS, the command is ls. Both list the files in the current directory, and only that directory. Their output changes whenever the current directory changes.
Illustrative session. The first dir lists the home directory. After cd Desktop, the second dir lists only the contents of Desktop.- The first dir shows the files in the starting current directory.
- cd Desktop changes the current directory into Desktop.
- The second dir lists different files because it now lists Desktop, not the starting folder.
Moving Between Folders with cd
The cd command changes the current directory. You follow cd with the name of the directory you want to move into, then press return. After that, short file names are interpreted inside the new directory, and the prompt usually reflects the new name.
The name you give to cd is itself interpreted relative to where you are standing right now. The starting location therefore matters a great deal. The same name can point to a different folder, or to no folder at all, depending on the current directory when you type it.
Moving from home into Desktop
Your current directory is your home directory, and it contains a Desktop directory. What happens when you type cd Desktop and press return?
- 1Identify the starting location
The current directory is the home directory, so cd is interpreted from there.
- 2Resolve the name
Desktop is looked up inside the home directory, where you are standing right now.
- 3Update the current directory
The current directory becomes Desktop. Short file names are now interpreted inside Desktop.
- 4Check the result
The prompt usually reflects the new name, so reading it confirms the move.
You are now in Desktop, and a dir or ls command lists the files in Desktop.
Where a New Window Begins
A new command window does not inherit the location of the window you closed. When a command window first opens, the current directory is set to your home directory. The home directory is the personal folder of the user, the place where that person's own files are stored.
This makes a fresh window a predictable place to begin. If you want files you saved personally, a newly opened window already points at the folder where they usually live. If you want to work somewhere else, you must move the current directory yourself with cd.
Mistakes That Send Commands to the Wrong Folder
Common mistake Assuming a short file name identifies one file anywhere on the machine
Why it failsMany folders may each hold a file with the same name, so the name alone is not unique across the whole machine.
Do this insteadRemember that a short name is resolved against the current directory, so check where you are before you rely on it.
Common mistake Assuming a new command window continues from the location of the last one
Why it failsA new window starts with the home directory as the current directory.
Do this insteadAfter opening a window, read the prompt or run dir or ls, then use cd to move to the folder you need.
Common mistake Typing cd with a folder name while standing in the wrong folder
Why it failsThe name given to cd is interpreted relative to the folder you are standing in, so the same name can point somewhere else.
Do this insteadConfirm your current location from the prompt before you type cd.
Common mistake Expecting dir or ls to show files from a folder you have not moved into
Why it failsdir and ls list only the files in the current directory.
Do this insteadUse cd to move into the folder first, then list its files.
Trace Your Own Location
Open a command window. Before typing anything, predict which folder is the current directory. Then read the prompt, and run dir on Windows or ls on Linux and macOS to list the files. Choose a subfolder name from that list and run cd followed by that name. Check the prompt and run the listing again. Note which files changed and which folder name now appears in the prompt.
Hints
- A new window should start with your home directory as the current directory.
- Only the files of the current directory appear in the listing.
- The name you give to cd is interpreted from the folder you are standing in right now.
Key Takeaways
- The current directory, also called the working directory, is the folder whose files commands can reach by short name.
- dir on Windows and ls on Linux and macOS list the files in the current directory, so their output changes when the current directory changes.
- A new command window starts with your home directory, your personal folder, as the current directory.
- The cd command changes the current directory, and the name you give it is interpreted relative to where you are standing right now.
- Reading the prompt is a quick way to check your location before running any command that depends on it.