Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows equivalent of Unix "find" command?

Tags:

find

windows

cmd

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?

like image 909
user3772839 Avatar asked Feb 04 '15 19:02

user3772839


People also ask

What is the find command equivalent in Windows?

The rough equivalent to the Windows find is the Unix grep .

Is there a find command in PowerShell?

The Find-Command cmdlet finds PowerShell commands such as cmdlets, aliases, functions, and workflows. Find-Command searches modules in registered repositories. For each command found by Find-Command , a PSGetCommandInfo object is returned.

How do I find Linux on Windows?

First, the easy one. From within the Windows Subsystem for Linux environment you want to browse, run the following command: explorer.exe . This will launch File Explorer showing the current Linux directory — you can browse the Linux environment's file system from there.


2 Answers

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)

like image 106
Walter A Avatar answered Oct 16 '22 04:10

Walter A


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)

like image 22
Solomon Rutzky Avatar answered Oct 16 '22 02:10

Solomon Rutzky