SYS-CON MEDIA Authors: Pat Romanski, Gary Arora, Zakia Bouachraoui, Yeshim Deniz, Liz McMillan

Blog Feed Post

Unix To PowerShell - Find

PowerShell_unix PowerShell is definitely gaining momentum in the windows scripting world but I still hear folks wanting to rely on Unix based tools to get their job done.  In this series of posts I’m going to look at converting some of the more popular Unix based tools to PowerShell.

find

The Unix “find” command searches through one or more directory trees of a file system, locating files based on some user specific criteria.  By default, find returns all files below the current working directory.  It also allows you to perform an action to be taken on each matched file.

In my PowerShell script I have only included the “file location” functions and will leave adding the action feature as an exercise for the reader.

This script starts out by calling the Do-Find function which basically just stuffs all the command line arguments into a hash table and calls the function Find-InDirectory with the given start location.  Find-InDirectory will get all child items in the specified location and then iterate through that list.  If the child item is a directory, the current depth is incremented, a recursive call to the Find-InDirectory is made for the child directory, and then the current depth is decremented.  If the child item is a file, the Get-IsMatch function is called to determine whether the file matches the specified criteria from the command line arguments.

The Unix parameters map to the following in my PowerShell script:

Unix PowerShell Description
path -start The directory to start the search from (default = “.”).
-maxdepth -maxdepth Descend at most “n” levels of directories below start path.
-mindepth -mindepth Do not apply tests at levels less than “n” levels below start path.
-amin -amin Only process files that were accessed more recently than “n” minutes ago.
-atime -atime Only process files that were accessed “n”*24 hours ago.
-empty -empty Only process empty files or directories.
-name -name Only process files where file name matches “name” pattern.
-path -path Only process files where the full path matches the “path” pattern.

 

   1: #----------------------------------------------------------------
   2: # Find.ps1
   3: #----------------------------------------------------------------
   4: param
   5: (
   6:   [string]$start = ".", # directory to start search in
   7:   [int]$maxdepth = -1, # decend at most "n" levels below starting line
   8:   [int]$mindepth = -1, # don't process levels less than mindepth
   9:   [int]$amin = -1, # file was last accessed "n" minutes ago
  10:   [int]$atime = -1, # file was last accessed "n"*24 hours ago
  11:   [bool]$empty = $false, # file is empty and is a file or directory.
  12:   [string]$name = "", # base of file name matches pattern.
  13:   [string]$path = "" # filename/path matches pattern.
  14: );
  15:  
  16: $script:CURRENT_DEPTH = 0;
  17:  
  18: #----------------------------------------------------------------
  19: # function Get-IsMatch
  20: #----------------------------------------------------------------
  21: function Get-IsMatch()
  22: {
  23:   param
  24:   (
  25:     $info = $null,
  26:     $context = $null
  27:   );
  28:   [bool]$bIsMatch = $true;
  29:   
  30:   if ( Is-InDepthRange -context $context )
  31:   {
  32:     if ( $context["name"].Length -gt 0 )
  33:     {
  34:       $bIsMatch = $info.Name -like $context["name"];
  35:     }
  36:     elseif ( $context["path"].Length -gt 0 )
  37:     {
  38:       $bIsMatch = $info.FullName -like $context["path"];
  39:     }
  40:     
  41:     if ( $bIsMatch -and ($context["amin"] -ne -1) )
  42:     {
  43:       $ts = [DateTime]::Now - $info.LastAccessTime;
  44:       if ( $ts.TotalMinutes -gt $context["amin"] )
  45:       {
  46:         $bIsMatch = $false;
  47:       }
  48:     }
  49:  
  50:     if ( $bIsMatch -and ($context["atime"] -ne -1) )
  51:     {
  52:       $ts = [DateTime]::Now - $info.LastAccessTime;
  53:       if ( $ts.TotalHours -gt (24 * $context["atime"]) )
  54:       {
  55:         $bIsMatch = $false;
  56:       }
  57:     }
  58:  
  59:     $bIsEmpty = $false;
  60:     if ( $info -is [System.IO.FileInfo] )
  61:     {
  62:       $bIsEmpty = $info.Length -eq 0;
  63:     }
  64:     elseif ( $info -is [System.IO.DirectoryInfo] )
  65:     {
  66:       $bIsEmpty = ( $info.GetFiles().Length -eq 0 );
  67:     }
  68:     if ( $context["empty"] ) { $bIsMatch = $bIsEmpty; }
  69:   }
  70:   else
  71:   {
  72:     $bIsMatch = $false;
  73:   }
  74:   
  75:   $bIsMatch;
  76: }
  77:  
  78: #----------------------------------------------------------------
  79: # function Is-InDepthRange
  80: #----------------------------------------------------------------
  81: function Is-InDepthRange()
  82: {
  83:   param($context = $null);
  84:   
  85:   $bInRange = $true;
  86:   if ( $context )
  87:   {
  88:     if ( -1 -ne $context["mindepth"] )
  89:     {
  90:       if ( $script:CURRENT_DEPTH -lt $context["mindepth"] )
  91:       {
  92:         $bInRange = $false;
  93:       }
  94:     }
  95:     if ( -1 -ne $context["maxdepth"] )
  96:     {
  97:       if ( $script:CURRENT_DEPTH -gt $context["maxdepth"] )
  98:       {
  99:         $bInRange = $false;
 100:       }
 101:     }
 102:   }
 103:   
 104:   $bInRange;
 105: }
 106:  
 107: #----------------------------------------------------------------
 108: # function Find-InDirectory
 109: #----------------------------------------------------------------
 110: function Find-InDirectory()
 111: {
 112:   param
 113:   (
 114:     [string]$location = ".", # directory to start search in
 115:     $context = $null
 116:   );
 117:   
 118:   $cis = Get-ChildItem -Path $location;
 119:   foreach ($ci in $cis)
 120:   {
 121:     if ( $ci.PSIsContainer )
 122:     {
 123:       if ( Get-IsMatch $ci -context $context )
 124:       {
 125:         $ci.FullName;
 126:       }
 127:       
 128:       # Recurse through directories
 129:       $script:CURRdENT_DEPTH++;
 130:       Find-InDirectory -location $ci.FullName -context $context;
 131:       $script:CURRENT_DEPTH--;
 132:     }
 133:     else
 134:     {
 135:       if ( Get-IsMatch $ci -context $context )
 136:       {
 137:         $ci.FullName;
 138:       }
 139:     }
 140:   }
 141:   
 142: }
 143:  
 144: #----------------------------------------------------------------
 145: # function Do-Find
 146: #----------------------------------------------------------------
 147: function Do-Find()
 148: {
 149:   param
 150:   (
 151:     [string]$start = ".",
 152:     [int]$maxdepth = -1,
 153:     [int]$mindepth = -1,
 154:     [int]$amin = -1,
 155:     [int]$atime = -1,
 156:     [bool]$empty = $false,
 157:     [string]$name = "",
 158:     [string]$path = ""
 159:   );
 160:   
 161:   $context = @{
 162:     "maxdepth" = $maxdepth; "mindepth" = $mindepth;
 163:     "amin" = $amin; "atime" = $atime;
 164:     "empty" = $empty; "name" = $name; "path" = $path};
 165:  
 166:   Find-InDirectory -location $start -context $context;
 167: }
 168:  
 169: Do-Find -start $start -maxdepth $maxdepth -mindepth $mindepth -amin $amin `
 170:   -atime $atime -empty $empty -name $name -path $path;

There are a few enhancements that could be made to this script such as better range checking to eliminate unnecessary directory recursion and also adding support for actions.

You can download the full source for the script here: Find.ps1

Read the original blog entry...

More Stories By Joe Pruitt

Joe Pruitt is a Principal Strategic Architect at F5 Networks working with Network and Software Architects to allow them to build network intelligence into their applications.

Latest Stories
Every organization is facing their own Digital Transformation as they attempt to stay ahead of the competition, or worse, just keep up. Each new opportunity, whether embracing machine learning, IoT, or a cloud migration, seems to bring new development, deployment, and management models. The results are more diverse and federated computing models than any time in our history.
On-premise or off, you have powerful tools available to maximize the value of your infrastructure and you demand more visibility and operational control. Fortunately, data center management tools keep a vigil on memory contestation, power, thermal consumption, server health, and utilization, allowing better control no matter your cloud's shape. In this session, learn how Intel software tools enable real-time monitoring and precise management to lower operational costs and optimize infrastructure...
"Calligo is a cloud service provider with data privacy at the heart of what we do. We are a typical Infrastructure as a Service cloud provider but it's been designed around data privacy," explained Julian Box, CEO and co-founder of Calligo, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Isomorphic Software is the global leader in high-end, web-based business applications. We develop, market, and support the SmartClient & Smart GWT HTML5/Ajax platform, combining the productivity and performance of traditional desktop software with the simplicity and reach of the open web. With staff in 10 timezones, Isomorphic provides a global network of services related to our technology, with offerings ranging from turnkey application development to SLA-backed enterprise support. Leadin...
While a hybrid cloud can ease that transition, designing and deploy that hybrid cloud still offers challenges for organizations concerned about lack of available cloud skillsets within their organization. Managed service providers offer a unique opportunity to fill those gaps and get organizations of all sizes on a hybrid cloud that meets their comfort level, while delivering enhanced benefits for cost, efficiency, agility, mobility, and elasticity.
DevOps has long focused on reinventing the SDLC (e.g. with CI/CD, ARA, pipeline automation etc.), while reinvention of IT Ops has lagged. However, new approaches like Site Reliability Engineering, Observability, Containerization, Operations Analytics, and ML/AI are driving a resurgence of IT Ops. In this session our expert panel will focus on how these new ideas are [putting the Ops back in DevOps orbringing modern IT Ops to DevOps].
Darktrace is the world's leading AI company for cyber security. Created by mathematicians from the University of Cambridge, Darktrace's Enterprise Immune System is the first non-consumer application of machine learning to work at scale, across all network types, from physical, virtualized, and cloud, through to IoT and industrial control systems. Installed as a self-configuring cyber defense platform, Darktrace continuously learns what is ‘normal' for all devices and users, updating its understa...
Enterprises are striving to become digital businesses for differentiated innovation and customer-centricity. Traditionally, they focused on digitizing processes and paper workflow. To be a disruptor and compete against new players, they need to gain insight into business data and innovate at scale. Cloud and cognitive technologies can help them leverage hidden data in SAP/ERP systems to fuel their businesses to accelerate digital transformation success.
Concerns about security, downtime and latency, budgets, and general unfamiliarity with cloud technologies continue to create hesitation for many organizations that truly need to be developing a cloud strategy. Hybrid cloud solutions are helping to elevate those concerns by enabling the combination or orchestration of two or more platforms, including on-premise infrastructure, private clouds and/or third-party, public cloud services. This gives organizations more comfort to begin their digital tr...
Most organizations are awash today in data and IT systems, yet they're still struggling mightily to use these invaluable assets to meet the rising demand for new digital solutions and customer experiences that drive innovation and growth. What's lacking are potent and effective ways to rapidly combine together on-premises IT and the numerous commercial clouds that the average organization has in place today into effective new business solutions.
Keeping an application running at scale can be a daunting task. When do you need to add more capacity? Larger databases? Additional servers? These questions get harder as the complexity of your application grows. Microservice based architectures and cloud-based dynamic infrastructures are technologies that help you keep your application running with high availability, even during times of extreme scaling. But real cloud success, at scale, requires much more than a basic lift-and-shift migrati...
David Friend is the co-founder and CEO of Wasabi, the hot cloud storage company that delivers fast, low-cost, and reliable cloud storage. Prior to Wasabi, David co-founded Carbonite, one of the world's leading cloud backup companies. A successful tech entrepreneur for more than 30 years, David got his start at ARP Instruments, a manufacturer of synthesizers for rock bands, where he worked with leading musicians of the day like Stevie Wonder, Pete Townsend of The Who, and Led Zeppelin. David has ...
Darktrace is the world's leading AI company for cyber security. Created by mathematicians from the University of Cambridge, Darktrace's Enterprise Immune System is the first non-consumer application of machine learning to work at scale, across all network types, from physical, virtualized, and cloud, through to IoT and industrial control systems. Installed as a self-configuring cyber defense platform, Darktrace continuously learns what is ‘normal' for all devices and users, updating its understa...
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Addteq is a leader in providing business solutions to Enterprise clients. Addteq has been in the business for more than 10 years. Through the use of DevOps automation, Addteq strives on creating innovative solutions to solve business processes. Clients depend on Addteq to modernize the software delivery process by providing Atlassian solutions, create custom add-ons, conduct training, offer hosting, perform DevOps services, and provide overall support services.