cmd dir Command Options


The dir command in Windows Command Prompt (cmd) is a powerful tool used to display a list of files and directories within a specified directory. Below, I’ll explain the command with examples that include various options and their corresponding outputs.

Basic Syntax

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

Common Options

  • /p: Pauses after each screen of output, allowing you to read the information.
  • /w: Displays the listing in a wide format, showing only file and directory names.
  • /a: Displays files with specific attributes (like hidden or system files).
  • /o: Sorts the output in a specified order (name, size, date, etc.).
  • /s: Lists all files in the specified directory and all subdirectories.
  • /b: Displays only the filenames without additional details.
  • /q: Displays the owner of the file.

Examples of Using dir with Options

1. Basic dir Command

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

2. Using the /p Option

dir /p

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 Press any key to continue . . .

Explanation: The /p option pauses the output after each screen, allowing you to read the information before proceeding.

3. Using the /w Option

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 the listing in a wide format, showing only file and directory names.

4. Using the /a Option to Show Hidden Files

dir /a

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 10/28/2024 10:00 AM 2,048 hiddenfile.txt <Hidden> 4 File(s) 5,072 bytes 2 Dir(s) 100,000,000 bytes free

Explanation: The /a option shows all files, including hidden files, which are not displayed by default.

5. Using the /o Option to Sort by Date

dir /o:d

Sample Output:

Volume in drive C is OS Volume Serial Number is 1234-5678 Directory of C:\Users\YourUsername 10/28/2024 10:00 AM 2,048 hiddenfile.txt 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 4 File(s) 5,072 bytes 2 Dir(s) 100,000,000 bytes free

Explanation: The /o:d option sorts the output by date, showing the oldest files first.

6. Using the /s Option to List 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.

7. Using the /b Option for Bare Format

dir /b

Sample Output:

Documents Downloads example.txt report.docx

Explanation: The /b option displays only the names of files and directories, omitting other details like date and size.

Summary

The dir command is essential for navigating and managing files and directories in Windows Command Prompt. By utilizing various options, you can customize the output to suit your needs, whether you want a simple list, detailed information, or specific file attributes.