dir - Search for files

Maintained on

The dir command lists the files and folders in a directory. Combined with wildcards and options, it also works as a file search tool.

This article covers the basics of dir, how to search for files, and the most useful options.

Quick Answer

GoalCommand
List the contentsdir
Filter by extensiondir *.txt
Search including subfoldersdir /s *.txt
Show file names onlydir /b
Show hidden filesdir /a:h
Sort by name, size, or datedir /o:n /o:s /o:d

Basics of the dir Command

Run without arguments, it lists the files and folders in the current directory.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir

It works the same way in a batch file.

@echo off
setlocal

dir

endlocal
exit

To inspect another folder, specify its path.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir C:\Users\user\Documents
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

Searching for Files

Search with Wildcards

Use * (any string) and ? (any single character) to display only matching files.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir *.txt
2023/04/29 14:11 7 test2.txt
2023/04/29 14:11 7 text.txt
2 File(s) 14 bytes
0 Dir(s) 266,096,242,688 bytes free
C:\users\user>

This lists the text files (.txt) in the current directory.

Include Subfolders (/s)

The /s option searches the current directory and all of its subfolders.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir /s *.txt

Combined with /b, only the full path of each match is shown, which is handy for finding where a file is located.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir /s /b *.txt

Main Options of the dir Command

/a: Filter by Attribute

/a:attribute shows only files and folders with the given attribute. Without it, dir does not show hidden or system files.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir /a:h
OptionDescription
/a:dFolders only
/a:-dFiles only (exclude folders)
/a:hHidden files
/a:sSystem files
/a:rRead-only files

/b: Names Only

Omits details such as date and size and shows only file and folder names. This is also useful when passing results to other commands.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir /b

/o: Sorting

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir /o:n
OptionDescription
/o:nSort by name (ascending)
/o:-nSort by name (descending)
/o:sSort by size (ascending)
/o:-sSort by size (descending)
/o:dSort by date modified (ascending)
/o:-dSort by date modified (descending)

Summary

  • dir lists a folder’s contents, and wildcards narrow the results
  • /s searches subfolders, and combined with /b it returns full paths
  • /a, /b and /o control what is shown, how, and in what order
#PowerShell #Command Prompt #Batch File