dirWindows DIR Command Options Explained
Learn every practical DIR command option in Windows, with examples for sorting, filtering, recursion, hidden files, and exporting results.
The DIR command in Windows lists files and directories, and its options let you filter by attributes, sort by size or date, recurse through folders, and output clean listings for scripts. If you need reliable file visibility in Command Prompt, DIR options are the fastest way to control what you see.
DIR matters because file discovery is usually the first step in troubleshooting, automation, and cleanup. System admins use it for audits, developers use it to inspect build artifacts, and support teams use it to confirm whether expected files exist before running delete, move, or backup operations.
This guide covers the most useful Windows DIR command options, practical command combinations, common mistakes, and quick answers to real user questions.
What Is the DIR Command in Windows?
DIR is the native directory listing command in Command Prompt. It returns file names, folder names, timestamps, and sizes, and it supports wildcard filters and switch-based output control.
DIR Syntax and Option Table
DIR [path] [pattern] [/A[:attributes]] [/B] [/O[:sort]] [/S] [/P] [/W] [/T[:timefield]]
| Option | What it does | Common example |
|---|---|---|
/A | Filter by attributes | DIR /A:H |
/B | Bare output, filenames only | DIR /B /S |
/O | Sort order | DIR /O:-S |
/S | Include subdirectories | DIR /S *.log |
/P | Paginate output | DIR /P |
/W | Wide compact view | DIR /W |
/T | Time field for display/sort | DIR /T:C /O:D |
Key DIR Options You Will Actually Use
Hidden Files (/A:H)
Use DIR /A:H to display hidden files that default DIR output omits.
Files Only (/A:-D)
Use DIR /A:-D when you only want files and no folder entries.
Sort by Size (/O:S and /O:-S)
/O:S sorts smallest to largest. /O:-S sorts largest first for quick storage analysis.
Sort by Name (/O:N)
DIR /O:N gives alphabetical output that is easy to compare between runs.
Recursive Search (/S)
DIR /S scans every nested directory. Combine with file patterns like *.txt.
Script-Friendly Output (/B)
DIR /B /S is ideal for piping into findstr, for, or audit logs.
Examples
DIR /A:H
DIR /A:H /O:S
DIR /A:-D /O:-S
DIR C:\Logs /S *.log
DIR /B /S > file-list.txt
DIR /T:C /O:D
For the query "list all hidden files and sort by size," use:
DIR /A:H /O:S
That command is correct because /A:H filters hidden entries and /O:S sorts by size.
Common Use Cases
- Audit hidden files in user profiles.
- Find the largest files before cleanup.
- Generate recursive inventory files for migration.
- Validate deployment output in build folders.
- Compare file changes by timestamp.
- Feed clean listings into automation scripts.
Tips and Best Practices
- Use absolute paths in scripts.
- Start with
DIR /A:-Dto reduce noise. - Combine
/Band/Sfor stable script output. - Use
/O:-Dfor recent file investigations. - Redirect output to text files for incident evidence.
Troubleshooting Common Issues
DIR shows nothing but files exist
Try DIR /A to include hidden and system entries.
Access denied
Run CMD as Administrator or validate ACLs with icacls.
Output scrolls too quickly
Use DIR /P or redirect to file.
Wrong sort results
Verify switch order and use one explicit /O value per run.
Related Commands
/blog/dirfor a full DIR reference./blog/treefor hierarchy visualization./blog/findstrfor filtering output./blog/forfilesfor date-based automation.
Frequently Asked Questions
What does DIR /A do?
DIR /A displays all files, including hidden and system items.
How do I list only directories?
Use DIR /A:D for folder-only output.
How do I list only files?
Use DIR /A:-D.
How do I sort by newest files first?
Use DIR /O:-D.
How do I sort by largest files first?
Use DIR /O:-S.
Which command lists hidden files sorted by size?
Use DIR /A:H /O:S.
Can I export DIR output?
Yes: DIR /B /S > output.txt.
Does DIR work in Windows 11?
Yes, DIR works across modern Windows versions.
Quick Reference Card
| Command | Purpose | Example |
|---|---|---|
DIR /A:H | Hidden files | Security review |
DIR /A:-D | Files only | Cleanup prep |
DIR /O:-S | Largest first | Disk usage scan |
DIR /O:-D | Newest first | Recent activity |
DIR /B /S | Script output | Inventory export |
Try It in the Simulator
Practice these options in the Windows Command Simulator, then browse more commands in the Commands Reference. For related reading, use DIR command guide and DEL command guide.
Summary
Windows DIR command options give you precise control over file listings. For most real tasks, combine attribute filtering (/A), sorting (/O), recursion (/S), and bare output (/B). If your goal is hidden files sorted by size, DIR /A:H /O:S is the exact command to use.