Hostwinds Blog

Search results for:


Command-line Interface: A Beginner's Guide Featured Image

Command-line Interface: A Beginner's Guide

by: Hostwinds Team  /  May 2, 2024


When you're managing a website, the command-line interface (CLI) is one of the most powerful tools at your disposal.

Understanding how CLI works and the components that make it  work can help you manage your hosting environment more effectively.

What is a CLI?

The command-line interface (CLI) is a text-based user interface (UI) used to interact with your computer's operating system—Instead of using a graphical user interface (GUI) with windows, icons, and buttons, commands are typed into a terminal to tell the computer what you want it to do.

Command-Line Core Components

CLI is comprised of three core programs working together that allow you to interact with your computer:

Terminal

The terminal is the application that provides you with access to the command-line interface. It's the window hosting the shell, where you type your commands and see the results. It goes by different names depending on the operating system—Terminal on macOS, Command Prompt or PowerShell on Windows, and various terminal applications such as GNOME Terminal or Konsole on Linux—but all perform the same role of providing a space to interact with the shell through the command line.

Command Line

The command line is the area within the terminal where you enter text-based commands to interact with your computer. When you open a terminal application, a prompt displays your current location in the computer's file system. Commands typed within the command-line are sent to the shell for processing.

Shell

The shell is the program that acts as a middleman between you and the operating system. When you type commands into the terminal, the shell interprets them, executes the tasks, and then shows the output in the terminal window.  Popular shell programs include Bash, Zsh, and Fish.

How CLI Commands are Processed

When a command is entered into the CLI, it goes through a series of steps that allow it to interpret, execute and display the information you requested.

Here's a step-by-step process that includes an example command with options and arguments to show how the command-line interface (CLI) process works from start to finish:

  1. Command Input: A command is typed into the terminal. Let's use the example command ls -l /home/user/Documents, which asks the system to list the contents of the /home/user/Documents directory in a detailed format.
  2. Execute Command: After typing the command, press the ENTER key to execute.
  3. Command Goes to Shell: The terminal sends the ls -l /home/user/Documents command to the shell for interpretation and processing.
  4. Shell Parses Command: The shell breaks down the command. In this case, ls is the command (to list files), -l is the option (long format for detailed listing), /home/user/Documents is the argument (the directory you want to list).
  5. Shell Searches Command: To ensure the ls command can be executed properly, the shell uses the PATH variable to search the system's directories and locate the executable file corresponding to the command.
  6. Shell Executes Command: Once the shell locates the ls file, it runs the program with the -l option and /home/user/Documents argument.
  7. Output Displayed: After the shell completes the task, it sends the output back to the terminal, displaying a detailed list of files and folders in the specified directory.
  8. Prompt Appears Again: Once the output is displayed, the terminal shows the prompt again, indicating that it is ready for your next command.

How to Access CLI on Windows, Mac and Linux

Depending on the operating system you use, there are different ways to open the command line interface.

Windows

To open the CLI terminal on Windows, you can go through either Command Prompt or PowerShell.

Command Prompt:

  • Open the Start menu and type "cmd" or "Command Prompt" in the search bar.
  • Click on the Command Prompt application in the search results to open it.

PowerShell:

  • Open the Start menu and type "powershell" in the search bar.
  • Click on the Windows PowerShell application in the search results to open it.

You can also use the shortcut Win + X and select either Command Prompt or PowerShell from the menu.

MacOS

To open the CLI terminal on macOS, follow these simple steps:

  • Open Finder and go to the Applications folder.
  • Click on the Utilities folder.
  • Find Terminal and double-click on it.

You can also use Spotlight Search (Command + Space) to quickly find "Terminal" and open it.

Linux

On Linux, opening the terminal can vary depending on the desktop environment you are using:

  • In most Linux distros, you can find the terminal in the system menu. Look for Terminal in the applications list and open it.
  • Many Linux distributions have a keyboard shortcut to open the terminal, such as Ctrl + Alt + T.
  • You can also search for "Terminal" in the search bar of your desktop environment and open the application from there.

Common Command-Line Interface Commands

There are multiple commands in CLI for executing nearly any task you could imagine. Formatting of the command may change depending what operating system you use, but the output will always be the same.

Here are some of the more common commands broken down by category and operating system:

File and Directory Management Commands

These commands are used to create, delete, modify, and navigate files and directories.

Command

Operating System

Windows

Mac

Linux

List Directory

dir

ls

ls

Change working directory

cd

cd

cd

Create new directory

mkdir

mkdir

mkdir

Delete files

del

rm

rm

Delete directories

rmdir

-r

-r

Copy file/directory

copy / xcopy

cp

cp

Move file/directory

move

mv

mv

Create empty file

copy NUL 'filename.txt'

touch

touch

Change file/directory permissions

icacls

chmod

chmod

Change file/directory ownership

icacls or takeown

chown

chown

Remove empty directory

rmdir

rmdir

rmdir

Networking Commands:

These commands allow you to configure and manage network settings, monitor network traffic, and troubleshoot network issues.

Command

Operating System

Windows

Mac

Linux

Test network connection

ping

ping

ping

Trace packet route

tracert

traceroute

traceroute

Display IP address

ipconfig

ifconfig

ip

Network stats

netstat

netstat

netstat

DNS lookup

nslookup

nslookup

nslookup

Display ARP (Address Resolution Protocol

arp

arp

arp

Display network diagnostics

netsh

netstat

netstat

System Information Commands:

These commands provide information about your system, including hardware, software, and configuration details.

Command

Operating System

Windows

Mac

Linux

System information

systeminfo

uname

uname

Disk usage for mounted file

wmic 'system' get

df

df

Disk usage by file/directory

dir

du

du

Memory usage

wmic 'system' get

free

free

System runtime since last reboot

systeminfo

uptime

uptime

Current host (computer)

hostname

hostname

hostname

CLI vs GUI

Both the command-line interface (CLI) and graphical user interface (GUI) have their strengths, but the CLI shines in several areas.

Efficiency

For those familiar with the CLI, it offers a fast and precise way to navigate and control your system. By typing simple text commands, you can quickly execute tasks with specific options and arguments.

Automation

The CLI is excellent for automating repetitive tasks through scripting. You can set up and run complex operations without needing to manually intervene, which can save you a lot of time and effort.

Lightweight

It uses fewer system resources like CPU and memory compared to a GUI, making it perfect for running tasks on servers or systems with limited resources, or when conserving resources is important.

Remote Access

The CLI is a preferred choice for managing remote servers and cloud instances over SSH (Secure Shell), and is particularly useful in headless environments where there's no graphical interface available.

Customization

The CLI allows you to customize your shell environment and create shortcuts for more efficient workflows. This direct interaction with files and underlying processes gives you a deeper understanding of how your system works.

Written by Hostwinds Team  /  May 2, 2024