Created
December 8, 2025 06:01
-
-
Save iCodeForBananas/95017e6be68e0009773d449b8b29e19d to your computer and use it in GitHub Desktop.
linux commands in my powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Linux-like compatibility layer for PowerShell | |
| # ----- navigation / shell ----- | |
| function cd { | |
| param([string]$Path = $HOME) | |
| Set-Location $Path | |
| } | |
| function pwd { Get-Location } | |
| # ----- listing ----- | |
| function ls { | |
| param( | |
| [string]$Path = ".", | |
| [switch]$a, | |
| [switch]$l | |
| ) | |
| $params = @{ Path = $Path } | |
| if ($a) { $params.Force = $true } | |
| $items = Get-ChildItem @params | |
| if ($l) { | |
| $items | Select-Object Mode, LastWriteTime, Length, Name | |
| } else { | |
| $items | |
| } | |
| } | |
| Set-Alias la ls | |
| function ll { ls -l -a } | |
| # ----- filesystem ops ----- | |
| function mkdir { | |
| param([Parameter(Mandatory=$true)][string]$Path) | |
| New-Item -ItemType Directory -Path $Path | Out-Null | |
| } | |
| function touch { | |
| param([Parameter(Mandatory=$true)][string]$Path) | |
| if (Test-Path $Path) { | |
| (Get-Item $Path).LastWriteTime = Get-Date | |
| } else { | |
| New-Item -ItemType File -Path $Path | Out-Null | |
| } | |
| } | |
| # rm with -r -f | |
| function rm { | |
| [CmdletBinding()] | |
| param( | |
| [switch]$r, | |
| [switch]$f, | |
| [Parameter(ValueFromRemainingArguments=$true)] | |
| [string[]]$Path | |
| ) | |
| if (-not $Path) { return } | |
| $params = @{ LiteralPath = $Path } | |
| if ($r) { $params.Recurse = $true } | |
| if ($f) { $params.Force = $true } | |
| Remove-Item @params | |
| } | |
| function cp { | |
| [CmdletBinding()] | |
| param( | |
| [Parameter(Mandatory=$true, Position=0)][string]$Source, | |
| [Parameter(Mandatory=$true, Position=1)][string]$Destination, | |
| [switch]$r | |
| ) | |
| $params = @{ Path = $Source; Destination = $Destination } | |
| if ($r) { $params.Recurse = $true } | |
| Copy-Item @params | |
| } | |
| function mv { | |
| [CmdletBinding()] | |
| param( | |
| [Parameter(Mandatory=$true, Position=0)][string]$Source, | |
| [Parameter(Mandatory=$true, Position=1)][string]$Destination | |
| ) | |
| Move-Item -Path $Source -Destination $Destination | |
| } | |
| # ----- search / text ----- | |
| function which { | |
| param([Parameter(Mandatory=$true)][string]$Name) | |
| Get-Command $Name -ErrorAction SilentlyContinue | | |
| Select-Object -ExpandProperty Source | |
| } | |
| function grep { | |
| [CmdletBinding()] | |
| param( | |
| [Parameter(Position=0, Mandatory=$true)] | |
| [string]$Pattern, | |
| [Parameter(ValueFromPipeline=$true)] | |
| $InputObject, | |
| [string[]]$Path, | |
| [switch]$i | |
| ) | |
| begin { $fromPipe = $false } | |
| process { | |
| if ($PSBoundParameters.ContainsKey('InputObject')) { | |
| $fromPipe = $true | |
| $InputObject | Select-String -Pattern $Pattern -CaseSensitive:(!$i) | |
| } | |
| } | |
| end { | |
| if (-not $fromPipe -and $Path) { | |
| Select-String -Pattern $Pattern -Path $Path -CaseSensitive:(!$i) | |
| } | |
| } | |
| } | |
| function head { | |
| param( | |
| [Parameter(Mandatory=$true)][string]$Path, | |
| [int]$n = 10 | |
| ) | |
| Get-Content -Path $Path -Head $n | |
| } | |
| function tail { | |
| param( | |
| [Parameter(Mandatory=$true)][string]$Path, | |
| [int]$n = 10, | |
| [switch]$f | |
| ) | |
| if ($f) { | |
| Get-Content -Path $Path -Tail $n -Wait | |
| } else { | |
| Get-Content -Path $Path -Tail $n | |
| } | |
| } | |
| function less { | |
| param([Parameter(Mandatory=$true)][string]$Path) | |
| Get-Content $Path | Out-Host -Paging | |
| } | |
| # ----- help ----- | |
| function man { | |
| param([Parameter(Mandatory=$true)][string]$Name) | |
| Get-Help $Name -Full | Out-Host -Paging | |
| } | |
| # ----- processes ----- | |
| function psaux { | |
| Get-Process | Select-Object ` | |
| @{n='USER';e={$_.SI}}, ` | |
| @{n='PID';e={$_.Id}}, ` | |
| @{n='%CPU';e={$_.CPU}}, ` | |
| @{n='MEM';e={$_.WS}}, ` | |
| @{n='NAME';e={$_.ProcessName}} | |
| } | |
| function top { | |
| Get-Process | Sort-Object CPU -Descending | Select-Object -First 15 ` | |
| Id, ProcessName, CPU, WorkingSet | |
| } | |
| # ----- disk usage ----- | |
| function df { | |
| Get-PSDrive -PSProvider FileSystem | | |
| Select-Object Name, | |
| @{n='TotalGB';e={[math]::Round(($_.Used/1GB + $_.Free/1GB),2)}}, | |
| @{n='UsedGB'; e={[math]::Round($_.Used/1GB,2)}}, | |
| @{n='FreeGB'; e={[math]::Round($_.Free/1GB,2)}} | |
| } | |
| function du { | |
| param([string]$Path = ".") | |
| $sum = (Get-ChildItem -Recurse -File $Path -ErrorAction SilentlyContinue | | |
| Measure-Object -Property Length -Sum).Sum | |
| [PSCustomObject]@{ | |
| Path = (Resolve-Path $Path).Path | |
| SizeM = [math]::Round(($sum / 1MB), 2) | |
| } | |
| } | |
| # ----- open / xdg-open ----- | |
| function open { | |
| param([string]$Path = ".") | |
| Invoke-Item $Path | |
| } | |
| Set-Alias xdg-open open | |
| # ----- sudo (elevated command) ----- | |
| function sudo { | |
| param( | |
| [Parameter(Mandatory=$true, Position=0)] | |
| [string]$Command, | |
| [Parameter(ValueFromRemainingArguments=$true)] | |
| [string[]]$Args | |
| ) | |
| $argLine = @( | |
| "-NoProfile", | |
| "-Command", | |
| "$Command $($Args -join ' ')" | |
| ) | |
| Start-Process pwsh -Verb RunAs -ArgumentList $argLine | |
| } | |
| # ----- chmod / chown (very rough) ----- | |
| function chmod { | |
| param( | |
| [Parameter(Mandatory=$true)][string]$Mode, | |
| [Parameter(Mandatory=$true)][string]$Path | |
| ) | |
| # only implements +x for scripts | |
| if ($Mode -eq "+x") { | |
| $acl = Get-Acl $Path | |
| $rule = New-Object System.Security.AccessControl.FileSystemAccessRule( | |
| "$env:UserName","ReadAndExecute","Allow" | |
| ) | |
| $acl.AddAccessRule($rule) | |
| Set-Acl $Path $acl | |
| } else { | |
| Write-Warning "chmod: only +x supported in this shim." | |
| } | |
| } | |
| function chown { | |
| param( | |
| [Parameter(Mandatory=$true)][string]$User, | |
| [Parameter(Mandatory=$true)][string]$Path | |
| ) | |
| Write-Warning "chown not supported on Windows; no-op shim." | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment