Introduction to the Command Line Environment
The current directory, also called the working directory, is the folder whose files commands can reach by short name.
A Prompt Waiting for Input
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 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 so you can type 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.
Tracing the Location Through a Session
Think of a command window as a session with a single running state: the location you are standing in. Each command runs against that location, and the location does not reset between commands. A command that looks for a file runs inside the place you are currently standing. A command that moves you somewhere changes that place, and every later command inherits the change.
Keep this picture in mind as we go. The state is the location. The commands are the operations. The prompt, which we will return to shortly, is the window's way of telling you the state before you type the next operation.
The Current Directory and Short Names
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. The 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. Whatever short names you type are understood as names inside that folder.
The current directory is therefore a kind of invisible context attached to your session. It does not appear as a separate file or setting you open. It is simply the answer to the question every command implicitly asks: where should I look for the things you named?
A command that uses a short name is really asking a question about one folder. Change the folder and the same short name can refer to a different file, or to no file at all.
Reading Your Location from the Prompt
Many command prompts make the current directory easier to spot because the directory name is often displayed as part of the prompt itself. Reading the prompt is the quickest way to check where you are before running anything that depends on location. The source does not fix one exact prompt format, so treat the prompt as a hint and confirm it by listing files when in doubt.
To see what the current directory holds, list its files. On Windows you use dir. On Linux and macOS you use ls. Each command lists the files in the current directory, so the output tells you exactly which short names will resolve right now.
- Windows: lists the files in the current directory
- Linux and macOS: lists the files in the current directory
Suppose you run the listing command and see notes.txt among the results. That tells you that a command typing notes.txt with no path will find that file. If notes.txt is not in the list, the same command will not find it from this location, even if a file with that name exists in some other folder on the machine.
Predicting Where a New Window Starts
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. So the first location you work in is not an arbitrary system folder but your own space.
What do you think happens?
You close a command window that was working inside a project folder, then open a brand new command window. Which folder is the current directory in the new window?
Reveal answer
The home directory, your personal folder
A new command window starts with the home directory as the current directory. The previous window's location does not carry over into a fresh window, so you must move to the project folder yourself if you want to work there.
This is why a fresh window is 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.
Moving the Current Directory 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.
Following a Session That Uses cd
A new window opens. The current directory is the home directory, and the home directory contains a Desktop folder. You type cd Desktop, press return, and then list the files. Which folder's files do you see?
- 1Step 1: Identify the starting location
A new window starts in the home directory, so the current directory is home before any command runs.
- 2Step 2: Interpret the name Desktop
The name Desktop is interpreted relative to where you are standing right now. Since you are in home, Desktop means the Desktop folder inside home.
- 3Step 3: Apply the move
cd changes the current directory to Desktop. The prompt usually reflects that new name.
- 4Step 4: Run the listing
dir on Windows or ls on Linux and macOS now lists the files inside Desktop, because that is the current directory.
The listing shows the files inside Desktop. The same listing command run before the cd would have shown the files in home.
Folder Hierarchy and Relative Moves
Folders sit inside other folders, forming a tree. The current directory is one node in that tree. When you type a name in cd, that name is read relative to the node you are standing on, not relative to the top of the disk. This is why the same name can point to different folders depending on where you started.
In the diagram, Home is the current directory at the start of a new window. From there, Desktop is a child folder, and the short name Desktop resolves to it. The file notes.txt sits one level further down. A short name for notes.txt only works from inside Desktop, because that is the folder whose files commands reach by short name.
Mistakes Beginners Make with Location
Common mistake Assuming a command finds a file anywhere on the disk when given only a short name
What it looks likeTyping notes.txt when notes.txt is in Desktop, but the current directory is home.
Why it failsShort names are resolved inside the current directory, not across the whole machine.
Do this insteadList the files first, or cd into the folder that holds the file, then use the short name.
Common mistake Expecting a new window to remember the location of the previous window
What it looks likeClosing a window that was inside a project folder and assuming the next window opens there.
Why it failsA new command window starts with the home directory as the current directory.
Do this insteadUse cd to move to the folder you need after the window opens.
Common mistake Ignoring the prompt before running a location-dependent command
What it looks likeRunning a command without checking which folder the prompt shows.
Why it failsThe command is interpreted against the current directory, so an unnoticed wrong location changes the result.
Do this insteadRead the prompt and, if needed, list the files to confirm your location before running the command.
Common mistake Forgetting that the name after cd is read relative to where you are standing
Practice Tracing Your Location
A new command window opens. Without running anything, state which folder is the current directory. Then say which folder the current directory becomes after you run cd Desktop, and what a listing command would show next. Finally, explain why a short name such as notes.txt might fail in the new location even though the file exists somewhere on the machine.
Hints
- Check where a new command window starts before you consider any command.
- The name after cd is read relative to the current directory at the moment you type it.
- Short names are resolved only inside the current directory.
Key Takeaways
- The current directory, also called the working directory, is the folder whose files commands can reach by short name.
- A new command window starts with your home directory, your personal folder, as the current directory.
- Use dir on Windows or ls on Linux and macOS to list the files in the current directory, and read the prompt to check your location.
- The cd command changes the current directory, and the name after cd is interpreted relative to where you are standing.