Dir Command (Examples, Options, Switches, and More)

Dir Command Examples, Switches, & More

Screenshot of the dir command in Windows 10
Dir Command (Windows 10).

The dir command is a Command Prompt command used to display a list of the files and subfolders contained in a folder.

For each file or folder listed, the dir command will, by default, show the date and time the item was last changed, if the item is a folder (labeled as <DIR>) or file, the size of the file if applicable, and finally the name of the file or folder including the file extension.

Outside of the file and folder list, the dir command also displays the current drive letter of the partition, the volume labelvolume serial number, total number of files listed, total size of those files in bytes, the number of subfolders listed, and the total bytes remaining free on the drive.

Dir Command Syntax

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

Tip: See How To Read Command Syntax if you're not sure how to interpret the syntax of the dir command as I've written it above.

drive:, path, filename = This is the drive, path, and/or filename that you want to see dir command results for. All three are optional since the dir command can be executed alone. Wildcards are allowed. See the Dir Command Examples section below if this isn't clear.

/a = When executed alone, this switch shows all types of files and folders, including those with file attributes that typically prevent them from showing up in Command Prompt or in Windows. Use /a with one or more of the following attributes (colon is optional, no spaces needed) to show only those types of files in the dir command result:

  • d = directories
  • h = hidden files
  • i = not content indexed files
  • l = reparse points
  • r = read-only files
  • s = system files
  • v = integrity files
  • x = no scrub files
  • - = Use this as a prefix to any of the above attributes to exclude items with those file attributes from the results.

/b = Use this option to show the dir results using "bare" format, which removes the typical header and footer information, as well as all the details on each item, leaving only the directory name or file name and extension.

/c = This switch forces the use of the thousands separator when the dir command is used in a way that shows file sizes. This is the default behavior on most computers so the practical use is /-c to disable the thousands separator in results.

/d = Use /d to limit the items displayed to just folders (contained within brackets) and file names with their extensions. Items are listed top-to-bottom and then across columns. Standard dir command header and footer data remain the same.

/l = This option to show all folder and file names in lowercase.

/n = This switch produces a result with columns in the date -> time -> directory -> file size -> file or folder name column structure. Since this is the default behavior, the practical use is /-n which produces columns in the file or folder name -> directory -> file size -> date -> time order.

/o = Use this option to specify a sort order for the results. When executed alone, /o lists directories first, followed by files, both in alphabetical order. Use this option with one or more of the following values (colon is optional, no spaces needed) to sort the dir command result in the specified manner:

  • d = sort by date/time (oldest first)
  • e = sort by extension (alphabetical)
  • g = group directory first, followed by files
  • n = sort by name (alphabetical)
  • s = sort by size (smallest first)
  • - = Use this as a prefix with any of the above values to reverse the order.

/p = This option displays the results one page at a time, interrupted with a Press any key to continue... prompt. Using /p is very similar to using the dir command with the more command.

/q = Use this switch to display the owner of the file or folder in the results. The easiest way to view or change a file's ownership from within Windows is via the Advanced button in the Security tab when looking at file's Properties.

/r = The /r option shows any alternate data streams (ADS) that are part of a file. The data stream itself is listed in a new row, under the file, is always suffixed with $DATA, making them easy to spot.

/s = This option shows all the files and folders in the specified directory plus all of the files and folders contained within any subdirectories of that specified directory.

/t = Use this option with one of the values below (colon is optional, no spaces needed) to specify a time field to be used when sorting and/or displaying results:

  • a = last access
  • c = created
  • w = last written

/w = Use /w to show results in "wide format" which limits the items displayed to just folders (contained within brackets) and file names with their extensions. Items are listed left-to-right and then down rows. Standard dir command header and footer data remain the same.

/x = This switch shows the "short name" equivalent for files whose long names don't comply with non-8dot3 rules.

/4 = The /4 switch forces the use of 4-digit years. At least in newer versions of Windows, the 4-digit year display is the default behavior and /-4 doesn't result in a 2-digit year display.

/? = Use the help switch with the dir command to show details about the above options directly in the Command Prompt window. Executing dir /? is the same as using the help command to execute help dir.

Tip: Considering the volume of information that the dir command usually returns, saving all of it to a text file via a redirection operator is usually a smart idea. See How To Redirect Command Output to a File for more on how to do this.

Dir Command Examples

dir

In this example, the dir command is used alone, without any drive:, path, filename specifications, nor any switches, resulting in a result like this:

C:\>dir
 Volume in drive C has no label.
 Volume Serial Number is F4AC-9851

 Directory of C:\

09/02/2015  12:41 PM    <DIR>          $SysReset
05/30/2016  06:22 PM                93 HaxLogs.txt
05/07/2016  02:58 AM    <DIR>          PerfLogs
05/22/2016  07:55 PM    <DIR>          Program Files
05/31/2016  11:30 AM    <DIR>          Program Files (x86)
07/30/2015  04:32 PM    <DIR>          Temp
05/22/2016  07:55 PM    <DIR>          Users
05/22/2016  08:00 PM    <DIR>          Windows
05/22/2016  09:50 PM    <DIR>          Windows.old
               1 File(s)             93 bytes
               8 Dir(s)  18,370,433,024 bytes free

As you can see, the dir command was executed from the root directory of C (i.e. C:\>). Without specifying where exactly to list the folder and file contents from, the dir command defaults to displaying this information from where the command was executed.

dir c:\users /ah

In the above example, I'm requesting that the dir command show results from the drive: and path of c:\users, not from the location I'm running the command from. I'm also specifying, via the /a switch with the h attribute, that I'd like to only see hidden items, resulting in something like this:

C:\>dir c:\users /ah
 Volume in drive C has no label.
 Volume Serial Number is F4AC-9851

 Directory of c:\users

05/07/2016  04:04 AM    <SYMLINKD>     All Users [C:\ProgramData]
05/22/2016  08:01 PM    <DIR>          Default
05/07/2016  04:04 AM    <JUNCTION>     Default User [C:\Users\Default]
05/07/2016  02:50 AM               174 desktop.ini
               1 File(s)            174 bytes
               3 Dir(s)  18,371,039,232 bytes free

The small list of directories and the single file you see in the result above doesn't make up the entirety of the c:\users folder - just the hidden files and folders. To see all files and folders, you would execute dir c:\users /a (removing the h) instead.

dir c:\*.csv /s /b > c:\users\tim\desktop\csvfiles.txt

In this slightly more complex, but much more practical, example for the dir command, I'm requesting that my entire hard drive be searched for CSV files and then the bare minimum results be outputted to a text document. Let's look at this piece by piece:

  • c:\*.csv tells the dir command to look at all files (*) that end in the CSV (.csv) extension in the root of the c: drive.
  • /s instructs dir to go deeper than the root of c: and instead, search for files like this in every folder, as deep as the folders go.
  • /b removes anything but the path and file name, essentially creating a readable "list" of these files.
  • > is a redirection operator, meaning "send to" somewhere.
  • c:\users\tim\desktop\csvfiles.txt is the destination for the > redirector, meaning that results will be written to the csvfiles.txt file which will created at the c:\users\tim\desktop location (i.e. the Desktop I see when I'm logged in).

When you redirect command output to a file, like we did here in this dir command example, Command Prompt doesn't display anything. However, the exact output you would have seen is instead located inside that text file. Here's what my csvfiles.txt looked like after the dir command had completed:

c:\ProgramData\Intuit\Quicken\Inet\merchant_alias.csv
c:\ProgramData\Intuit\Quicken\Inet\merchant_common.csv
c:\Users\All Users\Intuit\Quicken\Inet\merchant_alias.csv
c:\Users\All Users\Intuit\Quicken\Inet\merchant_common.csv
c:\Users\Tim\AppData\Roaming\condition.2.csv
c:\Users\Tim\AppData\Roaming\line.csv
c:\Users\Tim\AppData\Roaming\media.csv

While you certainly could have skipped the file redirection, and even the "bare format" switch, the results would have been very difficult to work with in the Command Prompt window, making it hard to get to what you were after - the location of every CSV file on your computer.

Dir Command Availability

The dir command is available from within the Command Prompt in all Windows operating systems including Windows 10, Windows 8, Windows 7, Windows Vista, and Windows XP.

Older versions of Windows include the dir command as well but with a few less options than I have listed above. The dir command is also a DOS command, available in all versions of MS-DOS.

The dir command can be found in offline Command Prompt versions, like the ones available from Advanced Startup Options and System Recovery Options. The dir command is also included in the Recovery Console in Windows XP.

Note: The availability of certain dir command switches and other dir command syntax may differ from operating system to operating system.

More From Us