No mundo da automação de tarefas, dominar a linha de comando é uma habilidade essencial. Seja com os tradicionais comandos GNU, ou no ecossistema Windows, com o poderoso PowerShell, conhecer as ferramentas certas pode transformar sua produtividade.
Nesta postagem, vamos confrontar 50 comandos GNU com seus equivalentes no PowerShell.
rm -rf /home/$USER/folder
Remove-Item -Path "C:\folder" -Recurse -Force
ps aux | grep apache2 # httpd
systemd:
systemctl status apache2
Get-Service | Where-Object { $_.DisplayName -like "*Apache*" }
sudo kill -9 $(pidof apache2) # httpd
systemd:
sudo systemctl stop apache2
Stop-Service -Name "Apache2.4"
unset NOME_DA_VARIAVEL
C:\App\bin
# Obtenha o valor atual da variável de ambiente Path do sistema
$envPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine)
# Separe os caminhos em um array
$paths = $envPath -split ';'
# Filtre para remover o caminho que você não quer mais
$newPaths = $paths | Where-Object { $_ -ne "C:\App\bin" }
# Recrie a variável de ambiente Path (sem o caminho indesejado)
$newPathString = ($newPaths -join ';').TrimEnd(';')
# Atualize a variável de ambiente do sistema
[Environment]::SetEnvironmentVariable("Path", $newPathString, [EnvironmentVariableTarget]::Machine)
which mycommand
Get-Command mycommand
mkdir my-project
New-Item -ItemType Directory "MyProject"
mkdir -p my-project/folder/new
New-Item -Path "C:/MyProject/folder/new" -ItemType Directory
mv folder new/path/
Move-Item -Path "folder" -Destination "C:\new\path\"
cd pasta/
Set-Location pasta
cp file path/to/dest
cp -r folder/ path/to/dest
Copy-Item file path\to\dest
Copy-Item folder\ -Recurse -Destination path\to\dest
$HOME
# echo $HOME
$USER
# echo $USER
$env:USERPROFILE
# Write-Host $env:USERPROFILE
$env:USERNAME
# Write-Host $env:USERNAME
ls -la
Get-ChildItem -Force
cat file.txt
Get-Content file.txt
grep "termo" file.txt
Select-String -Pattern "termo" -Path file.txt
df -h
Get-PSDrive -PSProvider FileSystem
free -h
Get-CimInstance Win32_OperatingSystem | Select-Object TotalVisibleMemorySize,FreePhysicalMemory
printenv
Get-ChildItem Env:
mv oldname newname
Rename-Item -Path oldname -NewName newname
sudo comando
Start-Process powershell -Verb runAs
ip addr show
Get-NetIPAddress
Exemplo para Terlang:
C:\Program Files\Terlang\bin
(Windows) e${HOME}/.local/terlang/bin/
(GNU)
export PATH="${PATH}:${HOME}/.local/terlang/bin/"
[System.Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\Program Files\Terlang\bin", [System.EnvironmentVariableTarget]::Machine)
tail -n 20 file.log
Get-Content file.log -Tail 20
top
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10
(não é em tempo real, mas dá um snapshot dos processos com maior uso de CPU)
pkill -f processo
Get-Process -Name processo | Stop-Process -Force
tail -f file.log
Get-Content file.log -Wait
tar -czvf archive.tar.gz folder/
Compress-Archive -Path folder\* -DestinationPath archive.zip
unzip archive.zip
Expand-Archive -Path archive.zip -DestinationPath destino\
echo $VARIAVEL
$env:VARIAVEL
export VARIAVEL=valor
$env:VARIAVEL="valor"
uname -a
Get-CimInstance Win32_OperatingSystem | Select-Object Caption, Version, OSArchitecture
date
Get-Date
who
query user
sudo netstat -tulpn
Get-NetTCPConnection | Select-Object LocalAddress,LocalPort,OwningProcess
find /path -name "arquivo.txt"
Get-ChildItem -Path C:\path -Recurse -Filter "arquivo.txt"
crontab -e
# Exemplo simples para criar tarefa agendada via PowerShell
$action = New-ScheduledTaskAction -Execute "notepad.exe"
$trigger = New-ScheduledTaskTrigger -At 9am -Daily
Register-ScheduledTask -TaskName "AbrirNotepad" -Action $action -Trigger $trigger
clear
Clear-Host
env
Get-ChildItem Env:
diff file1 file2
Compare-Object (Get-Content file1) (Get-Content file2)
./script.sh
.\script.ps1
Ctrl + C
Ctrl + C
history
Get-History
cat ~/.bash_history
Get-Content (Get-PSReadlineOption).HistorySavePath
history | grep termo
Get-History | Where-Object CommandLine -Match "termo"
set
Get-Variable
VARIAVEL=valor
$VARIAVEL = "valor"
command | less
command | Out-Host -Paging
alias ll='ls -la'
Set-Alias ll Get-ChildItem
unalias ll
Remove-Item Alias:ll
lscpu
Get-CimInstance Win32_Processor | Select-Object Name,NumberOfCores,NumberOfLogicalProcessors
vim arquivo.txt
notepad arquivo.txt
Fazer download de arquivo:
wget https://url.com/file.zip
# Ou: wget https://url.com/file.zip -O outronome.zip
Invoke-WebRequest -Uri "https://url.com/file.zip" -OutFile "file.zip"