203

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 ...

KumarAnkit
  • 713
  • 1
  • 9
  • 26
Bogdan Gavril MSFT
  • 20,615
  • 10
  • 53
  • 74
  • Possible duplicate of http://superuser.com/questions/177234/can-i-use-cmd-to-search-for-files-in-windows – handle Jul 27 '15 at 09:10

5 Answers5

272

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.

jumping_monkey
  • 5,941
  • 2
  • 43
  • 58
Gilles Arcas
  • 2,692
  • 2
  • 18
  • 13
  • I have another question, how to copy these found documents to another folder? THanks a lot – Hong Cheng Dec 19 '18 at 08:15
  • @HongCheng You can use **wildcard** with `Xcopy` after verifying the output with the `DIR` command above as use the same as `xcopy *foo* c:\Temp` to find all the `*foo*` files and copy them in `c:\Temp` directory – Vinod Srivastav Feb 22 '19 at 05:35
  • 1
    This refuses to find files I know are present within a subfolder of the current directory. Navigating to that folder and searching produces the result, but it is not found when searching via a parent directory. Is there any other more robust command? – Joe Dimig Jun 07 '21 at 18:04
  • @JoeDimig Probably it is a system or hidden file, for that you can use `dir /a:h /s /b` or `dir /a:s /s /b` for more reference plz see https://stackoverflow.com/a/25717502/3057246 – Vinod Srivastav Mar 17 '23 at 18:41
179

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

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

Note: 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/?
Vinod Srivastav
  • 3,644
  • 1
  • 27
  • 40
  • 5
    You can also export the list to your clipboard by replacing `>> filelist.txt` with `| clip`. [More info about using | clip](http://www.labnol.org/software/copy-command-output-to-clipboard/2506/). – Sjoerd Pottuit Apr 01 '16 at 11:38
  • 9
    By adding `/b` behind `dir` only the filename or filepath(if searching in multiple folders) is returned(**b**are format) and no details of the file and folder. By adding `/s` behind `dir` the **s**ub-directories will also be searched. [More info about the dir command](https://technet.microsoft.com/en-us/library/cc755121.aspx). – Sjoerd Pottuit Apr 01 '16 at 12:23
  • 2
    Don't know why this isn't the accepted answer. Contains everything that has and a lot more. Thanks for posting this. Wish I could upvote more than once! – Avrohom Yisroel Oct 02 '17 at 14:26
  • What if I can't change the directory? I have an E: drive and I can't change to it with CD. – user2924019 Oct 31 '17 at 14:06
  • ? even in drive E if you type cd\ it will take you to the root, i have updated the answer for your reference – Vinod Srivastav Oct 31 '17 at 16:13
  • 1
    one would expect that only cd\ or cd \ would change to the root directory, but it seems cd/ also does the job on Windows 10 - not sure since what DOS/Windows version this is true though – George Birbilis Oct 31 '17 at 16:13
  • btw, an answer https://superuser.com/questions/177234/can-i-use-cmd-to-search-for-files-in-windows also mentions using the FOR command for filtering results – George Birbilis Oct 31 '17 at 16:16
  • something that may be helpful with CD is the CD /D someFullPath which also changes the current drive and the CD %HOMEDRIVE% which changes to the home drive (https://support.microsoft.com/el-gr/help/101507/how-windows-nt-determines-a-user-s-home-directory). e.g. you can use CD /D %HOMEDRIVE%%HOMEPATH% to set user's home folder as the current folder – George Birbilis Oct 31 '17 at 16:24
  • @GeorgeBirbilis Yes, one can explore lot of things like using wildcard as cd Win*\\*32 , but it's out of context – Vinod Srivastav Oct 31 '17 at 16:35
  • 2
    @Vinod Srivastav, very nice. All possible scenarios explained. – Klanto Aguntuk Sep 13 '18 at 16:50
2

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

Agaz Wani
  • 5,514
  • 8
  • 42
  • 62
0

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...

Vinod Srivastav
  • 3,644
  • 1
  • 27
  • 40
-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

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Ali
  • 1
  • 1
  • Please try improve your answer. The original question is tagged with `windows` & `cmd` not with `sysinternals/pmon`. There are other ways like using the `FindFirstFileA` in a **C program** or to use `Directory.GetFiles` in a **c# program** but they are out of the context. – Vinod Srivastav Feb 14 '22 at 21:28