CLI Commands: An Introduction With Examples

CLI commands are a quick and efficient way for users to accomplish computing tasks. Our expert offers an introduction to their use.

Written by David Koff
Published on Feb. 02, 2023
Image: Shutterstock / Built In
Image: Shutterstock / Built In
Brand Studio Logo

The command-line interface (CLI) is a powerful tool that can be used to interact with computers. Unlike a graphical user interface or GUI, the CLI is text-based. It’s accessed via an application called a terminal window that, once launched, allows users to enter text commands to perform a range of tasks or to access a variety of data.

The CLI is also known as the console interface, the terminal interface, and the command prompt. It’s most commonly used in the Unix and Linux operating systems, although it can also be used in Windows and the macOS as well.

Command Line Interface (CLI): What Is It?

The command-line interface (CLI) is a powerful tool that can be used to interact with computers. Unlike a graphical user interface or GUI, the CLI is text-based. It’s accessed via an application called a terminal window that, once launched, allows users to enter text commands to perform a range of tasks or to access a variety of data.

More From David KoffWho Started Bitcoin?

 

How CLI Commands Work

The CLI works by allowing users to enter commands into the command prompt. The operating system then interprets and executes these commands. The user can then enter additional commands or modify existing ones to achieve the desired outcome. Let’s review an example.

The macOS is a version of UNIX operating system, one of the oldest, safest, and strongest operating systems. Because UNIX is managed via the CLI, Mac users can take advantage of some powerful features.

A screenshot of a CLI terminal
Image: Screenshot by the author.

For example, if Mac users wish to learn how many user accounts are on their computers, they can simply open the Terminal app and type (#1 in the image above):

ls /Users

This will list all of the user accounts on any Mac (#2 in the image above). For additional information about those user accounts, users can instead type the following (#3 in the image above):

ls -la /Users

Doing this will provide information about each user account’s level of permission, folder size, the date it was created, and more. (#4 in the image above).

In this example, we’ve used the CLI to provide us with information about a part of the computer. Now, let’s try telling the computer the perform a task: turning off or turning on WiFi. In order to test if these commands actually work, however, we’ll need to first open our network preference panel.

Normally, to do this, we’d click on the Apple Menu -> System Preferences -> Network. Instead of using the Mac’s GUI to do this, however, let’s use the CLI. Open the Terminal app and type the following:

open /System/Library/PreferencePanes/Network.prefPane

Magic! Now that our network preferences are open, let’s enter each line of the following, one at a time, into the Terminal app. After pressing enter, watch what happens to the WiFi status in the network preference pane window. 

networksetup -setairportpower en0 off
networksetup -setairportpower en0 on

Magic, again! By the way, you can do exactly the same thing on Windows and on Linux.

 

Why You Should Use CLI Commands

Using the CLI is often more desireable than using a GUI for a handful of reasons, as the example above illustrates. Let’s examine why:

4 Benefits of CLI Commands

  1. Increased Efficiency: The command line interface is fast! It allows users to quickly and efficiently perform tasks by typing commands rather than navigating through multiple menus or windows. Computers work faster when they don’t have to draw a graphical user interface.
  2. Automation: The CLI allows users to automate tasks by writing scripts that can be executed with a single command. This can save time and effort when performing repetitive tasks. For example, I’ve taken the commands above for playing with the Mac’s WiFi and turned them into a script here. That script can then be run on my Mac or, if I were administering a fleet of Macs, deployed to all of those computers so that other users could take advantage of it.
  3. Remote Access: The CLI allows users to access and control remote computers, making it ideal for administrators who need to manage multiple systems. Systems administrators, for example, are hired by companies to manage hundreds or thousands of computers, often in many different buildings, states, or countries. The CLI allows those professionals to retrieve data from all of those computers almost instantaneously.
  4. Increased Security: The CLI is more secure than a graphical user interface because it requires users to authenticate themselves before executing commands. Additionally, some commands require a second authentication before performing deeper (or “root-level”) commands. 

 

Challenges and Risks of Using CLI Commands

Although it’s extremely powerful, the CLI also poses risks and challenges.

The first and most obvious one is the learning curve. Learning the basics of UNIX and Linux is just like learning another language. Users must understand many commands, modifiers, and syntax rules and then use them correctly in order for the true power of the CLI to be harnessed. This can be difficult, especially for non-technical individuals. 

The CLI is also vulnerable to security risks. In UNIX and Linux, users can request and gain something called “root-level” access to a computer. That level of access allows anyone using it to have complete control of the system(s) into which they are logged. That means, therefore, that both users and systems administrators must have safeguards in place to mimize or prevent abuse. Because that abuse can come both from within and from outside of an organization, a company’s network administrators must also be vigilant and only allow CLI access to high-level and trusted individuals.

Lastly, the CLI’s simplicity is due to the fact that it doesn’t require a GUI. That lack of visualization, however, can make understanding the output of a command difficult. Users must be fluent enough in the syntax and expressions of an operating system in order to understand what a command prompt or terminal reports back.

More in Software EngineeringPython Lists and List Manipulation Explained

 

Who Should Use CLI Commands

The CLI is incredibly useful for developers, system administrators, network administrators, and power users who need to quickly and accurately perform tasks on their computer. It’s also one of the preferred tools of both white hat (good) and black hat (malicioius) hackers. Perhaps this is why photos of the command line and code are featured so prominently in images about hackers

In truth, anyone can and should use the CLI as a way to work more efficiently on their computer. Programming knowledge isn’t required. Instead, users can learn small, reasonable chunks of relevant information over time. Users should begin with commands that look and sound like words and commands that they already know. 

Finding a file on a computer is a time-honored task. In the command line interface on a Mac, typing the command “find” begins the process of looking for a file or folder. This command, for example, will search the entire internal hard drive for any file or folder with the word “resume” in it:

find / -name "*resume*"

By contrast, this command will only search the folder associated with user account named “david”:

find /Users/david -name "*resume*"

Need to open a file? Try the open command:

open /Users/david/Desktop/BuiltIn-Infographic.jpeg

This will open the file named “BuiltIn-Infographic” located on my desktop with the application that is normally set to open image files. On my Mac, that’s the Preview app. Of course, I also just choose to open the Preview app from the CLI:

open /System/Applications/Preview.app

Beginning with the basics is the most enjoyable way to begin exploring the CLI and seeing how it might change the way you compute.

Explore Job Matches.