Basic UNIX Commands and Beginner Tutorials

How to Learn UNIX in 24 Hours: A Quick Start Guide to UNIX for Beginners


Do you need to learn UNIX fast?  Do you need to gain a quick understanding of UNIX basics for an upcoming job interview?  This quick start guide to UNIX, which is oriented towards absolute beginners, will provide you with a practical framework to follow for learning UNIX in a short period of time so that you can successfully navigate a server running UNIX and be able to confidently talk about key operating system concepts, commands and practices.

The challenge for most people when they first start learning UNIX is that it can be intimidating since it is quite different from operating systems they have worked with in the past, and also because they do not know exactly where to begin.  We hope that this guide will make UNIX less intimidating by guiding you through the key areas that will provide you with a solid foundation to start from.

Let's get started...

Connecting and Logging in to a UNIX Server

There are multiple interactive and non-interactive mechanisms for connecting to and engaging with a server running the UNIX operating system.  The scope of this guide is limited to interactive methods since non-interactive methods are typically more advanced concepts.

UNIX Command Prompt vs. Graphical User Interface
One common misconception about UNIX is that you can only interface with it by typing in operating system commands at what is called the command prompt or command line interface (CLI).

QUICK TIP: In UNIX, the command prompt (where you type in UNIX commands) is typically a "$" or a "#" by default.  It is, however, possible to change the default prompt to be whatever you would like it to be by updating the shell variable that defines the value for the prompt.

It is even possible to make the prompt dynamic so that the value displayed changes when change the directory you are working in, connect to another UNIX server, or even change to a different user on the current system.  (More on UNIX shell variables and other shell concepts later.)
It surprises many people when they learn that there are GUIs (graphical user interfaces) for UNIX, similar to what you would use on a computer running the Windows or Macintosh operating systems.  For the UNIX operating system, the GUI is the X Window System but is also called X-Windows, X11 or simply X.  Like the GUI for Windows or Machintosh, X-Windows provides an intuitive interface for working with the underlying operating system.


Learn UNIX: X Windows Graphical User Interface (GUI)
X Windows Graphical User Interface (GUI)

Returning to the non-GUI space, there are different methods for connecting to a server that is running UNIX.  The two most common methods are by using either a Telnet or SSH client.  These "clients" are simply programs that run on your desktop, laptop or mobile device just like any other program such as a web browser.

Most operating systems come with a Telnet client already installed, and Macintosh also has an SSH client you can use out of the box.  One of the more popular SSH clients for computers running Windows is the PuTTY Telnet/SSH client, which can be freely downloaded from the Web.


Learn UNIX: PuTTY Telnet/SSH Client
PuTTY Telnet/SSH Client Window

Logging in to a UNIX System
After you successfully connect to a UNIX system, you will need to log in using your system credentials - basically, a username and password.  The username and password are created by the person or group that manages the server, typically called the UNIX system administrator or system admin for short.  The username is often a variant of your real name, such as the first letter of your first name and then your entire last name, and the password should be changed as soon as you log in the first time.

QUICK TIP: In addition to your username and password, you will also need to know the host name (or IP address) of the server you are connecting to.  This is used by the Telnet or SSH client to connect from your computer to the UNIX system. 

Again, the system administrator for the server you want to connect to will provide you with this information.  (Every computer or device on the Internet needs to have a unique IP address so that connections can be established for sending and receiving data.)
After you successfully log in, using your system credentials, you will be placed at the UNIX command prompt.  This is where you will type in commands that are used by the UNIX shell to communicate with the operating system kernel.  What is the UNIX shell you ask?  Keep reading to find out...




The UNIX Shell

To provide you with some context for this section and the Basic UNIX Commands section that follows, consider the following visual representation of the UNIX operating system environment:


Learn UNIX: The UNIX Operating System Environment
The UNIX Operating System Environment

If it's not obvious, you are the "User" in this diagram.  Unless you can speak computer language, and you would probably not be reading this guide if you could, there needs to be a way for the operating system to understand what you want it to do based on the commands you type in at the command prompt.  This is handled by the UNIX shell that serves as an interface between you and the kernel, which is the heart of the operating system.

Okay, so what exactly is the UNIX Shell?
The UNIX shell, which is also referred to as a command line interpreter (not to be confused with the command line interface terminology previously used in this guide), basically translates the command you type in so that the computer will understand and perform the desired operation.

You should understand that the shell is just a program, meaning it's not a tightly integrated component of the UNIX operating system.  For this reason, users can select what shell they want to use (have loaded) when working on UNIX systems.  Two powerful and frequently used shells, which use very similar syntax, are the Korn and BASH shells.  Korn is more typically used on systems running UNIX and the BASH shell is the default on systems running Linux, which is an operating system that is very similar to UNIX.

Shell Initialization Files
When the UNIX system administrator creates your login account (think system credentials) he or she will either leave your shell set to the default shell for the variant of UNIX running on the server, or can change it if you prefer to use a different shell.  As part of the account creation process, files known as shell initialization files are typically copied to your home directory. 

These files are specific to the shell you are using, are invoked each time you log in to the system, and contain various customizations to your shell environment so that you do not have to retype them every time you log on.  Changing the command prompt for its default value, defining command aliases and setting other shell variables are all examples of common customizations.

Before introducing you to some basic UNIX commands, it would be good to discuss a few key concepts about the UNIX operating system.
 

Introduction to Key UNIX Operating System Concepts

At this point, you should now understand the following topics:

· methods for connecting to a system running UNIX
· logging in using the system credentials provided by the UNIX system administrator
· the UNIX shell
· and last but not least, what the UNIX command prompt is

Okay, you are logged in and sitting at the command prompt...so where exactly are you on the system and what can you do next?

Your UNIX Home Directory
When you successfully log in to a UNIX system, the operating system will place you in what is called your UNIX home directory.  This is a directory, also called a folder in the graphical user interface world, which is associated with (or owned by) your username and contains your personal files.  This includes the shell initialization files covered in the What Exactly is the UNIX Shell?  section above. 

The location of your home directory is defined in the same system-level file that contains the name, or more precisely the absolute path (paths will be discussed shortly), to the particular UNIX shell which is loaded each time you log on to the system.  This file is updated and managed by the system administrator.

This directory is also called your current or working directory...at least for the moment.  As soon as you enter a command that changes the directory (folder) you are in, the new location or directory becomes your current/working directory.  Simple, right?  Don't worry, things quickly become more interesting in the UNIX world.

Absolute and Relative Paths in UNIX
For the next key UNIX concept, absolute and relative paths, consider the following diagram of the UNIX file system...

Learn UNIX: The UNIX File System Structure
The UNIX File System Structure

The root directory, not to be confused with the privileged user account (username) called root, is the topmost directory in the file system on a UNIX server.  Although there will typically be another directory between the root directory and your UNIX home directory, let's say the rightmost "Directory" bubble immediately below the "Root Directory" (the third "Directory" bubble out of the three at this level) is your home directory and you have two subdirectories (folders) inside/underneath it.  The first UNIX subdirectory contains two files, the kind of files is not relevant at this time.

UNIX Absolute Path
If your current/working directory is your home directory and you want to list information about one of the files in the first subdirectory, you can do so either using what is called the absolute or relative path.  The absolute path would start at the "Root Directory" and list every directory, each separated by a forward slash ("/"), in the  path to the desired file.  In the diagram above, the absolute path would be /Directory/Directory/File.  (Yes, this is only an example and directories would not be called "Directory" and files would not be called "File" and each file within a directory needs to have a unique name.)

UNIX Relative Path
Now, the relative path is the path to the file relative to your current location in the filesystem.  This means that if the current/working directory is your home directory and you want to list information about one of the files in the first subdirectory using its relative path, it would simply be Directory/File.  It's important to notice that there is no forward slash before "Directory" in the relative path.  If a forward slash was included, it would then become an absolute path starting at the "Root Directory" for a File that is stored in the top-level directory.  The top-level directory, which is represented by the forward slash ("/") and nothing else is commonly called "root" but should not be confused with the root user account.

The UNIX Root User
The UNIX root user is also known as the superuser account.  The root account is used by the UNIX system administrator to manage the server, and system-level programs that continuously run on a system are run under (associated with) this username.  Since this account is used for managing all aspects of the system, someone with root access can read, update and delete files that are owned by other users.  Conversely, non-privileged users cannot read, update or delete files owned by root unless they are explicitly granted the privilege to do so.

Enough of the conceptual...it's time to learn some basic UNIX commands!!




Some Basic UNIX Commands for Getting Started

Even if this beginner's guide to UNIX included every UNIX command available it would not do you much good since repetitive use of a command is the best method for learning the commands, and for learning UNIX in general.  For most, learning UNIX commands is often a learn them as you need them approach.

With that said, it is good for you to know a core set of basic commands so that you can at least navigate your way around your home directory, maybe create and remove some new directories, list the contents of your directories, and exit the system cleanly when you are done.  Since some of these command require at least one argument for them to work correctly, a quick overview of UNIX command options and arguments is necessary.

UNIX Command Options and Arguments
For each UNIX command, there are typically different options and arguments that are used with the command.  A command option is usually, but not always, prefaced with a hyphen ("-").  Options cause the command to behave in a way that is different from its default behavior (how it runs when no options are included on the command line). 

Command arguments, which are not prefaced by a hyphen, are objects that the command operates on or manipulates in some way.  There are commands that require an argument, some that will run with or without an argument, and others that do not require an argument at all.

Uppercase vs.  Lowercase
Everything in UNIX is case sensitive, so typing in the date  command at the command prompt to display the system's date and time is not the same as typing in DATE.  If you type in the latter, you will receive an error.  Although commands are almost always lowercase, uppercase is used for things such as shell variable names and programming variable names.

Learning Your First UNIX Commands
Again, we will assume you are successfully logged in to the UNIX server and sitting at the command prompt.  You assume that you are in your home directory, but where exactly is that?  The pwd command (print working directory) will provide the absolute path to your current location:

$ pwd
/home/lfl03b
$
The output in this example indicates this user's (lfl03b) home directory is in another directory named "home" which sits at the root directory level.

To find out what files, including directories, exist in your home directory you would use the ls command (list contents of directory):

$ ls
contacts_work  datafiles  demo_ksh_array_shell_script  perl5
$
The ls command output lists four files in lfl03b's home directory, but doesn't tell us much about the files.  At the most basic level, we don't even know if they are a file or a directory.  Adding the -l option to the ls command will display a listing using the long format:

$ ls -l
total 14
lrwxrwxrwx 1 lfl03b users    27 Aug  8 09:59 contacts_work -> datafiles/contacts_work.txt
drwxr-xr-x 2 lfl03b users  2048 Apr  9 15:04 datafiles
-rwxr-xr-x 1 lfl03b users 10130 Apr  9 15:02 demo_ksh_array_shell_script
drwxr-xr-x 2 lfl03b users  2048 Apr 10 09:29 perl5
$
In addition to knowing if a file is a directory (look for "d" at the beginning of each line), the UNIX ls -l command also indicates who has read, write or execute permission for each object (line item), what username (lfl03b) owns the object and what group do they belong to (users), the size of the object, when the object was last updated and obviously the name of the object itself.

From the sample output, you'll notice that datafiles is a directory - again, indicated by the letter "d" at the start of the output line for datafiles.  To move into the datafiles directory (in other words, change our current or working directory to datafiles), the cd command is used:

$ pwd
/home/lfl03b
$ cd datafiles
$ pwd
/home/lfl03b/datafiles
$
The pwd command was used to display the current working directory both before and after the cd command is run.  Notice that "datafiles" was the argument used with the cd command.

There are many different methods to return to our home directory (that is what makes UNIX both interesting and challenging at times).  To keep things basic, we will simply use the cd command again but this time without using (passing it) an argument:

$ pwd
/home/lfl03b/datafiles
$ cd
$ pwd
/home/lfl03b
$
If you wanted to create a new UNIX directory, the mkdir (make directories) command is used:

$ ls -l
total 14
lrwxrwxrwx 1 lfl03b users    27 Aug  8 09:59 contacts_work -> datafiles/contacts_work.txt
drwxr-xr-x 2 lfl03b users  2048 Apr  9 15:04 datafiles
-rwxr-xr-x 1 lfl03b users 10130 Apr  9 15:02 demo_ksh_array_shell_script
drwxr-xr-x 2 lfl03b users  2048 Apr 10 09:29 perl5
$ mkdir learn_unix
$ ls -l
total 16
lrwxrwxrwx 1 lfl03b users    27 Aug  8 09:59 contacts_work -> datafiles/contacts_work.txt
drwxr-xr-x 2 lfl03b users  2048 Apr  9 15:04 datafiles
-rwxr-xr-x 1 lfl03b users 10130 Apr  9 15:02 demo_ksh_array_shell_script
drwxr-xr-x 2 lfl03b users  2048 Aug 14 10:44 learn_unix
drwxr-xr-x 2 lfl03b users  2048 Apr 10 09:29 perl5
$
To remove a UNIX directory, use rmdir (remove directory) and pass the name of the directory to be removed as the argument:

$ ls -l
total 16
lrwxrwxrwx 1 lfl03b users    27 Aug  8 09:59 contacts_work -> datafiles/contacts_work.txt
drwxr-xr-x 2 lfl03b users  2048 Apr  9 15:04 datafiles
-rwxr-xr-x 1 lfl03b users 10130 Apr  9 15:02 demo_ksh_array_shell_script
drwxr-xr-x 2 lfl03b users  2048 Aug 14 10:44 learn_unix
drwxr-xr-x 2 lfl03b users  2048 Apr 10 09:29 perl5
$ rmdir learn_unix
$ ls -l
total 14
lrwxrwxrwx 1 lfl03b users    27 Aug  8 09:59 contacts_work -> datafiles/contacts_work.txt
drwxr-xr-x 2 lfl03b users  2048 Apr  9 15:04 datafiles
-rwxr-xr-x 1 lfl03b users 10130 Apr  9 15:02 demo_ksh_array_shell_script
drwxr-xr-x 2 lfl03b users  2048 Apr 10 09:29 perl5
$
The last command is used to exit an UNIX system cleanly.  Cleanly meaning it does not leave any system resources associated with your login session and username hanging around after you disconnect.  This one is system, just type exit at the command prompt and your login session will be terminated.

$ exit

Getting Help - The UNIX Man Pages

It is good to know that help for UNIX commands is readily available on right on the system you are working on.  You do not even need to switch to a browser window to use a search engine to get help.  This is a good thing since many times you may not have easy access to a web browser when working on a system running UNIX. 

The "man" command, which displays pages from the reference manual, is used to view detailed help for a UNIX command:

$ man pwd
Running this command will display a full-screen help page similar to the following output, which is only an excerpt from the actual man page for the pwd command:

PWD(1)                           User Commands                          PWD(1)

NAME
       pwd - print name of current/working directory

SYNOPSIS
       pwd [OPTION]...

DESCRIPTION
       Print the full filename of the current working directory.
<end of excerpt from pwd man page>

When you run the pwd command, you will notice that only one screen of information is displayed at a time.  Pressing the space bar on your keyboard will advance to the next page of output, and press the "Q" key on your keyboard will return you to the UNIX command prompt.  (Think of "Q" for quit.)




Do you need to learn UNIX, including how to read and write shell scripts, in 24 hours? If you are ready to master the basics and beyond our UNIX and Linux Operating System Fundamentals course, which allows you to practice what you learn on a real server, is a great place to start your UNIX adventure.

This comprehensive course on the fundamentals of UNIX, which includes a very good "Introduction to UNIX Shell Scripting" module, should be taken if you are new to the UNIX operating system environment or need a refresher on key concepts.

Hands-on lab system exercises are used throughout the course to re-enforce key concepts, and any questions you have while taking the course are answered by an experienced UNIX technologist.

Studying UNIX guides and tutorials is a terrific first step towards learning UNIX, but there is no substitute for being able to practice UNIX commands online while you work through the material.

Thanks for reading, and best of luck with your noble endeavor to learn UNIX!!!