Unix Primer - Basic Commands In the Unix Shell

Unix Primer - Basic Commands In the Unix Shell

Original by Theoretical and Computational Biophysics Group, UIUC (https://www.ks.uiuc.edu/).

Used with Permission

Modifications: Prof. Zachary Wartell, UNCC

Revised: Thu Aug 31 20:48:03 2023 -0400 (git logs)

If you have no experience with the Unix command shell, you need to  to work through this primer.

Aside: By time of graduation, Computer Science students gain significant experience with various operating system (OS) shells. Extensive use typically occurs in introductory operating systems or intro. 'systems programming' courses. This tutorial is designed assuming the reader has not yet take such a course. The tutorial is minimalist in coverage and does not substitute for full coverage found in the mentioned courses.

Unix Shell

The shell is a command programming language that provides an interface to a UNIX operating system. The shell is an example of a Command Line Interface (CLI) as opposed to a graphical user interface (GUI).  The shell is also an example of a REPL programming language (read-eval-print-loop).   This tutorial presents basic commands to use within the UNIX shell.  Many different shells exists such as sh, sh, bash, zsh, etc.   This tutorial is biased to the bash shell, but most of the commands covered here are the same across different Unix shells used in different flavors of Unix.

Directories and Common Commands

8/31/2023: Re-organized into separate section and add comment about home directorycdShell Directory Hierarchy vs OS GUI Folder Hierarchy

Desktop, workstation and mobile computing systems present the user with a graphic user interface shell for interacting with the native OS.  Each GUI shell has it's own built application for exploring the OS's file system's directory (akka folder) hierarchy, and for copying and moving and opening files, etc. The relationship between the directory structure presented by a command-line OS shell, such as bash, and the native OS GUI presented hierarchy can vary from OS to OS, and potentially from shell to shell.

In most installations of a Unix style shell, such as bash, the shell initially opens in your home directory. A typical default bash shell will appear as shown below.   The $ is the input prompt.   Depending on the configuration of your shell additional, information may also be shown above the $ input prompt as shown below.  In the code example below, whenever the mouse cursor display an ?, you can click to toggle a popup window with a brief explanation of the code under the cursor.


zwartell@CCI3DH7LN3WLT MINGW64
This displays the name of the logged in user, the computer's name and the name of the shell
/c/Users/zwartell
This displays the current directory

$
$ is the input prompt symbol
ls
ls command (see [ls])
AppData/
'Application Data'@
Contacts/
Desktop/
Documents/
Downloads/
'Dropbox (Personal)'/
'Dropbox (UNC Charlotte)'/
Favorites/
Links/
'Local Settings'@
Music/
'My Documents'@
Pictures/
Recent@
'Saved Games'/
Searches/
SendTo@
'Start Menu'@
Videos/

In Microsoft Windows, your can start the File Explorer to display the current bash directory by typing the following command:

zwartell@CCI3DH7LN3WLT MINGW64 /c/Users/zwartell $ explorer .

The screen capture below shows Windows File Explorer that would popup as a result.

Screen_Capture

Basic Operations

Typically, when you start the shell it will open to your home directory (as in the example above).  Your home directory your individual file space on the file system contains your personal files which are separate from all the operating system and installed applications' files.

No matter where in the file system's directory structure you are, you can always get back to your home directory by typing:

$ cd

cd is short for 'change directory'. (See cd)

The symbol ~ is also a shorthand for your home directory.   So the following will also cd to your home directory.

$ cd ~

The command pwd will display the name of the current directory.

$ pwd
/c/Users/zwartell
From your home directory, create a new subdirectory named "primer" for working through this tutorial:

$ mkdir primer

You can remove an empty subdirectory with the following command (but don't do this right now):

$ rmdir primer

(Note: if you do remove "primer", please create it again.)

Now change to the "primer" sub-directory, making it your current working directory:

$ cd primer

Files

Files live within directories. You can see a list of the files in your "primer" directory (which should be your current working directory) by typing:

$ ls

Since you just created the directory, nothing will be listed because the directory is empty. Create your first file using a text editor.  Unix shells are typically installed with one of several common Unix text editors such as pico, ed, vi or vim.   If you are running your shell within an native operating such as Microsoft Windows or MacOS, you generally can start their standard editors by simply typing the name of the application followed by a file name you want to created.   In rest of this tutorial assumes you are running a Unix shell within Windows and therefore assumes you will use "notepad.exe" for editing files in this tutorial.  If you are using the shell within another native OS, replace the references to notepad with one of the native OS's common text editors.  (For native Unix we suggest using 'pico').

$ notepad first

The text editor opens,  If it's a native Unix text editor it will fill the entire console window.

For pico:  You can type text and move the cursor around with the arrow keys; the bottom of the screen presents the commands available. Type the sentence: "My first file." Then press "^O" (hold the left "control" key while pressing 'O') and "enter" to save the file, then "^X" (hold the left "control" key while pressing 'X') to exit pico. Now when you list your files, you will see file "first" listed: 

For another editor:   Enter the sentence: "My first file."   Save the file and exit the editor.

$ ls
first

You can view a text file with the following command:

$ cat first

My first file.

("cat" is short for concatenate - you can use this to display multiple files together on the screen.) If you have a file that is longer than your 24-line console window, use instead "more" to list one page at a time or "less" to scroll the file down and up with the arrow keys. Don't use these programs to try to display binary (non-text) files on your console - the attempt to print the non-printable control characters might alter your console settings and render the console unusable.

Copy file "first" using the following command:

$ cp first 2nd

(cp is short for 'copy').   By doing this you have created a new file named "2nd" which is a duplicate of file "first". The file listing reveals:

$ ls
2nd first

Now rename the file "2nd" to "second":

$ mv 2nd second

(mv is short for 'move') Listing the files still shows two files because you haven't created a new file, just changed an existing file's name:

$ ls
first second

If you "cat" the second file, you'll see the same sentence as in your first file:

$ cat second
My first file.

"mv" will allow you to move files, not just rename them. Perform the following commands:

$ mkdir sub
$ mv second sub
$ ls sub
second
$ ls
first sub

This creates a new subdirectory named "sub", moves "second" into "sub", then lists the contents of both directories. You can list even more information about files by using the "-l" option with "ls":

$ ls -l
-rw-r--r-- 1 username group 15 May 22 16:26 first
drwxr-xr-x 2 username group 512 May 22 17:11 sub

(where "username" will be your username and "group" will be your group name). Among other things, this lists the creation date and time, file access permissions, and file size in bytes. The letter 'd' (the first character on the line) indicates the directory names.

Next perform the following commands:

$ cd sub
$ pwd
/Users/username/primer/sub
$ ls
-rw-r--r-- 1 username group 15 May 22 16:55 second
$ cd ..
$ pwd

/Users/username/primer

This changes your current working directory to the "sub" subdirectory under "primer", lists the files there, then changes you back up a level. The ".." always refers to the parent directory of the given subdirectory.

Finally, clean up the duplicate files by removing the "second" file and the "sub" subdirectory:

$ rm sub/second
rm is short for 'remove'
$ rmdir sub
$ ls -l
-rw-r--r-- 1 username group 15 May 22 16:26 first

This shows that you can refer to a file in a different directory using the relative path name to the file (you can also use the absolute path name to the file - something like "/Users/username/primer/sub/second", depending on your home directory). You can also include the ".." within the path name (for instance, you could have referred to the file as "../primer/sub/second").

8/31/2023: Added sectionCommand History, Tab Completion, Cursor Keys

gi

Modern shells keep track of the history of your past commands.  Typically, hitting the up arrow key will replace the current input command with the previous successful command.  Repeatedly pressing the up arrow key will replace the  current input command line with command further back in your shell command history.    The down arrow will then move forward through the command history.

Hitting the tab key will generally attempt to guess at what word you want to enter next by taking in account the letters you have typed in so far for the next word on the command line.  This is particular useful when perform files commands.   For example typing:  ls Fo<tab-key> will list all the files in the current directory whose names begin with the two letters "Fo".

The left and right arrows will also move the cursor forward and back over whatever words you have typed in the at the current command prompt.   Ctrl-Left and Ctrl-Right move one word at a time.   The Home and End key also work.

Other Useful Commands

General Syntax (and Warning about Spaces)

In the simplest usage, the Unix shell uses the following syntax:

$ command_name argument1 argument2 argument3 ...

The command shell use spaces to separate all arguments. This means that if you need to enter an argument that includes spaces you must surround the argument with quotes. For example if you are referring to a file or directory name whose name includes spaces you must put quotes around the name:

$ mkdir "my directory"
$ cd "my directory"

Long Output: Paging

Often a command will generate text output that exceeds the number of lines visible on the console.  A console screen full of text output is often called a page.  Assuming the shell is running in window of operating system GUI, using the window's scroll bar is a partial solution.  As example, the following command recursively lists all files in the file system

$ find /

Most likely the above will generate a very long list of files.   To interrupt and terminate the command, hit Control-C.

When you anticipate a command will produce many pages of output, you can use the command, less, to display one page at a time.  As an example type:

$ find / | less

The | character (Unicode U+007C) is the pipe symbol.  In shell programming, | instructs the shell to pipe the output of the first command, e.g. find /,  into the input of the second command, e.g. less.   less then displays one page of output at a time, pausing at each page until you hit the space key.   When the end of the output is reached, less will pause again and display END.  At this point, pressing the q key quits less and returns to the shell prompt.  less has many other functionalities.  Section 5. Online Help discusses how to get details on a command.

Note, many Unix commands automatically use the less paging functionality when the command outputs multiple pages.

Other Common Commands

The current date and time are printed with the following command:

$ date
Thu May 22 17:39:04 CDT 2003

Remote login to another machine can be accomplished using the "ssh" command:

In OS X command shell, you can open file using the command "open".  This command will open the file with the application corresponding to the file type. For example

      $ open README.pdf 

will open the file README.pdf using Preview.  You can also open applications using the -a flag

      $ open -a Safari 

    or open a file using TextEdit with the -e flag

      $ open -e mytext.txt 

Online Help

In the bash shell typing a command following by --help will display description of the command and what arguments and options it can use.  For example:

     $ cp --help

A completion installation of the shell will include the "man" pages ("man" is short for "manual"). The information is terse, but generally comprehensive, so it provides a nice reminder if you have forgotten the syntax of a particular command or need to know the full list of options.  

For example to see the manual pages for the cp command:

     $ man cp

Some Unix style shell installations are minimalist designed to install quickly and for special purposes usage (git-bash is an example).   In such installations the man pages are not installed.   Web versions are readily available, however, such as https://man7.org/linux/man-pages/index.html.

If man pages are install, you can use the "-k" option to provide a list of commands that pertain to a particular topic. For instance, try:

$ man -k "copy files"

(and make sure to include the quotation marks). One of the commands listed should be the "cp" file copy command. Display the man page for "cp" by typing:

Summary Of Basic Shell Commands

$ pico myfile common Unix text editor to edit  file "myfile"
$ ls list files in current directory
$ ls -l long format listing
$ cat myfile view contents of text file "myfile"
$ more myfile paged viewing of text file "myfile"
$ less myfile scroll through text file "myfile"
$ cp srcfile destfile copy file "srcfile" to new file "destfile"
$ mv oldname newname rename (or move) file "oldname" to "newname"
$ rm myfile remove file "myfile"
$ mkdir subdir make new directory "subdir"
$ cd subdir change current working directory to "subdir"
$ rmdir subdir remove (empty) directory "subdir"
$ pwd display current working directory
$ date display current date and time of day
$ man -k "topic" search manual pages for "topic"
$ man command display man page for "command"
$ exit exit a terminal window
$ logout logout of a console session
$ ssh -l myname host remote shell login of username "myname" to "host"
$ scp myname@host:myfile . remote copy of file "myfile" to current directory