View on GitHub

cli

Command Prompt and Linux's Terminal commands.

Unix/Linux Terminal

Table of Contents

Opening the Terminal

Linux

Search for “terminal” in the apps menu in most cases. You might have to look up specific instructions for your distro. Default Keyboard shortcut: Ctrl + Alt + T

MacOS

Search for “terminal” in Spotlight. Keyboard shortcut: Control + Option + Shift + T

Windows

The in-built terminals on Windows (cmd and PowerShell) are not POSIX compliant, so you will need to do one of the following:

Keyboard shortcuts

Shortcut Function
Tab Complete file & directory names and to further iterate over files
Shift + Tab Go back to the previous names
or Ctrl + P Go to all the commands previously executed in that instance of the cmd, one by one
or Ctrl + N Go towards the recent commands
Ctrl + R Search your command history
Ctrl + / Jump words
Home or Ctrl Jump to the beginning and end of the line
End or Ctrl + E Jump to the end of the line
Ctrl + C Stop running processes in your terminal and bring back the prompt

File System

Let’s get a brief on the Linux file system. If you’ve used Windows, you would be familiar with C:, D: etc.
In Linux, directory structure starts with / symbol, which is referred as the root directory The ~(tilde) is shorthand for your home directory. Within your home directory, you will find common directories, such as Documents, Music, Video, etc.,

A Gist on the file system folders and their general functions.

Absolute and Relative Paths

An absolute or full path points to the same location in a file system regardless of the current working directory. To do that, it must start from the root directory.

An absolute path always starts with a forward slash (/), which represents the start of the filesystem.

Eg: /home/username is an absolute path.

By contrast, a relative path starts from some given working directory, avoiding the need to provide the full absolute path. A filename can be considered as a relative path based at the current working directory. If the working directory is not the file’s parent directory, a file not found error will result if the file is addressed by its name.

A relative path never starts with a forward slash (/).

A relative path can start with a

Eg: ../docs is a relative path

Basics of Linux/Unix Commands

Command Structure

The components of the command line are:

The general form of a UNIX command is: command [-option(s)] [argument(s)]

#only the command
clear

#command with option
ls -l

#command with argument
man pwd

#command with option and argument
rm -r project

Command Help

$ help pwd
pwd: pwd [-LP]
  Print the name of the current working directory.

  Options:
    -L  print the value of $PWD if it names the current working directory
    -P  print the physical directory, without any symbolic links

  By default, `pwd' behaves as if `-L' were specified.

  Exit Status:
  Returns 0 unless an invalid option is given or the current directory
  cannot be read.

Variables

Syntax: ${var_name} or $var_name

Pre-defined variables: $HOME, etc…

Custom variables:

$ foo=bar
$ echo ${foo}
bar

NOTE: Strings in bash can be defined with ' and " delimiters, but they are not equivalent. Strings delimited with ' are literal strings and will not substitute variable values whereas " delimited strings will.

$ name=selena

$ echo 'Hey ${name}!'
Hey ${name}!

$ echo "Hey ${name}!"
Hey Selena!

Basic Commands

Use pwd to print name of current/working directory

$ pwd
/home/username

Clear screen & command history

Changing directories

$ pwd
/home/username/

$ # providing an absolute path as argument
$ cd /etc
$ pwd
/etc

$ # to go back to previous working directory
$ # if there's a directory named '-', use './-' to go that directory
$ cd -
/home/username
$ pwd
/home/username

NOTE:

Package manager

Check for package updates

$ # To check if any packages on the system have updates
$ sudo apt-get update

$ # To update all packages
$ sudo apt-get upgrade

NOTE: Always run the update command before the upgrade command.

Install a package

$ sudo apt-get install <package_name>
$ # This might prompt you with a Y (yes) or n (no) option.

Remove a package

$ sudo apt-get remove <package_name>
$ # This might prompt you with a Y (yes) or n (no) option.

Run files on cmd

$ ./file_to_run

Killing a Program

Listing directory contents & directory structure

Changing Permissions

First character indicates the file type The most common are

The other 9 characters represent three sets of file permissions for ‘user’, ‘group’ and ‘others’ - in that order

Character Meaning Value File Directory
r read 4 file can be read can see contents of directory
w write 2 file can be modified can add/remove files in directory
x execute 1 file can be run as a program can access contents of directory
- no permission 0 permission is disabled permission is disabled
$ chmod 664 sample.txt

This means it will change the permission of sample.txt to rw-rw-r– i.e, user and group only have read and write permissions and others only have read permission.

Further Reading:

Creating directories

# mkdir [option] dir_name
$ mkdir new_dir

# Spaces in directory name
$ mkdir "New Folder"

# Creating multiple directories
$ mkdir {dir1,dir2,dir3}

# Creating intermediate directories without causing errors if they already exist
$ mkdir -p public/css/summary public/html

Creating files

touch command

Redirect operator (>)

cat command

echo command

Using text editors

$ ln --symbolic /actual/file/location.ext /symlink/location.ext

Redirecting output

Redirect to a file

Eg: ls -al > listings.txt will save the output of command ls -al is re-directed to listings.txt instead of your screen.

Note that if listings.txt already had some data, then this will overwrite the old data.

Eg: ls -al >> listings.txt will append the output of command ls -al is re-directed to listings.txt.

Redirect to another command

Editing a file

Vi

Vim

Nano

Terminal Multiplexers

Terminal multiplexer introduction

tmux

Reading file content

cat

less

tail

Copying and moving files

More details

Renaming files

mv command

rename command

Further Reading:

Deleting files & directories

Chaining commands

You can chain multiple commands using &, &&, |, || and ; operators.

& Operator

$ command1 &

&& Operator

$ command1 && command2

| Operator

$ command1 | command2

|| Operator

$ command1 || command2

; Operator

$ command1 ; command2

Gathering System Information

Monitoring System

Finding Strings and Counting Occurrences

grep

AWK

Disks, USBs and Files

Prompt Customization

Periodically Running Tasks

CRON Jobs

Making Requests from the Command Line

Curl