Open In App

cmd | Dir command

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]

 [drive:][path][filename]
             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.



Dir

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
OLD SCREENSHOTS
UNUPLOADABLE_SCREENSHOTS.txt

Which is easier to read for most users. Note –

Dir [switches] [Path to the Directory/File]
Article Tags :