commands detail - f

find

The bash find command has loads of functionality - I could possibly devote many pages to Powershell equivalents of the various options, but at it’s simplest the bash find does this:

 1 find . -name '*BB.txt'
 2 
 3 ./Archive/Script_WO7171BB.txt
 4 
 5 ./Archive/Script_WO8541BB.txt
 6 
 7 ./Archive/Script_WO8645_BB.txt
 8 
 9 ./Archive/WO8559B/Script_WO8559_Master_ScriptBB.txt
10 
11 ./Archive/WO8559B/WO8559_finalBB.txt
12 
13 ./Archive/WO8559B/WO8559_part1BB.txt
14 
15 ./Archive/WO8559B/WO8559_part2BB.txt

The simplest Powershell equivalent of the bash find is simply to stick a -recurse on the end of a dir command

 1 PS x:\> dir  *BB.txt -recurse
 2 
 3     Directory: x:\Archive\WO8559B
 4 
 5 Mode                LastWriteTime     Length Name
 6 ----                -------------     ------ ----
 7 -----        28/02/2012     17:15        608 Script_WO8559_Master_ScriptBB.txt
 8 -----        28/02/2012     17:17         44 WO8559_finalBB.txt
 9 -----        28/02/2012     17:17      14567 WO8559_part1BB.txt
10 -----        28/02/2012     17:15       1961 WO8559_part2BB.txt
11 
12     Directory: x:\Archive
13 
14 Mode                LastWriteTime     Length Name
15 ----                -------------     ------ ----
16 -----        15/06/2011     08:56       2972 Script_WO7171BB.txt
17 -----        14/02/2012     16:39       3662 Script_WO8541BB.txt
18 -----        27/02/2012     15:22       3839 Script_WO8645_BB.txt

If you want Powersehll to give you output that looks more like the Unix find then you can pipe into | select fullname

 1 PS x:\> dir  *BB.txt -recurse | select fullname
 2 
 3 FullName
 4 --------
 5 x:\Archive\WO8559B\Script_WO8559_Master_ScriptBB.txt
 6 x:\Archive\WO8559B\WO8559_finalBB.txt
 7 x:\Archive\WO8559B\WO8559_part1BB.txt
 8 x:\Archive\WO8559B\WO8559_part2BB.txt
 9 x:\Archive\Script_WO7171BB.txt
10 x:\Archive\Script_WO8541BB.txt
11 x:\Archive\Script_WO8645_BB.txt

for

for loop - start, stop, step

The equivalent of this bash:

 1 for (( i = 1 ; i <= 5 ; i++ ))
 2 do   
 3   echo "Hello, world $i"
 4 done
 5 
 6 Hello, world 1
 7 Hello, world 2
 8 Hello, world 3
 9 Hello, world 4
10 Hello, world 5

…is

 1 for ($i = 1; $i -le 5; $i++)
 2 {
 3   write-output "Hello, world $i"
 4 }
 5 
 6 Hello, world 1
 7 Hello, world 2
 8 Hello, world 3
 9 Hello, world 4
10 Hello, world 5

for loop - foreach item in a list

For the Bash

1 for I in Chelsea Arsenal Spuds
2 do
3   echo $I
4 done

the equivalent Powershell is:

1 foreach ($Team in ("Chelsea", "Arsenal", "Spuds")) {write-output $Team}

for loop - for each word in a string

For the bash:

1 london="Chelsea Arsenal Spurs"
2 for team in $london; do   echo "$team"; done

…the equivalent Powershell is:

1 $London = "Chelsea Arsenal Spuds"
2 foreach ($Team in ($London.split())) {write-output $Team}

for loops - for lines in a file

Bash:

1 for team in $(egrep -v mill london.txt)
2 > do
3 >   echo $team
4 > done

Posh:

1 select-string -notmatch millwall london.txt | select line | foreach {write-output $_}

or:

1 foreach ($team in (select-string -notmatch millwall london.txt | select line)) {$tea\
2 m}

for loop - for each file in a folder

Bash:

1 for LocalFile in *
2 do   
3   echo $LocalFile
4 done

Posh:

1 foreach ($LocalFile in $(gci)) {write-output $LocalFile.Name}