What is the equivalent of the Unix find command on Windows?
I see that the find.exe
on Windows is more like a grep
. I am especially interested in the equivalent of
find . -name [filename]
Kevin Panko
7,35622 gold badges44 silver badges53 bronze badges
asked Mar 16, 2012 at 15:10
0
dir <drive: [drive:]> /s | findstr /i <pattern>
— alternative —
dir /s <drive:>\<pattern>
example
dir c: d: /s | findstr /i example.txt
— alternative —
dir /s c:\example.txt
answered Mar 16, 2012 at 15:16
JohannesMJohannesM
1,00011 silver badges17 bronze badges
5
With no additional cmdlets installed, you can simply use Get-ChildItem
:
Get-ChildItem -Filter *.zip -Recurse $pwd
answered May 14, 2015 at 22:13
djhaskin987djhaskin987
4985 silver badges7 bronze badges
1
The Find-ChildItem
Cmdlet in Windows Powershell is an equivalent of Unix/Linux find command
http://windows-powershell-scripts.blogspot.in/2009/08/unix-linux-find-equivalent-in.html
Some of Find-ChildItem Options
Find-ChildItem -Type f -Name ".*.exe"
Find-ChildItem -Type f -Name "\.c$" -Exec "Get-Content {} | Measure-Object -Line -Character -Word"
Find-ChildItem -Type f -Empty
Find-ChildItem -Type f -Empty -OutObject
Find-ChildItem -Type f -Empty -Delete
Find-ChildItem -Type f -Size +9M -Delete
Find-ChildItem -Type d
Find-ChildItem -Type f -Size +50m -WTime +5 -MaxDepth 1 -Delete
Disclosure: I am the developer of Find-ChildItem
cmdlet
answered Jun 25, 2012 at 8:32
Jagadish GJagadish G
4054 silver badges3 bronze badges
4
If you are using Unix’s find to search for files in a directory hierarchy, then
the Powershell way is to use Get-ChildItem
(alias is gci
) cmdlet and filter the results with the Where-Object
(alias is where
) cmdlet.
For example, to find all files (starting from C:\Users\
and recursively) with the word ‘essential’ in its name, use the following:
PS> gci -Path "C:\Users\" -Recurse | where {$_.Name -like '*essential*'}
The -like
option allows you to use wildcards for pattern matching.
answered Dec 25, 2016 at 8:47
Joshua KanJoshua Kan
3073 silver badges6 bronze badges
This one is not exactly GNU find, but more closely matches the linux command line philisophy under powershell:
PS> dir -recurse -ea 0 | % FullName | sls <grep_string>
Example:
PS> cd C:\
PS> dir -recurse -ea 0 | % FullName | sls "Program" | sls "Microsoft"
PS> dir -recurse -ea 0 | % FullName | sls "Program" | sls "Microsoft" | out-gridview
Note: Everything returned after «| % FullName» is a string, instead of an object.
You can also use the Where Operator, «?», however, its more work, and not much faster:
PS> cd C:\
PS> dir -Recurse -ea 0 | ? FullName -like "*Program*"
| ? FullName -like "*Microsoft*"
| % FullName
| out-gridview
Here’s a quick shortcut:
PS> function myfind {dir -recurse -ea 0 | % FullName | sls $args }
PS> cd C:\
PS> myfind "Programs" | sls "Microsoft"
#find all text files recursively from current directory
PS> myfind "\.txt$"
#find all files recursively from current directory
PS> myfind .
answered Dec 19, 2017 at 19:28
Bill MooreBill Moore
2693 silver badges8 bronze badges
1
In PowerShell you can use Get-ChildItem
(aka ls
), as noted in other answers.
ls . -Filter *.zip -Recurse
It might also be useful to get full paths of files instead of short names.
(ls -Path . -Filter *.zip -Recurse).FullName
And you can also easily execute arbitrary commands on the files found.
(ls -Path . -Filter *.zip -Recurse).FullName | ForEach-Object -Process {
# The $_ variable is the path to a located file.
echo "Found file: $_"
}
answered Feb 2, 2021 at 0:29
SeridSerid
1334 bronze badges
1
ls c:\ file.ext -r
You can use this simple powershell command.
use -ErrorAction Ignore to get rid of permission errors.
answered Nov 19, 2020 at 1:13
JustinJustin
1113 bronze badges
1
While not a full substitute, this simple batch file solved most of the problem for me:
# findw.bat
#
# usage: findw dir search-pattern
#
dir %1 /s /b | findstr /i %2
answered Feb 27, 2021 at 20:49
You can use get-childitem very similar to find.
get-childitem -recurse [startpath] -name [filetofind]
[startpath]
is the path where recursion should begin (e.g. .
for the current directory)
[filetofind]
is what you are looking for.
It is even possible to do this from cmd (without interactieve powershell):
powershell -command "get-childitem -recurse [startpath] -name [filetofind]"
ZygD
2,45912 gold badges26 silver badges43 bronze badges
answered Dec 12, 2021 at 17:27
You must log in to answer this question.
Not the answer you’re looking for? Browse other questions tagged
.
Not the answer you’re looking for? Browse other questions tagged
.
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 theFOR
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)
on February 13, 2012
It’s very common scenario in IT field that people who are familiar with one OS have to learn/work with another OS. I do see many people, who are familiar with Linux commands, looking for their equivalent commands in Windows OS. Below I have attempted to list down the Windows commands for most widely used Linux commands. If I have missed any, please write in the comments section and let me know.
Ls : The Windows equivalent one is dir. ‘Dir‘ has many switches to list files based on different attributes, sort the list on size or date modified etc.
Grep : Findstr is the closet matching one for grep. find is also for searching strings in files, but it does not have many options.
Adduser: We can use net user to manage user accounts.
Useradd: net localgroup can be used to manage user groups. Net group can be used to manage active directory (domain) groups.
Uname : ver command shows Windows OS version. Systeminfo shows lot of hardware and software information.
Ps : The closest matching command is tasklist, though it’s not as feature rich as ps.
Echo : echo. Very much similar to Linux’s echo.
Md : mkdir
rm: Del deletes files whereas rmdir can be used to delete directories.
Du : du can be used to monitor the disk usage. However, this tool is not part of Windows OS. We need to install it separately. See the link for more information.
Df : Net use. We can list the network shares mapped on the computer.
Mount : Net use . Map network share to local drive
Cat : Type print the contents of a text file in the console/command prompt.
Head : there’s no equivalent command I am aware of.
Tail : Tail resource kit tool.
touch: Fsutil can be used to create files of require size.
I will add few more commands to the list soon…If you are searching for some specific command and do not find it here, please let me know.
Last updated on
Table of linux and windows commands equivalents like:
- shutdown vs reboot
- ipconfig vs ifconfig
- F7 vs history
- help vs man
Have you ever wonder about a command — is it a Linux one or a Windows one? For me, this happens all the time. I’m using commands quite often so I’ve made my own cheat-list with Linux and Windows pairs of commands. You can use it also when you want to learn the other operating system.
If you find any mistakes or incomplete information feel free to comment. I’m open for suggestions.
Note: It’s personal list based on personal experience. Some of the commands are used with different version of the OS so they could vary a little.
Next part is: Windows and Linux variable equivalents
Full page version:
Windows vs Linux commands cheat sheet
Here’s the result of it:
Windows |
Linux |
||||||
Type |
Description |
Command |
Example |
Result |
Command |
Example |
Result |
General |
Logoff |
shutdown |
shutdown -l |
reboot |
reboot |
||
Restart |
shutdown |
shutdown -r |
logout |
logout |
|||
Shutdown |
shutdown |
shutdown -s |
poweroff |
poweroff |
|||
Show user |
echo %USERNAME% |
echo %USERNAME% |
user |
echo |
echo $USER |
user |
|
Install application |
apt-get |
sudo apt-get install ${package} |
|||||
Remove application |
apt-get |
apt-get remove ${package} |
|||||
Zip/unzip current folder |
zip |
zip -r file.zip folder |
unzip |
sudo apt-get install zip unzip |
|||
Connect ssh |
You need putty client |
plink [email protected] -P 33 |
ssh |
ssh [email protected] -p 33 |
|||
List mounted devices |
net use |
net use |
findmnt |
findmnt -lo source,target,fstype,label,options |
|||
Mount Remote |
net use |
net use \\Server\ShareFolder |
mount |
mount 192.168.1.1:/home /mnt/nfs/home |
|||
Show network info |
ipconfig |
ipconfig /all |
Windows IP Configuration |
ifconfig |
ifconfig |
eth0 Link encap:Ethernet HWaddr |
|
Execute script |
test.cmd |
./ |
./test.sh |
||||
History |
F7 |
F7 |
0: dir |
history |
history |
|
|
Go to previous command |
↑ or F8 |
F8 or ↑ |
↑ |
↑ |
|||
Search for commands |
CTRL+R |
CTRL+R and type the command |
|||||
Clears screen |
cls |
cls |
clear |
clear |
|||
Closes shell prompt |
exit |
exit |
exit |
||||
Displays or sets date |
date |
date |
date |
||||
Displays command help |
command /? |
dir /? |
info |
man command |
|||
Displays command help 2 |
help command |
help dir |
man |
info command |
|||
Autocompletion |
TAB |
TAB |
TAB |
TAB |
|||
Uptime and logged user |
quser |
C:\Users\user\Desktop>quser |
USERNAME;SESSIONNAME;ID; |
uptime |
uptime |
06:18:56 up 75 days, 17:31, 1 user, |
|
Files & |
Show current folder |
cd |
cd |
C:\Users\user |
pwd |
pwd |
/home/user |
Show current folder 2 |
chdir |
chdir |
C:\Users\user |
echo |
echo $PWD |
/home/user |
|
Create file |
copy NUL file1.txt |
touch |
touch file.txt |
||||
Create file |
echo. 2>file2.txt |
cat |
cat > myfile.txt |
||||
Renames a file |
ren |
ren file1.txt file2.txt |
mv |
mv tfileold.txt filenew.txt |
|||
Copies files |
copy |
copy file1.txt C:/foldercopy |
cp |
cp file.txt /home/foldercopy |
|||
Moves files |
move |
move file1.txt C:/newfolder |
mv |
mv file.txt /home/newfolder |
|||
Lists files |
dir |
ls |
ls |
||||
Collect file names |
dir |
dir «C:\» >FilesC.txt /b /o |
file1 |
find |
find /home -name ‘*’ |
/home/file1 |
|
Deletes files |
del |
del deleteme.txt |
rm |
rm deleteme.txt |
|||
Deletes folder |
rmdir |
rmdir /S deleteme |
rm -r |
rm -r deleteme |
|||
«Echoes» output to the |
echo |
echo |
echo this message |
||||
Delete file content |
type |
type nul |
cat |
> file.txt |
|||
Compares the contents of files |
fc |
fc file1.txt file2.txt |
Comparing files file1.txt and |
diff |
diff file1.txt file2.txt |
||
Finds a string of text in a file |
find |
find «test» 123.txt |
|
grep |
grep test file1.txt |
test line |
|
Displays help |
command /? |
dir /? |
info |
man command |
|||
Displays help 2 |
help command |
help dir |
man |
info command |
|||
Creates a folder |
mkdir |
mkdir newfolder |
mkdir |
mkdir directory |
|||
View file content |
more |
more file1.txt |
less |
less file1.txt |
|||
View file content 2 |
type |
type file1.txt |
tail |
tail -n 15 file1.txt |
|||
Edit file |
edit |
edit file1.txt |
vi |
vi file.txt |
|||
Changes directories with a |
cd |
cd C:/ |
cd |
cd /directory/directory |
|||
Go one folder |
cd |
cd .. |
cd |
cd .. |
|||
Search for a file |
dir |
dir /s *test* |
07/31/2010 02:30 AM 301 test.png |
find |
find / -name ‘*test*’ |
/home/test.txt |
|
Search for text files |
dir |
dir /b/s *.txt |
find |
find / -name ‘*.txt’ |
|||
Change files extension to lower |
ren |
ren *.TXT *.txt |
mv |
for i in $( ls |
|||
Check files permissions |
ls |
ls -l /home/user |
«-rwxr—xr-x 1 root root 53 Mar 23 |
||||
Change Permissions |
chmod |
chmod 755 file |
|||||
Changing ownership |
chown |
chown user file |
|||||
Changing group ownership |
chgrp |
chgrp group file |
|||||
Other |
Displays the date |
date |
date /T |
Wed 07/05/2017 |
date |
date ‘+%d %W %Y’ |
05 27 2017 |
Displays the time |
time |
time /T |
1:27 PM |
date |
date ‘+%X’ |
12:18:22 PM |
|
Shows amount of RAM in use |
wmic |
wmic OS get FreePhysicalMemory |
free |
free |
|||
Show disk space |
wmic |
wmic logicaldisk |
dh |
dh -f |
|||
Show processes |
tasklist |
tasklist |
top |
top |
|||
Services |
Stop execution |
CTRL+C |
CTRL+C |
CTRL+C |
CTRL+C |
||
Start service |
net |
net start [serviceName] |
service |
sudo service [serviceName] start |
|||
Stop service |
net |
net stop [serviceName] |
service |
sudo service [serviceName] stop |
|||
Start service 2 |
sc |
sc start [serviceName] |
systemctl |
sudo systemctl start [serviceName] |
|||
Stop service 2 |
sc |
sc stop [serviceName] |
systemctl |
sudo systemctl stop [serviceName] |
|||
Variables |
Display all variables |
set |
set |
USERNAME=user |
printenv |
printenv |
HOME=/home/user |
Display variable |
echo |
echo %USERNAME% |
user |
echo |
echo $HOME |
/home/user |
|
Display variable 2 |
set |
set USERNAME |
USERNAME=user |
printenv |
printenv | grep |
HOME=/home/user |
|
Set variable |
set |
set MYVAR=VALUE |
export |
export MYVAR=/path/to/var |
|||
Set variable permanent |
setx |
setx MYVAR VALUE |
change profile |
vi ~/.bash_proflle |
Linux provides the find
command in order to find files and directories. The find command is executed via the command line interface and it is also provided by the Unix and other related Unix-based operating systems. The find command can be used to search files and folders according to their names, creation date, modification date, ownership, permission, path, etc. Also, it provides useful features like executing commands for the match results.
find Command Syntax
The find command has the following syntax.
find PATH -name SEARCH_TERM OPTIONS
- PATH is the search path where the specified SEARCH_TERM will be searched.
- SEARCH_TERM is searched in the specified PATH.
- OPTIONS are used for different search types. The option can e used to search for a specific file name pattern or file extension or recursively etc.
The find command provides different help options where the --help
option is one of them. The –help option list very basic help and usage information about the find command.
$ find --help
To get more details about the find command the man page can be used. The man page of the find command provides a lot of information about the options, usage examples, etc.
$ man find
Search File with Name
The files can be searched according to their names by using the -name
option. The complete or partial name is provided as a parameter with the -name option. In the following example, we search for the file named “database” in the home directory of the user “ismail”.
$ find /home/ismail -name database
Alternatively, we can search in the current working directory and all child directories by using bash “.” operator.
$ find . -name database
Search File with Pattern
The find command provides the ability to search files with their names for specific patterns. The “*” glob operator can be used to search files with partial names. We can search files those names that contain “data” with the following command.
$ find / -name "*data*"
Search File with Extension
The find command can be used to search files according to their extensions. The glob operator is used to express the name of the file and the extension is specified explicitly. In the following example, we search files with the “*.txt” extension.
$ find / -name "*.txt*"
Find and Delete File
One of the useful features of the find command is the ability to delete founded files. The -exec
option is used to delete files by executing the “rm” command. In the following example, we delete all text or “*.txt” extension files.
$ find / -name "*.txt*" -exec rm {} \;
Search Empty Files and Directory
The Linux operating system may contain empty files and directories. The find command can be used to find empty files and directories easily by using the -empty
option. There is no need to provide a file or directory name.
$ find / -empty
Search Empty Files with Specific Extension
The -empty option can be used to search empty files with a specific extension. In the following example, we search empty *.txt files by using the -name option.
$ find / -name "*.txt" -empty
Search with Permission
Another useful feature of the find command is the ability to search files and directories according to their permissions. The Linux system use permission consisting of 3 numbers like “000”, “777” etc. The -perm
the option is used to specify the permission value where all matched files and directories will be listed.
$ find / -perm 777
Search SUID Enabled Files
The find command can be used to search SUID-enabled files. SUID is a permission that enables the normal user to run privileged commands on its own. The -perm
option is used to specify SUID with /u=s
parameters.
$ find / -perm /u=s
Even we can search and delete SUID-enabled bash scripts for the specified path and all of its children.
$ find /tmp -perm /u=s -exec rm {} \;
Search with User Name
In Linux, every file and directory has an owner. The find command can be used to search according to the owner information of the file or directory. The -user
option is used to specify the owner name of the file or directory. In the following example, we search files and directories owned by “ismail”.
$ find / -user "ismail"
Search with Size
Another interesting option for the find command is the ability to search for the size of the files. The -size
option can be used to specify search terms that specified the size of the files to match. In the following example, we search for files with 100M size.
$ find / -size 100M
In the following example, we search for the files whose sizes are higher than 100M.
$ find / -size +100M
In the following example, we search for the files whose sizes are lower than 100M.
$ find / -size -100M
In the following example, we will search files with sizes between 100M and 200M.
$ find / -size +100M -size -200M
Delete Matched Files
The find command provides the ability to execute commands using the matched file names. The -exec
option is used to run commands and scripts where the matched filename is expressed with the {}
. In the following example, we run rm
command for every matched file by the find command.
$ find / -t f -name *.tmp -exe rm {} \;
Windows find Command
The counterpart operating system Windows also provides the find
command but it is very weak with its features and does not provide the same functionality Linux find command. The Windows find command can be used for basic search operations by providing the search term and search path like below.
> find /V "ismail" C:\