Bogdan Gavril MSFT
Bogdan Gavril MSFT

Reputation: 21448

How to do a simple file search in cmd

I want to quickly search for a file given its name or part of its name, from the windows command line (not power shell). This is similar to opening explorer and using the search box at the top.

Note: dir can search based on a string template but it will not search in the subdirectories.

Note2: findstr can be used to search for a token inside files and has a recursivity flag; it's funny that a more complex find can be easily discovered ...

Upvotes: 233

Views: 734097

Answers (6)

Vinod Srivastav
Vinod Srivastav

Reputation: 4255

Please try the following commands

List all files in the current directory & subdirectories

dir /b/s *.txt  

The above command searches for all txt file in the directory tree.

But as windows is started naming directories as .nuget,.vscode it also comes with the command above.

In order to avoid this and have a clean list use /a:-d filter as

dir /a:-d /b/s <yourfilename>

Before using it just change the directory to root using

cd/

There is one more hacky command to do the same

for /r %f in (*) do @echo %f <yourfilename>

Caution: If you miss the @echo part in the command above it will try to execute all the files in the directories, and the /r is what making it recursive to look deep down to subdirectories.


Export result to text file

you can also export the list to a text file using

dir /b/s *.exe >> filelist.txt

and search within using

type filelist.txt | find /n "filename"

If you are looking for files with special attributes, you can try

List all Hidden Files

dir /a:h-d /b/s

List all System Files

dir /a:s-d /b/s

List all ReadOnly Files

dir /a:r-d /b/s

List all Non Indexed Files

dir /a:i-d /b/s

If you remove the -d from all commands above it will list directories too.


Using where in windows7+:

Although this dir command works since the old dos days but Win7 added something new called Where

where /r c:\Windows *.exe *.dll

will search for exe & dll in the drive c:\Windows as suggested by @SPottuit you can also copy the output to the clipboard with

where /r c:\Windows *.exe |clip

just wait for the prompt to return and don't copy anything until then.

Page break with more

If you are searching recursively and the output is big you can always use more to enable paging, it will show -- More -- at the bottom and will scroll to the next page once you press SPACE or moves line by line on pressing ENTER

where /r c:\Windows *.exe |more

For more help try

where/?

Upvotes: 198

Shubham
Shubham

Reputation: 61

If you are searching any file or directory with name example

Use

dir example /s /p

Upvotes: 1

Gilles Arcas
Gilles Arcas

Reputation: 2712

dir /s *foo* searches in current folder and sub folders.

It finds directories as well as files.

where /s means(documentation):

/s Lists every occurrence of the specified file name within the specified directory and all subdirectories.

Upvotes: 310

Ali
Ali

Reputation: 1

You can search in windows by DOS and explorer GUI.

DOS:

1) DIR

2) ICACLS (searches for files and folders to set ACL on them)

3) cacls ..................................................

2) example

icacls c:*ntoskrnl*.* /grant system:(f) /c /t ,then use PMON from sysinternals to monitor what folders are denied accesss. The result contains

access path contains your drive

process name is explorer.exe

those were filters youu must apply

Upvotes: -1

Hobbe Lundahl
Hobbe Lundahl

Reputation: 21

Problem with DIR is that it will return wrong answers. If you are looking for DOC in a folder by using DIR *.DOC it will also give you the DOCX. Searching for *.HTM will also give the HTML and so on...

Upvotes: 0

Mullai Nathan
Mullai Nathan

Reputation: 94

dir *.txt /s /p will give more detailed information.

Upvotes: 2

Related Questions