简体   繁体   中英

Windows equivalent of Unix “find” command?

I'd like to be able to search files on a Windows machine using the command line instead of the GUI interface. For example, on Linux, I use:

find . -name "*.c" -exec grep -Hn "sqlcommand" {} \\;

Is there something similar with Windows?

After long time working with Unix systems I had to make some scripts on Windows. For serious scripting Powershell is the tool you should use. You can search Internet with keywords like powershell find string in file , or other combinations and you find a lot of information. That's the problem, a simple oneliner like

get-childitem C:\yourdir -include *.c -recursive |Select-String -pattern sqlcommand

won't help you much. You need to find the PowerShell IDE, learn the different syntax and try to love / accept that new stuff.

Prepare for a study with PowerShell when you want to do these things more often, or try to get a Unix-like environment on your windows (cygwin, or better git for windows)

NEW AND IMPROVED ANSWER

I recently stumbled upon a built-in command that is rather similar to find in Unix:

ForFiles

Basic syntax is:

forfiles [/p <Path>] [/m <SearchMask>] [/s] [/c <Command>] [/d [{+|-}][{<Date>|<Days>}]]

There are several variables to use when constructing the command to execute per each file (via the /c switch):

  • @FILE File name.
  • @FNAME File name without extension.
  • @EXT File name extension.
  • @PATH Full path of the file.
  • @RELPATH Relative path of the file.
  • @ISDIR Evaluates to TRUE if a file type is a directory. Otherwise, this variable evaluates to FALSE.
  • @FSIZE File size, in bytes.
  • @FDATE Last modified date stamp on the file.
  • @FTIME Last modified time stamp on the file.

It looks like you would use the command like this:

FORFILES /m *.cs /c FINDSTR /I /N /C:"sqlcommand" @FILE

I'm not sure how long this command has been around, but the earliest reference I could find in the documentation is from 2008-09-02:

https://web.archive.org/web/20080902221744/http://technet.microsoft.com:80/en-us/library/cc753551.aspx

and that page states that it was last updated on "April 25, 2007". The documentation is filed under "Windows Server" so it likely started there and was added to the desktop OSes starting with Windows Vista, I believe. I did check Windows XP and didn't see it there, though it is on Windows 10.


ORIGINAL ANSWER

This requires a combination of two DOS commands:

  • FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

    and

  • DIR /B /O:N /W *.c (this is the 'command' noted in the FOR command above)

Create a CMD script as follows:

@ECHO OFF

FOR /F %%B IN ('DIR /B /O:N /W *.cs') DO (
    findstr /I /N /C:"sqlcommand" %%B
)

OR , just use the find command found in this set of Unix command ports:

http://unxutils.sourceforge.net/

or

http://sourceforge.net/projects/unxutils/

(both links should be the same project)

There is a FIND command in DOS as well, but it works a bit differently than find in the Linux shell.

Syntax: FIND [/V] [/C] [/N] [/I] "string" [[drive:][path]filename[...]]

Where:

/V Displays all lines NOT containing the specified string.

/C Displays only the count of lines containing the string.

/N Displays line numbers with the displayed lines.

/I Ignores the case of characters when searching for the string.

https://en.wikipedia.org/wiki/Find_%28command%29

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM