Comprehensive Guide to Using the Command Prompt for File and Directory
The Command Prompt (CMD) in Windows is a basic tool you'll need to compile and run Java programs manually. In this lesson we cover the core commands for navigating the file system, the difference between absolute and relative paths, and working with batch/shell files.
Absolute and relative paths
A file path can be absolute or relative. An absolute path points to a specific location in the file system regardless of the current directory and always starts from the root of the drive (for example, C:\Program Files\Java). A relative path is resolved from the current working directory (for example, src\Main.java).
Navigating directories
The main commands for moving around the file system:
| Command | What it does | Example |
|---|---|---|
cd folder | Move into a subfolder of the current directory | cd src |
cd folder\subfolder | Move several levels deep | cd src\com\example |
cd.. | Go up to the parent directory | cd.. |
cd \folder | Move from the root of the drive | cd \Users |
D: | Switch the current drive | D: |
cd /d D:\path | Switch drive and directory in one command | cd /d D:\projects |
Note: the plain cd command does not change the current drive. To move into a folder on another drive in one step, use the /d switch: cd /d D:\projects.
Listing contents and getting help
| Command | What it does |
|---|---|
dir | List the contents of the current directory |
cls | Clear the Command Prompt screen |
help | Show the list of available commands |
help command | Help for a specific command (e.g. help cd) |
Useful keyboard shortcuts
A few tricks that speed up work in the Command Prompt:
- Win + R, then
cmd— quickly open the Command Prompt. - ↑ / ↓ — scroll through the history of entered commands.
- Tab — autocomplete file and folder names (Shift + Tab — in reverse). Type the first letters and press Tab to cycle through matching names: for example, "s" + Tab shows
system,system32, and so on.
Batch (.bat) and shell (.sh) files
You can create files containing several commands: with the .bat extension for Windows and .sh for Linux. They can be run both from the Command Prompt and by double-clicking in the file explorer.
When you run a .bat file from the explorer, the cmd window closes immediately after execution, leaving no time to see the result. To keep the window open, add the pause command at the end of the file — it halts execution until you press a key.
Frequently asked questions
How do I switch drives in the Command Prompt?
Type the drive letter followed by a colon, e.g. D:. To jump straight into a folder on another drive, use cd /d D:\path — without the /d switch, cd won't change the drive.
How do I go up one level in the Command Prompt?
Use cd.. — it moves to the parent directory. You can go up several levels at once: cd..\..
What's the difference between an absolute and a relative path?
An absolute path starts from the root of the drive and doesn't depend on the current directory (C:\src\Main.java). A relative path is resolved from the current directory (src\Main.java).
Why is the pause command used in a .bat file?
So the Command Prompt window doesn't close right after the script runs, letting you see the result. pause waits for any key to be pressed.
How do I open the Command Prompt quickly?
Press Win + R, type cmd, and press Enter.
Comments