Retro Posts
I’m starting a series of importing some old posts I wrote on an almost forgotten WordPress site to Medium, with some light editing and grammatical fixing.
This is #retropost number 1. Enjoy!
I had started working at a company with extensive Windows-based tooling and software stack.
Learning new platforms and technologies was an exciting change and challenge, as I previously worked at companies with mainly Linux and Unix-based software stacks.
As a DevOps Engineer, you must always use the command line. It can be for ad-hoc commands or for writing comprehensive automation scripts. At first, I tried using as many cross-platform utilities as possible, such as Python or the Bash shell on Windows 10 feature. However, for some existing Windows-based builds and tasks, I had no choice but to adapt my familiar shell commands from Linux to Powershell.
There are many challenges in the Windows ecosystem. For example, have you ever tried to automate the creation of an extended Active Directory Domain and Schema using LDIF files? When working with many LDIF files, I was frustrated that I could not find a syntax highlighter for Visual Studio Code, my favorite IDE, so I decided to write my own 😉
Fortunately, Powershell includes nifty aliases from many common Linux shell commands to native Powershell cmdlets that fulfill the same expected functionality as their Linux shell counterparts.
Some example aliases include:
cd -> Set-Location
ls -> Get-ChildItem
cp -> Copy-Item
mv -> Move-Item
cat -> Get-Content
pwd -> Get-Location
mkdir -> New-Item -ItemType Directory
You got the gist.
However, not all commands have these convenient aliases for Linux users, and some basic commands you have been using in Linux shells do not exist.
Here are five of my top Powershell equivalents for Linux commands.
1. Select-String => grep
The SELECT-STRING
cmdlet is the grep
command equivalent for searching and extracting text patterns from text files.
A few interesting features worth mentioning in Select-String
- The default mode is case-insensitive (Like Windows itself).
- If you are used to piping to the
tr
command, you can skip that since the-Replace
flag comes out of the box for this cmdlet (like in many others) - It’s got a nifty alias:
sls
2. Write-Output => echo
When all you want is to echo some text out to the terminal. It’s worth mentioningWrite-Output.
This command is not an exact replacement for echo
but is more like a mixture of ls
, and find
since it includes file information and finding the files by filename.
For example, the default output of the command on Linux find . -name "*cont*"
yoseft@YOSEF-LAPTOP:~$ find . -name "*cont*"
./.local/lib/python2.7/site-packages/ansible/galaxy/data/container_enabled
./.local/lib/python2.7/site-packages/ansible/galaxy/data/container_enabled/meta/container.yml.j2
./.local/lib/python2.7/site-packages/ansible/modules/cloud/docker/docker_container.py
./.local/lib/python2.7/site-packages/ansible/modules/cloud/docker/docker_container.pyc
./.local/lib/python2.7/site-packages/ansible/modules/cloud/lxc/lxc_container.py
Is in Powershell:
PS C:\WINDOWS\system32> Get-ChildItem . "*cont*"
Directory: C:\WINDOWS\system32
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---s- 10/15/2017 10:21 PM containers
d----- 9/29/2017 5:41 PM MailContactsCalendarSync
-a---- 9/29/2017 4:41 PM 518 @WindowsUpdateToastIcon.contrast-black.png
-a---- 9/29/2017 4:41 PM 810 @WindowsUpdateToastIcon.contrast-white.png
-a---- 9/29/2017 4:41 PM 920064 AppContracts.dll
As you can see, we get additional file information about the desired files.
3. Get-Childitem => find
This command is not exactly a replacement but more like a mixture of ls
, and find
since it includes file information and finding the files by filename.
For example, the default output of the command on Linux find . -name "*cont*"
yoseft@YOSEF-LAPTOP:~$ find . -name "*cont*"
./.local/lib/python2.7/site-packages/ansible/galaxy/data/container_enabled
./.local/lib/python2.7/site-packages/ansible/galaxy/data/container_enabled/meta/container.yml.j2
./.local/lib/python2.7/site-packages/ansible/modules/cloud/docker/docker_container.py
./.local/lib/python2.7/site-packages/ansible/modules/cloud/docker/docker_container.pyc
./.local/lib/python2.7/site-packages/ansible/modules/cloud/lxc/lxc_container.py
Is in Powershell:
PS C:\WINDOWS\system32> Get-ChildItem . "*cont*"
Directory: C:\WINDOWS\system32
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---s- 10/15/2017 10:21 PM containers
d----- 9/29/2017 5:41 PM MailContactsCalendarSync
-a---- 9/29/2017 4:41 PM 518 @WindowsUpdateToastIcon.contrast-black.png
-a---- 9/29/2017 4:41 PM 810 @WindowsUpdateToastIcon.contrast-white.png
-a---- 9/29/2017 4:41 PM 920064 AppContracts.dll
As you can see, we get additional file information about the desired files.
4. Get-Date => date
Pretty straightforward 🙂
5. Get-Command => which
This cmdlet will give you basic information about the command, function, or alias given as a parameter. Although typically, it will give you the type of command, name, version, and source module but not the location of the executable on the filesystem like in Linux.
PS C:\WINDOWS\system32> Get-Command get-help
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-Help 3.0.0.0 Microsoft.PowerShell.Core
These are some of the commands I have found useful.
What are your favorite Linux command equivalents in Powershell?
Originally published at http://udevops.wordpress.com on February 13, 2018.