Windows dir Command: Syntax, Switches, and Practical Examples

The dir command lists files and directories in a given path. It is available on every Windows system, works without additional tools or modules, and is reliable in scripts, remote sessions, and recovery environments. For system administrators, it is often the first command used when inspecting an unfamiliar system or building a file inventory.

This article covers the full syntax, all available switches grouped by purpose, and practical patterns for real administrative tasks.

Syntax

dir [<drive>:][<path>][<filename>] [...] [/p] [/q] [/w] [/d] [/a[[:]<attributes>]] [/o[[:]<sortorder>]] [/t[[:]<timefield>]] [/s] [/b] [/l] [/n] [/x] [/c] [/4] [/r]

Switches Reference

Output Format

SwitchWhat it doesWhen to use
/BBare output — filenames only, no headers or totalsAlways use when redirecting output to a file or pipe
/WWide format — filenames in columns, no metadataQuick visual scan of a large directory
/DSame as /W but sorted by columnAlternative to /W for column-first reading
/NLong list format with filenames on the far rightWhen short 8.3 names are present alongside long names
/XShows both short 8.3 name and long nameLegacy apps or scripts that require 8.3 paths
/LOutputs names in lowercasePiping into case-sensitive tools or scripts
/-CRemoves thousand separator from file sizesScript parsing — separator breaks numeric processing
/4Shows years in four-digit formatAudit logs and exports where two-digit years are ambiguous
/PPauses after each screenfulInteractive browsing only — do not use in scripts

Filtering by Attribute

Use /A with a modifier to filter by file attribute. Without a modifier, shows all files including hidden and system. Modifiers can be combined: /A:HS matches files that are both hidden and system. Prefix with - to negate: /A:-D shows everything except directories.

ModifierShows
/A:HHidden files
/A:SSystem files
/A:DDirectories only
/A:RRead-only files
/A:AFiles with archive bit set
/A:LReparse points — symlinks and junctions
/A:IFiles excluded from content indexing
/A:-DAll files, excluding directories

Recursion

/S includes all subdirectories. By itself it produces verbose output. Combined with /B it produces a flat list of full paths — the most useful pattern for scripting and auditing.

dir /S /B *.log

Sorting

Use /O with a modifier to sort output. Prefix with - to reverse the order. Modifiers can be combined: /O:GN lists directories first, then files alphabetically.

ModifierSort byExample
/O:NName (A–Z)dir /O:N
/O:EExtension (A–Z)dir /O:E
/O:SSize (smallest first)dir /O:-S for largest first
/O:DDate/time (oldest first)dir /O:-D for newest first
/O:GDirectories listed firstdir /O:GN

Timestamps

Use /T to control which timestamp is displayed and used for date sorting.

ModifierTimestampExample
/T:CCreation timedir /T:C /O:D
/T:ALast accesseddir /T:A /O:-D
/T:WLast written (default)dir /T:W /O:-D

Security and Advanced

/Q — Shows the owner of each file. Requires read permission on the file’s security descriptor. May not resolve ownership correctly across domain boundaries or on shares with restricted ACLs.

/R — Shows alternate data streams (ADS) attached to files. Relevant for security audits — malware sometimes uses ADS to hide payloads.

Admin Tasks with Examples

Find all log files recursively, sorted by size

Useful before a cleanup operation to understand what takes the most space.

dir C:\inetpub\logs /S /B *.log /O:-S

C:\inetpub\logs\LogFiles\W3SVC1\u_ex260101.log
C:\inetpub\logs\LogFiles\W3SVC1\u_ex260315.log
C:\inetpub\logs\LogFiles\W3SVC2\u_ex260310.log

Export a full file index to a text file

Standard input for batch scripts, PowerShell pipelines, or audit documentation.

dir C:\Backup /S /B > filelist.txt

C:\Backup\configs\server01.xml
C:\Backup\configs\server02.xml
C:\Backup\scripts\deploy.ps1
C:\Backup\logs\backup_2026-03-01.log

List hidden files in a user profile

Used during malware investigations, GPO audits, or profile cleanup.

dir C:\Users\jsmith /A:H /S /B

C:\Users\jsmith\AppData\Local\Temp\~DF3A12.tmp
C:\Users\jsmith\NTUSER.DAT
C:\Users\jsmith\ntuser.dat.LOG1
C:\Users\jsmith\ntuser.ini

Find all symlinks and junctions under a path

Useful when diagnosing unexpected path redirections or auditing storage layout.

dir C:\inetpub /A:L /S

 Directory of C:\inetpub

03/15/2026  09:14 AM    <JUNCTION>     logs [C:\Logs\IIS]
03/10/2026  02:45 PM    <SYMLINKD>     wwwroot_old [C:\OldSite\wwwroot]
               0 File(s)              0 bytes
               2 Dir(s)   48,302,415,872 bytes free

Audit file ownership in a shared folder

dir C:\Shares\Finance /Q /B

CORP\svc-backup        budget_2025.xlsx
CORP\j.wilson          forecast_Q1.xlsx
BUILTIN\Administrators salaries_confidential.xlsx
CORP\svc-backup        archive_2024.zip

Find stale files by last access time

dir C:\Logs /T:A /O:D

 Directory of C:\Logs

01/04/2026  06:30 AM             8,192 app_old.log
01/19/2026  11:15 PM            24,576 system.log
03/01/2026  08:00 AM           102,400 iis_access.log
03/15/2026  04:22 PM             4,096 debug.log
               4 File(s)        139,264 bytes
               0 Dir(s)   48,302,415,872 bytes free

Redirecting Output

dir output can be sent to a file using standard shell redirection. > creates or overwrites the file. >> appends to an existing file.

dir /S /B *.log > loglist.txt
dir /S /B *.log >> loglist.txt

When redirecting, use /B and /A:-D together to get a clean list of file paths only — no headers, totals, or directory entries. This makes the output directly usable as script input.

dir /S /B /A:-D > filelist.txt

Things to Know Before Using dir in Scripts

Output encoding

cmd.exe uses the system OEM code page by default. On Russian Windows this is typically CP866. If you redirect dir output to a file and open it in a UTF-8 editor, extended characters in filenames may appear corrupted.

Before switching, check the currently active code page:

chcp

Active code page: 866

chcp (Change Code Page) changes the active encoding for the current cmd.exe session only — it does not affect system settings or other sessions. Note the original value, switch to UTF-8, run the command, then restore:

chcp 65001
dir /S /B > output.txt
chcp 866

The console font must also support the target encoding for characters to render correctly on screen. The file content will be correctly encoded regardless.

UNC paths

dir \\server\share works, but /Q may not resolve file owners correctly across domain boundaries or on shares with restricted ACL access.

No regex support

dir supports only * and ? wildcards. For pattern-based filtering that requires regular expressions, use PowerShell Get-ChildItem with -Filter or -Include.

dir is a cmd.exe built-in

It is not a standalone executable. When calling it from PowerShell, wrap it in cmd /c:

cmd /c dir /S /B *.log

In PowerShell scripts, Get-ChildItem is the native alternative and should be preferred. For a full comparison of dir vs Get-ChildItem see: (article in progress)

Quick Reference

CommandWhat it does
dir /BFilenames only, no metadata
dir /S /B *.logAll matching files, full paths
dir /A:H /S /BAll hidden files recursively
dir /A:LAll symlinks and junctions
dir /O:-SSorted by size, largest first
dir /T:A /O:DSorted by last access, oldest first
dir /Q /BFilenames with owner
dir /RShow alternate data streams
dir /S /B /A:-D > list.txtClean file index export

Related Resources