brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: where /r finds a file by name from the command prompt

Terminal
~/tips/tip-windows-where-recursive

where /r C:\Users\Public *.log

Finds files by name under a folder, or shows which copy of a program is on your PATH.

Explorer search is slow when I only know a file name. The where command in the Command Prompt searches by name, and it can show size and date too.

where displays the location of files that match a pattern. By default it searches the current directory and the folders in the PATH environment variable, which is why it is good at answering “which copy of this program do I actually run”. With /r it searches recursively from a directory you give it.

where /r C:\Users\Public *.log

The pattern can use * and ? wildcards. Add /t to show the file size and last modified date and time of each match, which helps when several files share a name:

where /t /r D:\Backups config.xml

If you do not give a file name extension, the extensions in the PATHEXT environment variable are appended to the pattern. That is why where notepad finds notepad.exe without you typing the extension.

Two more switches are handy in scripts. /q returns only an exit code, 0 for success and 1 for failure, without printing the matches, so you can test whether a program exists. And /f puts the results in quotation marks, which helps when paths have spaces:

where /q git && echo git is installed

The docs also show the form $ENV:pattern, which searches the paths stored in an environment variable, and path:pattern for a single directory. They add that these forms should not be combined with /r.

The caveat is the scale of a recursive search. Starting at the root of a big drive takes a while, so start in the most likely folder instead of C:\. And run it from the Command Prompt: the examples in the docs are written for that, and I did not check how other shells treat the word where.

Further reading: where

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link