MS-DOS Commands :: dir
Overview:
Use the dir command to display a listing of the contents of the current directory. Information about the files and subdirectories of the current directory will be displayed.
Command Variations:
dir directory
By specifying a directory, the contents of that directory is displayed. For example,
dir \mystuff will display the contents of the \mystuff directory. Note: If directory contains a space, then type quotation marks around the directory name; for example,dir "c:\program files" dir > myfiles.txt
Will save a list of all files contained in the current directory to the file myfiles.txt. You can use
>filename at the end of any command to cause that command's output to be written to the file specified.Tip: You can use the clip command to save the directory listing to the Windows clipboard, such as:
dir | clip Tip: If you want to save a list of files and their properties (e.g.: file size, last modified date) to a .csv spreadsheet file, try using the filelist utility.
dir *.txt
Displays a listing of all the .txt files located in the current (or specified) directory.
dir my*
Displays a listing of all files that start with the pattern you specify; in this example, "my".
Command Options:
There are a few options you can use with the dir command. For the complete list of options, type
dir /? orhelp dir Position of options: If you specify a directory as part of the command, options can be specified before or after directory
Multiple options: You can type more then one option at the same time. For example,
dir /p/w \mystuff would display a wide listing of the \mystuff directory and pause after each screenful.The following examples demonstrate some of the more useful options:
dir /p
Pause the directory listing after every screenful. To continue the listing, press any key (e.g.: spacebar or Enter key).
dir /o:gne
Displays a directory listing that is sorted. You specify the sort order by one or more letter after the /o: part. The most useful sort order is gne which puts all the subdirectories before files (g), and sorts by name (n), and then by filename ending (e).
Tip: You can set the sort order (remains in effect until you close the
MS-DOS window) by typing a set command, such as:set dircmd=/o:gne dir /o:s
Displays a directory listing that is sorted by size (smallest to largest). To reverse the sort order (largest size to smallest size), prefix the sort order with a - (minus sign):
dir /o:-s dir /o:d
Displays a directory listing that is sorted by date/time (oldest to newest; oldest at top, newest at bottom). To reverse the sort order (newest to oldest; newest at top, oldest at bottom), prefix the sort order with a - (minus sign):
dir /o:-d dir /s
Displays the contents of the current directory (or specified directory) as well as the contents of all subdirectories and their subdirectories too. Since the output can be very long, especially if you have subdirectories inside subdirectories, you probably should also use the /p option to pause output. If you forget to do that, you'll probably see the output zipping by on your screen; in that case, press
Ctrl-C to stop the command. Or you might want to save the output to a text file, for example:dir /s > myfiles.txt will output a list of all files contained in the current directory and all subdirectories to the file myfiles.txtdir /s *.txt
Displays a listing of all .txt files located in the current directory and in all subdirectories.
dir /s mystuff.txt
Displays a listing of all instances of a file named mystuff.txt located in the current directory and in all subdirectories. Useful when you know the name of a file but don't recall which subdirectory it is located in. You can think of this command as the equivalent of the Windows Search Companion (accessible by pressing F3 when viewing a Windows folder).
dir /w
Wide directory listing.
dir /b
Displays a directory listing showing just the filenames without header information or file size/date. This bare format is useful when you just want the actual filenames and you're going to do some sort of further processing. For example
dir /b /s \mystuff > files.txt would create a file named files.txt that contains just the filenames of files located in the \mystuff directory and all its subdirectories.dir /ah
Displays a listing of hidden files (if any) that are located in the directory. Tip: Use the attrib command to change the hidden attribute of a file.
help dir
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] [/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 - Prefix meaning not /B Uses bare format (no heading information or summary). /C Display the thousand separator in file sizes. This is the default. Use /-C to disable display of separator. /D Same as wide but files are list sorted by column. /L Uses lowercase. /N New long list format where filenames are on the far right. /O List by files in sorted order. sortorder 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 /P Pauses after each screenful of information. /Q Display the owner of the file. /S Displays files in specified directory and all subdirectories. /T Controls which time field displayed or used for sorting timefield C Creation A Last Access W Last Written /W Uses wide list format. /X This displays the short names generated for non-8dot3 file names. The format is that of /N with the short name inserted before the long name. If no short name is present, blanks are displayed in its place. /4 Displays four-digit years Switches may be preset in the DIRCMD environment variable. Override preset switches by prefixing any switch with - (hyphen)--for example, /-W.
(Enlarge: help dir)
Related Commands:
- du
- du (disk usage) reports the disk space usage for the directory you specify. By default it recurses directories to show the total size of a directory and its subdirectories.
- find
- Searches for a text string in a file or files.
- findstr
- Search for a regular expression text string in a file or files.
- strings
- Find all strings of a specified minimum length in a file or files.
- tree
- Graphically displays the directory structure of a drive or directory.