Open CheatSheet Back To List Next Tutorial

Unix and Linux Introduction (Video Tutorial)

Unix and Linux Introduction

Intro

[0:00 Video timestamp]

This is the first in series of tutorials on the Unix operating system, including the Unix shell program to access and interact with it.

This tutorial covers:
  • Unix and Linux terms and concepts you should be familiar with.
  • What a shell is, and how to use it.
  • How to get help with Unix commands.
  • Useful Unix commands: copy, paste, last command, clear, history, find, new terminal window or tab.
  • How to launch other shell programs from the Unix shell.

It is recommended to open your system's shell program and follow along with the topics covered.

Read it, watch it, do it, review it:
  • There is both a written and accompanying video version of this tutorial. There are timestamps under the headings that align with the video version of the topic.
    • Read it: For each topic heading, First read the topic. 
    • Watch it: Then watch the associated section of the video.
    • Do it: Then follow the instructions to replicate the steps in your Unix shell program.
  • The Unix CheatSheet has a major category that aligns with this tutorial.
    • Review it: When you are done with the whole tutorial, open the CheatSheet and review the General category from this tutorial. Make sure you understand everything, and can refer back to the CheatSheet when you are working on your own projects in the future. 



What is Unix and where is it used?

Unix is a family of operating systems first published in 1971. It is used by the majority of servers, and command line interfaces. Computer programmers need to be comfortable using the command line and Unix commands.

Which machines use Unix?
[0:10 Video timestamp] 
  • Mac uses a Unix based operating system called Debian, and Macs ship with a Unix shell already installed.
  • If you are on a PC that uses a Unix based operating system, such as Ubuntu or Debian then this fully applies to you your machine. And it most likely has a Unix shell installed, probably Bash.

[0:36 Video timestamp]
  • If you are on a Windows machine then by default you don't have Unix installed. Windows PCs ship with a shell called Powershell which has a similar look and feel to a Unix shell but it's not a Unix shell.
  • However, Windows allows you to pretty easily install a Linux distribution such as Ubuntu, Debian or others using the Windows Subsystem for Linux. 
  • WSL lets developers run a Unix-like environment -- including most command-line tools, utilities, and applications -- directly on Windows, unmodified, without the overhead of a traditional virtual machine or dualboot setup.


The Unix Shell

[1:21 Video timestamp]


You use a shell application, like Bash or zsh (aka Z shell), to execute commands on a Unix or Unix-like operating system.

Shell terms
[2:48 Video timestamp]

There are many different terms for the shell that re used interchangably. You can refer to the Unix shell as the the Unix shell, the terminal, the console, the CLI, Bash or Z shell. While related, these terms mean somewhat different things. Let's go thru them one by one.

  • Shell: A command-line interpreter program that executes commands on the underlying program. A Unix shell interacts with the operating system's services. It is called a shell because it is the outermost layer around the operating system kernel.
  • Command-line interface (CLI): Interface for entering text-based commands. It provides a prompt letting you know it is ready for you to enter a command. Commands are sent by pressing enter.
  • Terminal: In the old days of computing, users had a monitor with a keyboard that connected to a mainframe (i.e., a thin client). Nowadays, the Terminal is a GUI application that emulates a terminal for interacting with the shell. It provides a command-line interface, as well as GUI features like a menu across the top to open new windows, copy/paste, etc. It doesn't process the input though. That is done by the shell.
  • Console: Essentially the same thing as terminal.
  • Bash and zsh (Z shell) - open source Unix shells.

Launch Unix Shell:
  • Mac: To launch the Unix shell on a Mac open the Terminal desktop application. To find Terminal, go to Launchpad, Open "Others" and amidst several utility type applications you'll find the Terminal App. Open it and it will open to your Unix shell. 
  • VS Code: If you are using the VS Code text editor, you can launch the shell by clicking on the Terminal menu and selecting New Terminal.

Test out the Unix shell by entering a few commands:
  • echo $SHELL - Returns the name of the shell program you are using.
  • uname - Returns the Unix operating system name.
  • whoami or echo $USER - Returns the current user's username.
  • pwd - Returns the present working directory (i.e., the name of the directory you are in).


Unix terms:

[5:14 Video timestamp]

Now that we talked about the shell, let's talk about what Unix actually is.

  • Unix: A family of operating systems first published by AT&T's Bell Labs in 1971. 
    • It has the following Characteristics:
      • It has at its core a centralized operating system "kernel" that manages system and process activities.
      • It is Modular: Non-kernel programs are organized into modular kernel-managed processes.
      • Shell: It has a "shell" that acts as an interface between the user and the kernel.
      • It has a hierarchical file system with the root / at then top, like an inverted tree with directories and subdirectories branching down from it.
      • It has Built-in networking.
      • Everything is a file. Programs are executed from binary files (1s and 0s). Scripts are run from script files. Etc.
      • Multiuser: Multiple users can log in and use the OS at the same time. That's why it's popular with servers and networking.
      • Multitasking: Multiple programs or processes can run simultaneously (i.e., it manages multiple threads).
      • Portable: It uses a high-level language meaning it's human readable, which, along with its modularity, makes it flexible and modifiable to adapt to multiple types of hardware and implementations.
  • Unix is written almost entirely in C which was also developed by Bell Labs at about the same time.

[6:58 Video timestamp]
  • The Kernel: is the central part of an operating system. It manages the core tasks of the computer including memory and CPU time, and Input/Output devices like keyboard, mouse, monitor, hard drive, printer. A computer user doesn't interact with the kernel directly. It runs behind the scenes. 
    • Critical kernel code is loaded on startup to a protected section of computer memory called the kernel space. Applications that are launched occupy a different area of memory called user space.
    • Different Unix variants use different kernel designs. 

[7:46 Video timestamp]
  • UNIX trademark and Single UNIX Specification: UNIX in all capital letters is a trademarked name. Originally owned by AT&T, it has since been transferred to a neutral industry consortium called the Open Group. The Open Group publishes a standard called the Single UNIX Specification (SUS). They offer a compliance check and certification which allows companies to use the UNIX trademark. Some prominent operating systems that are compliant and can therefore use the UNIX trademark include macOS, HP-UX (HP Enterprise), AIX (IBM), and Solaris (Sun/Oracle).

[8:32 Video timestamp]
  • Unix-like operating systems: Unix-like is the Unofficial umbrella term for operating systems that behave like a Unix system, whether they're certified as following the Single UNIX Specification or not. 
    • In terms of Market share: 
      • All mainframe computers use Unix-like OSs. 
      • Both the major smartphone OS's (iOS and Android) use it.
      • Approximately 70% of servers use it. The rest mostly use Windows. 
      • Desktops and laptop computers: PCs overwhelmingly use Windows, with only around 2% of PCs using some Unix variant. MacOS is based on a Unix variant called Darwin. 

[9:18 Video timestamp]
  • Linux: is A family of open source Unix-like operating systems built around the Linux kernel.
    • The Linux kernel is a reimplementation of the Unix kernel built from scratch. It was Created by Linus Torvalds and initially released in 1991. 
    • Linux is the leading Unix-like OS apart from macOS (Darwin). Android uses a Linux kernel.

[9:46 Video timestamp]
  • Darwin: A free open-source Unix-like operating system released by Apple in 2000. It makes up the core components of macOS and iOS.



Command Syntax


[10:01 Video timestamp] 

Now we are going to talk about the syntax for Unix commands.

The command format is the command name followed by the optional parameters: options and arguments.
  • command [-options] [arguments] 
    • The square brackets [] are just a notation to let you know they are optional. Don't put the square brackets in the actual command.

  • Command: The name of the command comes first.
  • Options (aka flags): Options modify how the command does its job.
    • -<character> Unix options use single dash and one character.
    • -<char1><char2> or -<char1> -<char2> Applies multiple options to the command. 
    • --option-keyword Some Unix variants allow long options. Use two dashes followed by a keyword.
  • Arguments: Arguments pass information to the program you want to run. There can be zero, one, or multiple arguments. Typically they specify the data upon which the program will perform an action.

Example: ls command
We will use the list command as an example. It is covered in detail in Unix File System tutorial.

ls The list command name is ls. It lists the files and folders in the current directory, excluding hidden system files.

ls -1 The -1 option (the number 1) puts each file and directory name on a separate line.
ls -a The -a option shows all files, including hidden system files. In Unix, systems files start with a dot and are hidden.
ls -1 -a or ls -1a Applies both the -l and -a options to the ls command.
ls ~/Documents The ~/Documents argument applies the command to the Documents directory instead of the current directory. Unix knows it is an argument and not an option because it does not start with a dash. 
ls -a ~/Documents You can use options AND arguments in the same command.

Multiple commands on the same line:
You can run multiple commands on the same line using one of the following:
  • cmd1 && cmd2 Separate the commands with && to run sequentially.
  • cmd1cmd2 Separate commands with semicolons.

Example: Run the change directory command (cd) and then the list command (ls) from one line:
  • cd ~/Documents && ls 
  • cd ~/Documents; ls 

When typing a command:
  • enter/return Press enter/return to execute the command. The cursor does not have to be at the end of the line. 
  • left | right arrow Use the left arrow to move the cursor one character to the left. Right arrow to move one character to the right.
  • Control+A Moves the cursor to the beginning of the line.
  • Control+E Moves the cursor to the end of the line.
  • up | down arrow Press the upArrow to to get the previously entered command. Press it multiple times to get earlier entered commands. DownArrow goes back down one command.
This can save you a lot of typing. You can up arrow to a previous command, then press enter to execute it again.
  • partial command + tab Unix tries to guess the rest of the command or filename.

Other notes on Unix syntax:
Case: Unix is case sensitive. As a general rule of thumb, use lower case where possible.
Quotes: You can use either single or double quotes around strings: echo 'Hello'; echo "Hello"
You must use double quotes when accessing a variable: echo "My user name is $USER"  (variables are covered in the environmental variables tutorial).
Single line Comments: Put a # sign in front of a comment # comment goes here



Help / Manual


[12:43 Video timestamp]

To get information on a command from the Shell's manual: 
man command_name Enter "man" and the command name. 
Example: man ls opens the manual for the list command.

If you have a Bash shell you can also type in help and the command name: help ls

The manual uses the Less paging program. Once you are in the manual, you can navigate using the following Less navigation commands:
  • enter or down Arrow key: scrolls down one line. upArrow scrolls up one line.
  • u scroll up half the screen, d scroll down half the screen. 
  • f | space key: forward (scroll down) one screen, b back (scroll up) one screen. 
  • g takes you back to the start of the help topic, G to end.

Less search commands:
  • /search-term Enter a search term.
  • n If there are multiple instances of the search term, n takes you to the next instance. 
  • N Takes you to the previous instance.

Quit: q to quit out of the manual.



Basic Commands

Now let's go over some basic terminal and Unix commands.

Terminal keyboard shortcuts:
We'll start with some basic keyboard shortcuts that are specific to the Terminal application, and may or may not work in other Console applications.
  • CmdOrControl+N Opens a new terminal window.
  • CmdOrControl+T Opens a new terminal tab on the existing terminal window.
  • CmdOrControl+F Finds a search string on your Terminal screen if it exists.
  • CmdOrControl+C Copy selected text.
  • CmdOrControl+V Paste.
  • Cmd+K Clear the screen. All previous lines are removed.

Unix Commands:
[13:14 Video timestamp]


  • history The history command returns a list of recent commands.
  • clear The clear command clears the screen. This will just scroll your screen so that all previous text is out of view, but it is still there if you want to scroll up or do a find on it.

User/Group/Host Info:
  • whoami Returns the current logged in user's id.
  • id -p Returns the current logged in user's id and groups.
  • users Lists the user ids of all users on the system.
  • uname Returns the name of the operating system.
  • hostname Returns the name of the current host system.

Some utility commands:
  • echo arg The echo command returns the value of the arguments. 
    • echo 'Hello' returns Hello
    • echo $USER returns the User_id. $USER is an environmental variable, which is covered in a later tutorial. 
  • date Displays the current date and time.
  • cal Displays a calendar. You can specify a year: cal 1982. Or specify a month: cal 01 2020

Run Other Shell Programs in the Terminal:
[15:22 Video timestamp]

  • program_name Starts the program.
    • Example: node Launches the Node.js shell program if it's installed.
    • Example: python Launches the Python shell program if it's installed.
  • Control+C Closes an operation in the program (without exiting the program).
  • Control+D Exits the program.
  • program_name --version Returns the version of the program if it is installed.
    • Example: node --version Returns the version of Node.js if it is installed.
  • which program_name Returns the executable file path which is called by the command.
    • Example: which node Returns the location of the node.js executable file if it is installed.

Open CheatSheet Back To List Next Tutorial