Concepts / Listing Files on the Command Line

Listing Files on the Command Line

The current directory, also called the working directory, is the folder whose files commands can reach by short name.

  • Programming

Why a Command Needs a Location

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. Every command you type afterward, however, 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 repeats for every command. You type a command, the computer carries it out, any results appear in the window, and 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.

The Current Directory as Hidden Context

Definition

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 an 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?

Root of diskHome directorycurrent directory atstartupDesktopa folder inside homenotes.txtreachable by short namefrom Desktopcontainscontainscontains
Where does the current directory sit inside the folder hierarchy, and which folders and files can be reached by short name from there?

The diagram shows one possible arrangement, which is a generated example and not a fixed layout. The idea it illustrates is the one from the source: the current directory is a single position in the folder tree, and short names are looked up from that position. Folders above it and beside it are not reachable by short name until you move.

Reading the Prompt to Find Your Location

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 therefore the quickest way to check where you are before you run anything that depends on location. Get into the habit of glancing at the prompt before every command that works with files.

Listing Files with dir and ls

Once you know where you are, you can see what is there. On Windows the command is dir. On Linux and macOS the command is ls. Each one lists the files in the current directory. Because the listing is taken from the current directory, the list changes whenever the current directory changes.

Notes on the code
  • Windows: lists the files in the current directory
Notes on the code
  • Linux and macOS: lists the files in the current directory

Predicting Where a New Window Starts

A fresh command window does not start in an arbitrary system folder. When it first opens, the current directory is set to your home directory, which is your personal folder, the place where your own files are stored. Work through the prediction below before reading on.

What do you think happens?

You close every command window and then open a brand new one. Which directory is current when the prompt first appears?

  • The root of the disk

  • The home directory, your personal folder

  • The directory you were in when you closed the last window

Reveal answer
Answer:

The home directory, your personal folder.

A new command window starts with the home directory as the current directory. The location you left in another window does not carry over into a new window.

This makes a fresh window a predictable starting point. If you want files you saved personally, the new window already points at the folder where they usually live. If you want to work somewhere else, you must move the current directory yourself.

Changing 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.

Notes on the code
  • moves the current directory into Desktop, which must be inside the current directory
Worked example

Moving from home into Desktop and listing the result

Assume your home directory is the current directory and it contains a Desktop directory. Which files can you reach by short name before and after running cd Desktop?

  1. 1
    Check the starting point

    The window opens in the home directory, so short names are looked up in home. Read the prompt to confirm this.

  2. 2
    Run the listing

    Running dir or ls at this point lists the files in home, including the Desktop folder itself.

  3. 3
    Change directory

    Run cd Desktop. The name Desktop is interpreted relative to where you are standing, which is home, so you move into Desktop.

  4. 4
    Check the new location

    The prompt now usually shows Desktop. Short names are now looked up inside Desktop, so a file such as notes.txt that sits in Desktop is reachable by its short name.

  5. 5
    List again

    Running dir or ls now lists the contents of Desktop, not the contents of home. The same command produces a different list because the current directory changed.

Result

The same listing command shows different files before and after cd, because the current directory is different.

Home directorycurrent before cdDesktopcurrent after cd Desktopcd Desktop
After running cd into a subfolder, which directory is now current, and what changes about which files can be listed by short name?

The transition is the whole point of cd. The directory you were standing in is replaced by the one you moved into, and every short name you type afterward is resolved against the new location.

Short Names Versus Full Paths

A short name such as notes.txt is resolved against the current directory. This is why the same short name can refer to different files depending on where you are. If you need to refer to a file that is not in the current directory, you can name it in a way that does not depend on your starting position, which is what a full path does. A full path describes the file's location from the top of the disk rather than relative to where you are standing.

notes.txtshort nameDesktopcurrent directoryFull pathstarts from the top of thedisknotes.txt in Desktopsame file, named completelylooked up insidenames exactly one filecontains
How does a command resolve a short file name against the current directory, compared with a full path that starts from the root?

The diagram uses a generated arrangement to show the contrast. The short name depends on the current directory and is correct only when that directory holds the file. The full path names the file without reference to your current position, so it does not change when you run cd.

Mistakes Beginners Make with Location

  • Common mistake

    Assuming the list shows the whole disk

    What it looks like

    Running dir and expecting to see every file on the computer

    Why it fails

    dir and ls list the files in the current directory only.

    Do this instead

    Check which directory is current, then list. Use cd to move if the file you need lives elsewhere.

  • Common mistake

    Assuming a new window keeps your last location

    What it looks like

    Leaving a window inside a project folder, opening a new window, and expecting to be in the project folder

    Why it fails

    A new command window starts with the home directory as the current directory.

    Do this instead

    After opening a window, read the prompt and run cd to reach the folder you need.

  • Common mistake

    Typing a short name from the wrong folder

    What it looks like

    Running a command on notes.txt while the current directory is home, when notes.txt is inside Desktop

    Why it fails

    Short names are interpreted inside the current directory, so the file is not found from home.

    Do this instead

    Run cd Desktop first, or use a full path to name the file exactly.

  • Common mistake

    Ignoring the prompt before running a file command

Practice Finding Your Way Around

EASY

Starting from a freshly opened command window, describe the steps that would let you list the files in a Desktop folder inside your home directory. State which directory is current at each step, and say which list you would see before and after moving.

Hints
  • Start by predicting which directory a new window uses.
  • Remember that cd Desktop is interpreted relative to where you are standing.
  • Use dir on Windows or ls on Linux and macOS for each listing.

Key Takeaways

  • The current directory, also called the working directory, is the folder whose files commands can reach by short name, and every short name is resolved against it.
  • Read the prompt to check your location, since it often displays the current directory's name.
  • dir on Windows and ls on Linux and macOS list the files in the current directory, so the listing changes when the directory changes.
  • A new command window starts with your home directory, your personal folder, as the current directory.
  • The cd command moves the current directory, after which short names are interpreted inside the new directory.