DEV Community

Cover image for Command Line 101
Akshat Mittal
Akshat Mittal

Posted on • Updated on

Command Line 101

The Command Line Interface, or CLI, is a text-based interface that allows users to interact with a computer and its operating system through textual commands. It provides a direct line of communication with the system, enabling users to execute commands by typing specific instructions.

In this small series, we will explore the basics of CLI, the essentials, and some techniques.

Installation

All the experiments performed in this blog are executed in the Linux Environment.

Linux

All Linux distros come with an internal terminal which by default uses bash shell. You are free to try more shells like, Hyper Terminal, Fish, OhMyZsh (An Open source alternative for Zsh), etc.

Windows:

  1. Approach 1: Install git, along with that a utility git bash gets installed which creates a bash environment, the environment used by the Linux machines.
  2. Approach 2: Windows Powershell is a rather powerful software and is capable of running all (if not more) commands of the bash environment.

MacOS:

  1. Instead of a bash shell, Mac uses a zsh shell. It runs on a UNIX environment, which is mostly a Linux-like environment that makes sure that all of the commands of the bash environment are executable in Mac too.

Prompts and Symbols

Several prompts and symbols are used in the CLI, to denote separate things, some of them are listed below:

  1. $: denotes shell environment
  2. #: denotes that the current user is the root user
  3. ~: denotes the Home Directory
  4. /: denotes the root directory
  5. .: refers to the current directory
  6. ..: refers to the previous directory

Structure

Most CLIs are designed with the following structures:

  • <command> [arguments] [options]
    • Example: ls /home/dir --all
  • <program> <command> [arguments] [options]
    • Example: git fetch origin main --depth=10
  • <program> [arguments] [options]
    • Example: docker -v

Image description

Image description

Arguments

Arguments in a CLI allow users to send data to the application, sometimes in a command context. Arguments need to follow a specific order to be executed correctly.

Options

Options are named parameters that can be passed to a command and are represented by key-value pairs. Unlike arguments, options do not need to be in specific order.

Flags

Options that don't require a value are called Flags. They are boolean, meaning their presence indicates true and their absence is false.

Navigation

Folders (also known as directories), can be navigated from the CLI itself.

pwd

To check which directory you are in, use the pwd command.
pwd stands for 'Print Working Directory'. This returns the absolute path of the current directory, from the root directory.
For example, if your username is paul-norway, initially the output of the pwd command is /home/paul-norway.

ls

ls stands for list. This command is used to list the contents of the current working directories.

Image description

This ls command here, lists down the contents of the snap directory.

ls -a

An option -a can be used with ls which shows all the hidden files and folders.

cd

cd stands for Change Directory. This command is used to navigate from one directory to another. cd commands work on both relative and absolute paths.

  • To enter a directory in the current location, use cd directory-name/ or cd ./Documents/.
  • To enter a directory relative to the home, use cd ~/Documents/.
  • To enter a nested directory, use cd Documents/legal/case-2401.
  • To use the top-level directory, (previous directory), use cd ../Documents. The .. denotes the previous directory

Managing Directories

  • To create a directory, use the mkdir <dir-name> command. For example, mkdir banners.
  • To remove an empty directory, use the rmdir <dir-name> command. For example, rmdir banners.
  • Note: rmdir does not work for non-empty folders.
  • To remove non-empty folders, use rm -rf <dir-name> instead.
  • Warning: This is a dangerous command, use it carefully since the deleted content does not go to the trash bin, instead is deleted completely.

Some useful tips and tricks

MAN Pages

Most of the commands and programs come with a manual page entry that you can view using the man command. The manual pages keep the documentation for the command where every option, argument, and usage is written down.

For example, to read more about the ls command

man ls
Enter fullscreen mode Exit fullscreen mode

Image description

--help flag

Most commands give a --help flag which can tell the usage of the command easily.

ls --help
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)