Quick answer: Use the dir command in Command Prompt to list files in a Windows directory.
Many believe typing ls in Windows works like it does in Linux or macOS, but Windows uses dir as its native file listing command. The dir command displays files and folders in the current directory, including names, sizes, and timestamps.
dir Lists Files and Subdirectories
Run dir in Command Prompt to see all visible files and folders in the current location. This includes system files unless hidden, and outputs each item’s name, size in bytes, and last modified date and time.
By default, dir includes a summary line showing total files and directories counted at the bottom. To limit output to just filenames, use dir /b for bare format, which omits headers, summaries, and metadata.
If you're navigating via Command Prompt, use cd to change into the target directory first. For example, cd \Users\John\Documents followed by dir lists all items in that folder. You can also run dir with a path: dir C:\Logs lists files in C:\Logs without changing directory.
ls Does Not Work by Default in Windows
Unlike Unix-based systems, Windows Command Prompt does not recognize ls as a built-in command. Typing ls returns 'is not recognized as an internal or external command' unless a third-party tool like Git Bash, WSL (Windows Subsystem for Linux), or a custom alias has been installed.
PowerShell, Microsoft’s modern shell, does support ls as an alias for Get-ChildItem. But in traditional CMD (Command Prompt), dir is the only native command for listing directory contents. Users switching from macOS or Linux often expect ls to work in CMD and are surprised when it fails.
If you frequently use ls, consider switching to PowerShell or installing WSL. Otherwise, adopt dir as the standard for CMD sessions.
dir Command Variants for Filtering Output
The dir command supports switches to filter and format results. These are case-insensitive and can be combined. Common switches include:
| Switch | Effect | Best Used When |
|---|---|---|
/b | Bare format: filenames only, no headers or summary | Redirecting output to a file or script |
/a | Shows files by attribute (e.g., hidden, system) | Finding hidden files with dir /a:h |
/s | Lists files in current directory and all subfolders | Searching for a file across nested folders |
/w | Wide format: multiple files per line | Quick visual scan in full-screen CMD |
/o | Sorts output (e.g., /o:n by name, /o:-s by size descending) | Organizing large directory listings |
For example, dir /b /s *.txt outputs full paths of all text files in the current folder and subfolders, one per line. This is useful for feeding into scripts or search tools.
Use dir /a to see all files, including system and hidden ones. To show only hidden files, use dir /a:h. To exclude directories from the count, use dir /a:-d.
Redirect Output to a File
To save a file list for later use, redirect dir output with the > operator. For example, dir /b > filelist.txt writes only filenames to filelist.txt in the current directory.
If the file already exists, > overwrites it. To append instead, use >>. For example, dir *.log /s >> logfiles.txt adds all log file paths to an existing list.
Use dir /b /s > allfiles.txt to create a complete inventory of files under a folder tree. This file can be opened in Notepad, Excel, or searched with findstr.
Ensure you have write permissions in the target directory. If running CMD without admin rights, avoid system folders like C:\Windows unless you only list files without redirecting.
dir vs PowerShell’s Get-ChildItem
PowerShell’s Get-ChildItem (aliased as gci or ls) is more powerful than CMD’s dir. It returns objects with properties like creation time, owner, and permissions, enabling complex filtering and scripting.
For example, Get-ChildItem -Path C:\Temp -Filter *.tmp -Recurse | Where-Object {$_.Length -gt 100KB} finds temporary files over 100KB in size across subfolders. CMD cannot perform size-based filtering directly.
However, CMD’s dir is faster for simple listings and available in minimal environments like recovery mode. Use CMD for quick checks, PowerShell for automation or detailed queries.
If you work across both, remember that dir in PowerShell is not the same as in CMD—it’s an alias for Get-ChildItem, so behavior differs slightly (e.g., color output, object handling).
File Listing for Scripts and Automation
When using dir in batch scripts, always specify formatting and path explicitly. Relying on default output can break scripts if headers change or errors occur.
Use dir /b /a:-d "%%A" in a for loop to iterate over files in a variable %%A. The /a:-d ensures only files (not directories) are processed.
Always test scripts in a safe directory first. Use echo to preview commands before executing file operations. Example:
for /f "delims=" %i in ('dir /b *.txt') do echo Processing %i
This previews each filename before acting on it.
For robust automation, prefer PowerShell. CMD batch files lack error handling and data types, making them fragile for complex tasks.
Common Errors and Fixes
'File Not Found' appears when the path is incorrect or no files match the filter. Verify the directory exists and use quotes for paths with spaces: dir "C:\My Documents".
'Access is Denied' means insufficient permissions. Run CMD as Administrator only if necessary, and avoid system folders unless required.
'The system cannot find the path specified' indicates a typo or drive not ready (e.g., disconnected USB). Check spelling and drive availability.
If dir returns nothing with /b, confirm files exist and are not filtered out by attributes or extension.
Windows File List Command in Practice
Here are real-world uses:
- Inventory of a folder:
dir /b > inventory.txt - Find all JPGs recursively:
dir /s /b *.jpg - List files sorted by size descending:
dir /o:-s - Show hidden files:
dir /a:h - List only directories:
dir /a:d
Use these patterns as templates. Adjust paths and switches based on need.
dir Command for Large Directories and Kids
When listing directories with thousands of files, dir may pause or scroll quickly. Use more to paginate: dir | more shows one screen at a time. Press Enter to advance.
For children learning CMD, start with dir in their Documents folder. Combine with cd and mkdir to teach navigation and organization. Avoid system paths to prevent confusion or accidents.
Use dir /w for a cleaner, multi-column view that’s easier to read on one screen. Pair with cls to clear the screen before each command for focus.