Windows CMDInteractive Lab
File Managementdir

How to Use DIR Command in Windows CMD

Step-by-step DIR command tutorial for Windows users. Learn syntax, options, examples, and the best combinations for daily command-line tasks.

Rojan Acharya··Updated Apr 17, 2026
Share

If you are learning Windows Command Prompt, DIR is the first command to master. It lists files and folders, and with switches, it can filter hidden files, sort by date or size, and scan subdirectories.

What Is DIR and When Should You Use It?

Use DIR whenever you need to inspect a folder before running commands like del, copy, or move.

Syntax

DIR [path] [pattern] [/A[:attrs]] [/O[:sort]] [/S] [/B] [/P]

Step-by-Step Examples

Step 1: List current folder

DIR

Step 2: List another path

DIR C:\Users\Public

Step 3: Show hidden files

DIR /A:H

Step 4: Sort by newest files

DIR /O:-D

Step 5: Search recursively

DIR /S *.log

Step 6: Export clean list

DIR /B /S > files.txt

Common Use Cases

  • Pre-delete validation.
  • Build artifact checks.
  • Hidden file review.
  • Large file cleanup.
  • Incident evidence collection.

Tips and Best Practices

  • Always confirm path before deletion.
  • Use /B for automation.
  • Use /A in security investigations.

Troubleshooting Common Issues

Nothing appears

Use DIR /A.

Wrong folder

Run CD and check prompt path.

Output too large

Use wildcard filters or redirect output.

Permission issues

Run elevated CMD.

Related Commands

Frequently Asked Questions

Is DIR only for files?

No, it lists files and folders.

How do I show only files?

Use DIR /A:-D.

How do I show only folders?

Use DIR /A:D.

How do I search all subfolders?

Use /S.

How do I sort by size?

Use /O:S or /O:-S.

Can I save output?

Yes, with > redirection.

Does DIR support wildcards?

Yes, like *.txt and data?.csv.

What is the safest beginner workflow?

Run DIR before file-changing commands.

Quick Reference Card

CommandPurpose
DIRBasic list
DIR /A:HHidden files
DIR /O:-DNewest files
DIR /S *.logRecursive log search
DIR /B /SScript output

CTA

Practice safely in the Windows Command Simulator, and then explore every command in Commands Reference.

Summary

Using DIR well means combining path, pattern, attributes, sort order, and recursion. Once you learn these pieces, command-line file discovery becomes fast and reliable.