Windows command line dir command

Windows command line dir command

Displays a list of a directory’s files and subdirectories. If used without parameters, this command displays the disk’s volume label and serial number, followed by a list of directories and files on the disk (including their names and the date and time each was last modified). For files, this command displays the name extension and the size in bytes. This command also displays the total number of files and directories listed, their cumulative size, and the free space (in bytes) remaining on the disk.

The dir command can also run from the Windows Recovery Console, using different parameters. For more information, see Windows Recovery Environment (WinRE).

Syntax

Parameters

] Specifies the drive and directory for which you want to see a listing. [ ] Specifies a particular file or group of files for which you want to see a listing. /p Displays one screen of the listing at a time. To see the next screen, press any key. /q Displays file ownership information. /w Displays the listing in wide format, with as many as five file names or directory names on each line. /d Displays the listing in the same format as /w, but the files are sorted by column. /a[[:] ] Displays only the names of those directories and files with your specified attributes. If you don’t use this parameter, the command displays the names of all files except hidden and system files. If you use this parameter without specifying any attributes, the command displays the names of all files, including hidden and system files. The list of possible attributes values are:

  • d — Directories
  • h — Hidden files
  • s — System files
  • l — Reparse points
  • r — Read-only files
  • a — Files ready for archiving
  • i — Not content indexed files

You can use any combination of these values, but don’t separate your values using spaces. Optionally you can use a colon (:) separator, or you can use a hyphen (-) as a prefix to mean, «not». For example, using the -s attribute won’t show the system files. /o[[:] ] Sorts the output according to sortorder, which can be any combination of the following values:

  • n — Alphabetically by name
  • e — Alphabetically by extension
  • g — Group directories first
  • s — By size, smallest first
  • d — By date/time, oldest first
  • Use the prefix to reverse the sort order

Multiple values are processed in the order in which you list them. Don’t separate multiple values with spaces, but you can optionally use a colon (:).

If sortorder isn’t specified, dir /o lists the directories alphabetically, followed by the files, which are also sorted alphabetically. /t[[:] ] Specifies which time field to display or to use for sorting. The available timefield values are:

  • c — Creation
  • a — Last accessed
  • w — Last written
/s Lists every occurrence of the specified file name within the specified directory and all subdirectories. /b Displays a bare list of directories and files, with no additional information. The /b parameter overrides /w. /l Displays unsorted directory names and file names, using lowercase. /n Displays a long list format with file names on the far right of the screen. /x Displays the short names generated for non-8dot3 file names. The display is the same as the display for /n, but the short name is inserted before the long name. /c Displays the thousand separator in file sizes. This is the default behavior. Use /c to hide separators. /4 Displays years in four-digit format. /r Display alternate data streams of the file. /? Displays help at the command prompt.
Читайте также:  Unix and microsoft windows

Remarks

To use multiple filename parameters, separate each file name with a space, comma, or semicolon.

You can use wildcard characters (* or ?), to represent one or more characters of a file name and to display a subset of files or subdirectories.

You can use the wildcard character, *, to substitute for any string of characters, for example:

dir *.txt lists all files in the current directory with extensions that begin with .txt, such as .txt, .txt1, .txt_old.

dir read *.txt lists all files in the current directory that begin with read and with extensions that begin with .txt, such as .txt, .txt1, or .txt_old.

dir read *.* lists all files in the current directory that begin with read with any extension.

The asterisk wildcard always uses short file name mapping, so you might get unexpected results. For example, the following directory contains two files (t.txt2 and t97.txt):

You might expect that typing dir t97\* would return the file t97.txt. However, typing dir t97\* returns both files, because the asterisk wildcard matches the file t.txt2 to t97.txt by using its short name map T97B4

1.TXT. Similarly, typing del t97\* would delete both files.

You can use the question mark (?) as a substitute for a single character in a name. For example, typing dir read. txt lists any files in the current directory with the .txt extension that begin with read and are followed by up to three characters. This includes Read.txt, Read1.txt, Read12.txt, Read123.txt, and Readme1.txt, but not Readme12.txt.

If you use /a with more than one value in attributes, this command displays the names of only those files with all the specified attributes. For example, if you use /a with r and -h as attributes (by using either /a:r-h or /ar-h ), this command will only display the names of the read-only files that aren’t hidden.

If you specify more than one sortorder value, this command sorts the file names by the first criterion, then by the second criterion, and so on. For example, if you use /o with the e and -s parameters for sortorder (by using either /o:e-s or /oe-s ), this command sorts the names of directories and files by extension, with the largest first, and then displays the final result. The alphabetic sorting by extension causes file names with no extensions to appear first, then directory names, and then file names with extensions.

If you use the redirection symbol ( > ) to send this command’s output to a file, or if you use a pipe ( | ) to send this command’s output to another command, you must use /a:-d and /b to only list the file names. You can use filename with /b and /s to specify that this command is to search the current directory and its subdirectories for all file names that match filename. This command lists only the drive letter, directory name, file name, and file name extension (one path per line), for each file name it finds. Before you use a pipe to send this command’s output to another command, you should set the TEMP environment variable in your Autoexec.nt file.

Examples

To display all directories one after the other, in alphabetical order, in wide format, and pausing after each screen, make sure that the root directory is the current directory, and then type:

The output lists the root directory, the subdirectories, and the files in the root directory, including extensions. This command also lists the subdirectory names and the file names in each subdirectory in the tree.

To alter the preceding example so that dir displays the file names and extensions, but omits the directory names, type:

To print a directory listing, type:

When you specify prn, the directory list is sent to the printer that is attached to the LPT1 port. If your printer is attached to a different port, you must replace prn with the name of the correct port.

You can also redirect output of the dir command to a file by replacing prn with a file name. You can also type a path. For example, to direct dir output to the file dir.doc in the Records directory, type:

If dir.doc does not exist, dir creates it, unless the Records directory does not exist. In that case, the following message appears:

To display a list of all the file names with the .txt extension in all directories on drive C, type:

The dir command displays, in wide format, an alphabetized list of the matching file names in each directory, and it pauses each time the screen fills until you press any key to continue.

MS-DOS and Windows command line dir command

The dir command displays information about files and directories, and how much disk space is available. By default, it displays the name, size, and last modification time of every file in the current directory.

Availability

Dir is an internal command available in the command line of all Microsoft operating systems.

Description

The dir command displays a list of files and subdirectories in a directory. With the /S option, it recurses subdirectories and lists their contents as well.

Options listed below may be preset in the DIRCMD environment variable. To override preset options, prefix any switch with — (hyphen), for example, «/-W«.

Syntax

The syntax of the dir command has evolved somewhat over time. Select your operating system to jump to the information that applies to your version.

Windows 10, 8, 7, Vista, XP, and 2000 syntax

[Drive:][Path][FileName] Specifies the drive, directory, or files to list. Multiple filespecs are allowed, e.g., «*.txt *.exe«.
/A:Attributes Displays only files with the specified file attributes. Attributes is a series of letters, each representing an attribute as shown below.

For example, the option «/A:R-A» would match only files whose attributes (/A:) are read-only (R) and not () ready to be archived (A). /B Uses bare format (no heading information or summary, only the information itself). /C Display the thousand separator in file sizes (e.g., a comma after every third digit), which is the default setting. Use /-C to disable the display of separator. /D Same as wide (/W), but files are sorted by column, rather than by row. /L Uses lowercase in the output. /N «New long-list» format, which displays file names on the far right. /O:SortOrder Sort the list of files by SortOrder, a series of letters representing sort criteria.

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.

For instance, an option of «/O:D» displays files oldest-to-newest, and «/O:-S» displays files biggest-to-smallest. /P Pauses after each screenful of information. /Q Display the owner of the file. /R Display alternate data streams of the file. /S Displays files recursively, traversing any subdirectories. /T TimeField Specify the time field displayed and used for sorting. TimeField may be any of the following letters.

C : Creation time.
A : Last access time.
W : Last write time.

For instance, when you use the option «/T:C,» the time listed is when the file was created. /W Uses wide list format, displaying file/folder names only, with multiple names on every line. /X Displays the short names generated for non-8dot3 file names. The format is that of /N («new long-list» format, see above), with the short name inserted before the long name. If no short name is present, blanks are displayed in its place. /4 Display years with four digits, e.g., 2018 instead of 18.

Windows ME, 98, 95, 3.x, and MS-DOS syntax

[Drive:][Path][FileName] Specifies drive, directory, or files to list. Multiple filespecs are allowed, e.g., «*.txt *.exe«.
/P Pauses after each screenful of information.
/W Uses wide list format.
/A[:Attributes] List only files with the specified file attributes. Attributes is a series of letters indicating.

D : Directories.
R : Read-only files.
H : Hidden files.
A : Files ready for archiving.
S : System files.
: Prefix meaning «not». /O[:SortOrder] List files in sorted order, indicated by SortOrder.

N : By name (alphabetic).
S : By size (smallest first).
E : By extension (alphabetic).
D : By date and time (earliest first).
G : Group directories first.
: Prefix to reverse order.
A : By last access date (earliest first). /S List files and directories recursively, traversing subdirectories. /B Uses bare format (no heading information or summary). /L Uses lowercase. /V Verbose mode. Display extra information.

Examples

Lists all files and directories in the current directory.

Lists any file whose name has the file extension «.exe«.

Because of how the wildcard matching works in the command line. If a file has a file extension with four or more characters and starts with the same first three characters (e.g., .exec) it’s also shown.

List any files whose name has the file extension «.txt» or «.doc«.

Lists only directories.

List only files with the read-only attribute.

Recursively lists files and directories in the directory, and in any subdirectories. For instance, if your current directory is the root directory «C:\>,» this command lists every file and directory on the C: drive.

Pause after each screenful of output. Use this option if the information is scrolling past the screen before you can read it. You’ll be prompted to press any key before listing continues past the current screen.

Lists multiple file names on every line, producing «wide» output, which displays more file names at once. However, other information such as file size is omitted.

Recursively lists all files and directories in the current directory and any subdirectories, in wide format, pausing after each screen of output.

Same as the above command, but lists everything in C:\Program Files, instead of the current directory. Because the directory name contains a space, it is enclosed in double-quotes, to prevent it from being interpreted is as two separate options.

Lists files and directories in the current directory in alphabetical order. This example is the same as only running dir because files and directories are listed in alphabetical order by default.

Lists files in reverse alphabetical order.

Lists any files and directories in C:\Windows, and any of its subdirectories (/s), which have both the «hidden» and «system» file attributes (/a:sh). Also, lists the owner of the file (/q), and pauses after each screen of output (/p).

The above command uses vertical bars to pipe the output from dir to the command find, and then to the command more. The result is a list of all files and directories in the root directory of the current drive (\), with extra information. Namely, find also displays the number of files in each directory, and the amount of space occupied by each.

Runs the dir command, but redirects the output to the file myfile.txt, instead of displaying it on the screen. Here, the dir command has no options, but redirection works with any command you specify, so the following command also works.

To view the contents of the file, you can use the type command:

If the file is very long, you can pipe type to more, so it pauses after each screen:

Оцените статью