Open In App

cmd | Dir command

Last Updated : 21 Jun, 2022
Like Article

Dir is a command found inside the windows command processor (cmd.exe) that is generally used for listing the directories and files within the current directory. The command by itself is really basic, but the presence of its extensive switches makes it quite a dynamic command that has several use cases. It is one of the most useful commands while navigating the command line, and is present in its different forms in several operating systems. In this article, we will take a look at the Dir command, and would learn several use cases for it.                

Description of the command :

help dir

Output :

Displays a list of files and subdirectories in a directory.

DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
 [/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

             Specifies drive, directory, and/or files to list.

 /A          Displays files with specified attributes.
 attributes   D  Directories                R  Read-only files
              H  Hidden files               A  Files ready for archiving
              S  System files               I  Not content indexed files
              L  Reparse Points             O  Offline files
              -  Prefix meaning not
 /B          Uses bare format (no heading information or summary).
 /C          Display the thousand separator in file sizes.  This is the

Usage of the command : The command is mainly used for displaying the list of files and subdirectories in a directory. This could be done by executing the Dir command without any arguments.


which would produce an output similar to this. Output :

Volume in drive C has no label.
Volume Serial Number is 2C7D-7820

Directory of C:\Users

09/26/2020  11:34 AM    <DIR>          .
09/26/2020  11:34 AM    <DIR>          ..
09/02/2020  07:07 PM             1, 000 applese
09/24/2020  08:59 PM    <DIR>          Public
10/20/2020  06:39 PM    <DIR>          Soap
              1 File(s)          1, 000 bytes
              4 Dir(s)  13, 879, 459, 840 bytes free

This would serve the purpose for most users, but the command offers more functionality than this, By appending various switches we could modify the working of the command, to produce custom output. We would be looking over at some of the commonly used switches of the command. Displaying files/subdirectories having certain attributes : You can filter the output of dir by sending/A switch, followed by a specific attribute. What this will do is it will display only those files/folders which do have the provided attributes. The command would have the following syntax as follows.

Dir /A[attribute]

Where attribute will be a one/combination of one of the characters from the following list

              D  Directories                R  Read-only files
              H  Hidden files               A  Files ready for archiving
              S  System files               I  Not content indexed files
              L  Reparse Points             O  Offline files
              -  Prefix meaning not

According to the above list, If you want to display a list of Directories only. You can use the given below command.

Dir /AD

Which will display a list of subdirectories (Junction Points & Directory Symlinks as well) within the current directory. Displaying file/subdirectories of a directory using its absolute/relative path : You can get the list of file/subdirectories of now only the current directory, but other directories as well. If you provide the full path to the directory you can execute the dir command on that directory. The syntax would be as follows :

Dir [Path to the Directory]

Where Path to the Directory is either Relative or a full path to the Directory that we are interested in. For getting the contents of “C:\Users\Public” directory, the command would be :

Dir "C:\Users\Public"

It should be noted, that if the path to a file is provided as an argument, then only information regarding that file would be displayed. Sorting the output of Dir command : You can sort the list of files/folders in the output of dir command using the /O switch. The switch takes one/combination of these characters to produce a sorted output.

              N  By name (alphabetic)       S  By size (smallest first)
              E  By extension (alphabetic)  D  By date/time (oldest first)
              G  Group directories first    -  Prefix to reverse order

According to the above list, if you want the output to be sorted by the size of files (in descending order). The command syntax would be as follows.

Dir /O-S

Which would produce an output where the files having larger size would be at the top of the list, and smaller files/folder at the bottom. Note – In general, Directories would be at the bottom, as they aren’t as generally fixed size (existing as file table entry), as opposed to a file. Displaying output of Dir command in minimal format : The output of the dir command contains way too much information than what one may be interested in. In order to display the output of Dir command, in a bare format we can append /B switch to it. This will remove additional information such as Time of modification, Sizes, Types, etc. from the list of entries. The command syntax would be as follows.

Dir /B

Example – Consider if a directory has the following content. Then running the Dir command on the directory would produce the following output.

Volume in drive C has no label.
Volume Serial Number is 2C7D-7820

Directory of C:\Users\Sauleyayan\Pictures\Screenshots

10/21/2020  11:37 AM    <DIR>          .
10/21/2020  11:37 AM    <DIR>          ..
10/21/2020  11:12 AM         1, 240, 912 2020-10-21 11꞉12꞉23.png
10/21/2020  11:37 AM         1, 376, 105 2020-10-21 11꞉37꞉04.png
10/04/2020  10:10 AM    <DIR>          OLD SCREENSHOTS
10/19/2020  06:18 PM               287 UNUPLOADABLE_SCREENSHOTS.txt
              3 File(s)      2, 617, 304 bytes
              3 Dir(s)  12, 749, 389, 824 bytes free

While running the Dir command with /B switch would produce

2020-10-21 11꞉12꞉23.png
2020-10-21 11꞉37꞉04.png

Which is easier to read for most users. Note –

  • There exists more switches for the command, which could be found out at the help page of the command.
  • If the path to a directory/file is provided for displaying its contents, switches should be added after the before providing the path.      Creating a syntax such as –
Dir [switches] [Path to the Directory/File]
  • Dir is an internal command

Similar Reads

move cmd command
The move is an internal command found in the Windows Command Interpreter (cmd) that is used to move files and folders/directories. The command is robust than a regular move operation, as it allows for pattern matching via the inclusion of Wildcards in the source path. The command is a very generic one and is available (in one form or the other) in
5 min read
Assoc cmd command
Assoc is command (internal) found inside Windows Command Processor Command Prompt, that allows for file extension to file type association. A file extension is generally last few characters in File Name after period (“.”). Though it is not mandatory for file to have an extension at all (in case of extension-less files). Extension to file type assoc
3 min read
cmd | Rem command
Rem (abbreviation of remark) is a command (internal) found inside the Windows Command Processor Command Prompt, that allows for inclusion of comments inside batch programs. Comments are supported by most Programming languages usually via special syntax that is associated to them. Ex. Python using pound sign/hashtag ( # ) to denote a inline comment.
3 min read
Cd cmd command
Cd is the abbreviation or synonym for chdir. It is a command found inside the Windows Command Processor (cmd) that allows for change of the current working directory of a shell instance. The CWD (Current Working Directory) is a path (of a directory) inside the file system, where the shell is currently working. The current working directory is essen
3 min read
Color cmd command
Color is an inbuilt command found inside the Windows Command Processor (cmd.exe), that is used for changing the colors of the console's foreground and background. By default, the console has white foreground color and black background color (07 color code). The command is generally used either to personalize the aesthetics of the console window, or
3 min read
Getting hash of a file using CMD
A cryptographic hash is a fixed size string (or text) that is used as an identifier/fingerprint of some data. These are particularly useful in determining the integrity of files after they are transferred over a communication channel. Hashes are even utilized by certain OS-level processes for their working. The command processor of Windows OS (cmd.
2 min read
Mimic the Linux adduser command in C
Programming for fun can manifest itself in the works of leisurely coding practice as well. Regardless of being academically inadequate, the task certainly does contribute to the programmer's understanding of the resourcefulness of the language. Here let's look at a distinct programming activity that also translates into a low-level insight and flue
9 min read
Difference between Terminal, Console, Shell, and Command Line
Terminal :A terminal is a text input and output environment. A terminal window, also known as a terminal emulator, is a text-only window that emulates a console in a graphical user interface (GUI). In other words, a graphical user interface application from which we can access a user's console. A terminal is a program that allows you to run a shell
4 min read
Diskpart Command in Windows
DiskPart is a powerful tool for disk management under Windows OS. Diskpart Command is used to manage the computer hard drive, partitions, and volumes. This Command is not only limited to Windows 10 but also available in Windows 8.1,8,7 or even in Windows 2000. The Diskpart is used from creating or deleting partitions to wiping the drive clean or fi
4 min read
Umask command in Linux with examples
The umask command in Linux is used to set default permissions for files or directories the user creates. How does the umask command work?The umask command specifies the permissions that the user does not want to be given out to the newly created file or directory.umask works by doing a Bitwise AND with the bitwise complement(where the bits are inve
7 min read
Article Tags :