If you are an advanced user of Windows, you might want to use the command line to find files on your system. You can use the command line to find files by date, content, size, and location, etc. In this article, I will show you how to use various methods and commands to find files using the command line in Windows 10/11.
Basic File Search Commands
One of the simplest ways to search for files using the command line is to use the dir and findstr commands. The dir command lists the files and folders in a directory, and you can use it with wildcards (*) to find file by name or extension from the Windows command line. For example, to use CMD to search for files with the extension TXT in the current directory, you can use the command:
dir *.txt
The Findstr command searches for a string of text in a file or files. You can use it with the /i option to ignore case sensitivity and with the /s option to search in all subdirectories. For example, to search for the word “hello” in all text files in the current directory and its subdirectories, you can use the command:
findstr /i /s "hello" *.txt
CMD Find File Name Containing a String
As mentioned, you can use the dir command to find a file name containing a specific string. For example, to find a file name containing the string “share” in the current directory and its subdirectories, you can type:
dir /s /b *share*
This will return a list of file names that match the pattern. The /s switch tells the command to search in all subdirectories, and the /b switch tells it to display only the bare file name.
You can also use wildcards to specify more complex patterns. For example, to find a file name that starts with “report” and with extension “.docx”, you can type:
dir /s /b report*.docx
Advanced File Search Commands
The find command searches for files that match certain criteria, such as name, size, date, and attributes. You can search words within files using the command prompt.
For example, to search for the word “hello” in all text files in the current directory, you can use the following command:
findstr /i /m "hello" *.txt
To search for all files larger than 1 MB in the C: drive, you can use the command:
forfiles /P C:\ /S /C "cmd /c if @fsize gtr 1048576 echo @path"
where Command for Executable Files
If you want to search for executable files, such as programs and applications, you can use the where command. It is mainly for locating executable files in a specified path or paths. You can use wildcards (*) with the where command to search for files with a specific name or extension. For example, to search for all executable files with the word “chrome” in their name in the C: drive, you can use the command:
where /r C:\ *chrome*.exe
Combine Dir and Find Commands
You can also use advanced filtering options with the dir command to search for files based on their attributes, such as size, date, and extension. You can use various switches with the dir command to filter your results. For example, to search for all readonly files in the current directory, you can use the command:
dir /a:r
To search for all files modified after January 1st, 2024 in the current directory, you can use the command:
dir /t:w /o:d | findstr /b "01/01/2024"
To search for all PDF files smaller than 100 KB in the current directory and its subdirectories, you can use the command:
dir /s *.pdf | findstr /v "<DIR>" | findstr /v "bytes" | findstr /r "[09][09][09] KB"
Windows CMD Find File Recursively
If you want to perform recursive searches using the dir command, you can use the /s switch. The /s switch searches for files in the current directory and all its subdirectories. For example, to search for all text files in the current directory and its subdirectories, you can use the command:
dir /s *.txt
Recursive searches are useful for locating files that are buried deep in your system’s folders.
You can also use the Where command to search recursively. For example, to list all files that start with “ffmp” in the c:\windows directory and its subdirectories, use the following command:
where /r c:\users ffmp*
PowerShell Commands for File Searches
PowerShell is a powerful command line tool that allows you to perform various tasks on your system. You can also use PowerShell commands to find files based on various criteria. For example, to search for all text files that contain the word “hello” in the current directory and its subdirectories, you can use the command:
Get-ChildItem -Path .\* -Include *.txt -Recurse | Select-String -Pattern "hello"
To search for all files larger than 1 MB in the C: drive, you can use the command:
Get-ChildItem -Recurse -Path C:\ | Where-Object {$_.Length gt 1MB}
To search for all PDF files created before January 1st 2024 in the current directory and its subdirectories, you can use the command:
Get-ChildItem -Path .\* -Include *.mp4 -Recurse | Where-Object { $_.CreationTime -lt '2024-01-01' }
Common CommandLine File Search Parameters
Here are some general tips for optimizing your command line file searches in Windows:
- Use wildcards (*) to search for files with a specific name or extension.
- Use the /s switch with the dir, find, and findstr commands to search in all subdirectories.
- Use the /r switch with the where command to search in a specified path or paths.
- Use the /a switch with the dir and find commands to filter files by attributes, such as hidden, readonly, system, etc.
- Use the /t switch with the dir command to filter files by date, such as creation, modification, or access.
- Use the /o switch with the dir command to sort files by name, size, date, or extension.
- Use PowerShell commands to perform more advanced file searches with various criteria.
Conclusion
In this article, I have shown you how to find files using the command line in Windows 11/10. Hopefully, you have learned to use advanced filtering and sorting options with these commands to get your results quickly.