SysAdmin to DevOps: Enhancing Your Skills with the Command Line

Boost Your DevOps Career with this Command Line Primer

Hello Muser!

As is common in any endeavor, technical difficulties come up. In my case, writing this 2nd issue had some problems with the native text editor that took some time to get sorted out! BUT, it’s now resolved, and I’m doing everything in my power to consistently write and I wanted to thank you for your patience. With that, on to the topic du jour: The Command Line!

But I like Pointing AND Clicking

Me too buddy, me too. The command line, however, gives you some tools and a level of control that you’ll have a hard time finding with just a graphical user interface (GUI). If you’re a sysadmin/syseng working primarily in GUIs, the command line will level up your game as if it was pay-to-win. On a scale of “Why can’t I change this setting?!” to “I can think in the Machine Language”, the command line sits in a beautiful sweet spot. I have a 100% scientifically-backed diagram below to illustrate this:

We’re in the zone here

The command line, the terminal, the shell, or console, is a text-based interface to your computer. It's like having a text conversation with your machine, where you're the boss and the computer is your loyal, but too-literal, assistant.

----------

So why is it so important in DevOps work?

The diagram is funny (or at least, I think it’s funny) but the obvious question, for maximum control, is “Why don’t we write code to change settings directly in the OS?”. You can, but I try to keep in mind that my job as a DevOps engineer is to make things easier and smooth the path for software delivery. The command line utilities are a layer of abstraction that we, as engineers, can use to keep ourselves away from the fiddly bits of coding interactions with the OS directly.

In turn, we are the abstractions that keep software developers and users away from the fiddly bits of how to deliver or use that software!

Command line utilities allow us to script, test, and consistently deliver the same results every time, with less errors. In addition, we can use them in a modular fashion by combining them together, depending on the particular command line you’re using.

----------

Ok, I’m sold. How do I use this magical tool?

I’m not going to go into the details of using a command line for the first time here because I’m assuming you have a passing understanding of how to search for information online. There are lots of tutorials that can get you started but I’ll cover some of what I think are the most important points.

Arguments or Parameters

Arguments and parameters (used interchangeably) to a command usually take one of two forms: a positional or a named value.

Positional

A positional argument will depend on the location of the arguments after the original command. For example cp is the copy command and takes 2 positional arguments:

cp dir1/file1 dir2/file2

will copy dir1/file1 to the dir2 folder and name the file file2.

If you were to flip the position of the arguments i.e.

cp dir2/file2 dir1/file1

you’d be copying a file in the dir2 directory named file2 to dir1/file1 which may fail if dir2/file2 doesn’t exist! Keeping track of positional arguments is important to make sure the command does what you want!

Named Value

A named value argument will have a name of the argument followed by the actual value you want to assign to it:

kubectl get pod --namespace test-ns

In this example, the name of the argument is --namespace and the value you’re assigning to it is test-ns. This command is a Kubernetes CLI command that gets all pods in the namespace test-ns (more Kubernetes content in the future!).

Flags

The vast majority of commands will have ways to change the behavior of the command using what are called flags. These don’t change any arguments or parameters but instead specify a different way for the command to do its task.

Single Letter

A simple example is ls command.

ls -l

The -l flag changes the behavior of the ls command to output the directory's contents in the long format. You can usually combine single letter flags together like so:

ls -lh

This tells the ls command to output in long and human-readable format which includes things like easily readable filesizes in MB and GB rather than a massive number in bytes alone.

Not all commands will allow you to do this but if they do, the below is equivalent:

ls -l -h

Word

You may also see the convention of a full word with two hyphens like so:

ls --color

This tells ls to output using terminal colors rather than in monochrome.

ExplainShell is a great resource to learn about commands that you may see and does a great job explaining all 3 types of parameters and flags!

----------

Pipes

Pipes are the | character which you may see more often in scripts for bash or powershell. The concept is that the | character connects the output of one command to another like a pipe in plumbing. Here’s an example:

# Bash
# List all files in the current directory

$ ls -lh
total 0
drwxr-xr-x  2 dtang  staff    64B May 29 17:00 dir1
-rw-r--r--  1 dtang  staff     0B May 29 17:00 file1
-rw-r--r--  1 dtang  staff     0B May 29 17:01 searchterm1
drwxr-xr-x  2 dtang  staff    64B May 29 17:01 searchterm2

# Take the output of the ls -lh command above and 
# filter it using the grep command

$ ls -lh | grep searchterm
-rw-r--r--  1 dtang  staff     0B May 29 17:01 searchterm1
drwxr-xr-x  2 dtang  staff    64B May 29 17:01 searchterm2

In this examples, the 4 lines of output from the ls command are “piped” into the second command which only shows selected lines based on the search term. You can repeat this process too, chaining multiple pipes back to back to back: ls -lh | grep searchterm | grep May .

Once you learn multiple commands, you can use the pipe to combine and operate on command outputs in a staggering number of combinations!

----------

Scripting

Now that you’ve got some commands under your belt and know how to combine them, scripting is the next step. It may seem daunting but a script is nothing more than a sequence of commands saved into a file. Another example:

1 #!/bin/bash
2 mkdir testdir
3 cd testdir
4 touch testfile
5 echo testcontent > testfile
  1. This line tells the OS what interpreter to use (other options include #!/bin/zsh or #!/bin/python3 )

  2. Make a directory named testdir

  3. Move into that directory

  4. Create a file named testfile

  5. Write the word testcontent into the file named testfile

There’s a lot more syntax and interesting things that you can do with scripting that border on programming, but I’ll leave it to you to explore deeper with the tons of resources available online.

----------

Resources

This was a lot longer than I originally intended but there’s a lot of ground to cover. I’ll leave off with some links here for documentation and please comment or message me directly if these don’t cover what you need!

Until next time, keep learning and keep growing!

Darrell Tang

Reply

or to participate.