cmd dir Command


The dir command in Windows Command Prompt (cmd) is used to display a list of files and directories within a specified directory. It provides various options for filtering and formatting the output.

Basic Syntax

dir [drive:][path][filename] [/options]

Key Components

  • drive: The drive you want to list (e.g., C:).
  • path: The directory path you want to list (e.g., C:\Users).
  • filename: A specific file name or wildcard to filter results (e.g., *.txt).
  • /options: Additional flags to modify the output.

Example Usages

1. List All Files and Directories in the Current Directory

dir

Sample Output:

Volume in drive C is OS Volume Serial Number is 1234-5678 Directory of C:\Users\YourUsername 10/29/2024 12:15 PM <DIR> Documents 10/29/2024 12:15 PM <DIR> Downloads 10/29/2024 12:15 PM 1,024 example.txt 10/29/2024 12:15 PM 2,048 report.docx 2 File(s) 3,072 bytes 2 Dir(s) 100,000,000 bytes free

Explanation:

  • The output shows the volume name, serial number, and directory listing with:
    • Date and time of last modification.
    • <DIR> for directories.
    • File sizes for regular files.
    • Summary of total files, directories, and free space.

2. List Files in a Specific Directory

dir C:\Users\YourUsername\Documents

Sample Output:

Directory of C:\Users\YourUsername\Documents 10/29/2024 12:15 PM 3,456 project.pdf 10/28/2024 11:00 AM 1,024 notes.txt 2 File(s) 4,480 bytes 0 Dir(s) 100,000,000 bytes free

Explanation:

  • This shows the contents of the Documents directory, including file sizes and modification dates.

3. List All Files Recursively

dir /s

Sample Output:

Directory of C:\Users\YourUsername 10/29/2024 12:15 PM <DIR> Documents 10/29/2024 12:15 PM <DIR> Downloads 10/29/2024 12:15 PM 1,024 example.txt 10/29/2024 12:15 PM 2,048 report.docx Directory of C:\Users\YourUsername\Documents 10/29/2024 12:15 PM 3,456 project.pdf 10/28/2024 11:00 AM 1,024 notes.txt 2 File(s) 4,480 bytes 0 Dir(s) 100,000,000 bytes free

Explanation:

  • The /s option lists all files and directories within the specified directory and its subdirectories.

4. List Only Directories

dir /ad

Sample Output:

Directory of C:\Users\YourUsername 10/29/2024 12:15 PM <DIR> Documents 10/29/2024 12:15 PM <DIR> Downloads

Explanation:

  • This lists only the directories in the current directory, omitting files.

5. List Files with Detailed Information

dir /w

Sample Output:

Volume in drive C is OS Volume Serial Number is 1234-5678 Directory of C:\Users\YourUsername Documents Downloads example.txt report.docx

Explanation:

  • The /w option displays files and directories in a wide format, showing only names without additional details.

Common Options

  • /p: Pauses after each screen of output.
  • /a: Displays files with specified attributes (e.g., hidden).
  • /o: Sorts output (e.g., by name, size, or date).
  • /b: Displays filenames only (bare format).
  • /q: Displays the owner of each file.

Summary

The dir command is essential for navigating and managing files and directories in the Windows Command Prompt. It provides a variety of options to customize the output, making it a powerful tool for file system operations.