Danny Brown

A Blog on Code and Occasionally Other Things

Introduction to Unix: Exam Prep

Danny BrownMarch 3, 2020

I’m currently enrolled in an introductory Unix course at FSU. With the midterm approaching, the best way for me to cover the material is to write it down, and why shouldn’t I go ahead and publish those hard-written words? Of course I will do that!

General Concepts on Unix

Unix is a multitasking, multiuser operating system originally developed in the 1970s. The OS has four major components a user should know about:

  1. Kernel: This is the master control program, which schedules tasks and manages resources.
  2. Shell: This is the interface that interprets user commands and passes them to the kernel.
  3. File System: Information in the Unix OS is organized into files and directories (which themselves are specialized files).
  4. Utilities: Software provided by the OS also known as commands.

There are a bunch of different versions of Unix, including both free (GNU and Linux distributions such as FedoraCore/Red Hat, Ubuntu, and Debian) and commercial (SunOS, Solaris) versions.

We can use a Secure Shell (SSH) client, which encrypts data sent over what would otherwise be an unsecured network, to access a Unix system. In the course I’m taking, we are using Tectia (tcsh), but a number of other SSH client options exist. Commonly used shells identified in class are Bash, Bourne, C, Korn, Zsh, and Ion.

Files and Pathnames

In Unix’s file system, items are either files or directories. To determine if something is a file name or a directory, we can start with context clues – it is common practice to give files an extension preceded by a dot. However, Unix doesn’t require this.

We can use the ls shell command to get a list of items in our current directory. Depending on one’s shell, this will likely display directories as a different color and will also display a forward slash after the directory name, such as this: directory/.

For an even more detailed way to view, add a flag to the last command: ls -l. This will print the long listing of files, and in this case, the first column in each row is a list of permissions. That column will look something like this:

drwxr-xr-x

More on permissions later, but for now it’s good enough to know that the d in the first position indicates that the item in this row is a directory.

Traversing Directories

We can use the command cd to change the active working directory. Pathnames are formed by placing a forward slash (/) between each directory and the final file name being selected. One can use relative paths simply by considering the path from one’s currently selected directory. Absolute paths in Unix start with a forward slash (/), which alone indicates the root directory.

The following special symbols are highly useful with traversing directories in Unix:

  • / – Refers to the root directory – the top of our inverted tree within which all other files are stored. Also used to delimit paths, as already noted.
  • ~ – Refers to one’s own home directory.
  • ~username – Refers to another user’s home directory (based on the username of the user).
  • .. – Refers to the parent directory, i.e., the directory “above” the current working directory.
  • . – Refers to the current working directory.
  • \ – Used to escape special characters that have other meanings in Unix commands.
  • " " – Easiest way to handle spaces in commands is to enclose the relevant portion in quotation marks.
  • * – Matches any number of characters, including zero. i.e., *.py will select all files ending in a .py extension.
  • * – Matches exactly one character. i.e., b?t will select all files named “bat”, “bot”, “bit”, “bct”, etc.
  • [] – Used to match any one character included in the brackets, including ranges. i.e., 1[a-c].py will select 1a.py, 1b.py, and 1c.py.

Formatting Unix Commands

The basic formatting of Unix commands is fairly straightforward:

commandname [flags] [parameters]

The commandname typically refers to a program stored in /usr/bin.

Flags are special indicators that indicate various settings for the command in question. Syntactically, flags are typically a dash followed by one or more letters, with each letter indicating a different flag. The ls -l command I used above was a command with the -l flag, which is short for “long listing.” We can combine flags under one dash or put a series of single dashes and single letters. For example, the commands ls -l -a and ls -la are equivalent.

Finally, parameters are required or optional values needed by the command to perform the action correctly. Parameters often include filenames and pathnames and are separated by spaces. Use double quotation marks, as noted above, to handle paths or filenames with spaces in them.

With most Unix commands, we can add a --help flag to print help documentation for that command. Sometimes this is pretty simple documentation, such as the help command for cd:

~> cd --help
Usage: cd [-plvn][-|<dir>].

Finally, it’s worth knowing that one can type multiple commands on the same line by separating those commands with a semicolon (;). For example, one might want to create and move into a directory using just one line:

~> mkdir test ; cd test
~/test>

File Permissions

When discussing directories, I briefly touched on the first column of a ls -l command. This column defines permissions for each file and directory in a Unix system. When one calls a long listing, each file will get data that looks like this:

-rw------- 1 danny CS-Class 0 Mar 3 18:49 test.txt

Translated, each of this text refers to the following:

permissions number_of_linked_hard_links username groupname file_size_in_bytes month_edited day_of_month_edited time_edited file_name.txt

With permissions, there are always 10 characters in the first column.

We covered the first character above: if it’s a directory, that character will be a d; for a standard file, a -. (In researching a bit, I see that there are other options, such as character- and block-device files, but that’s seemingly a more advanced topic. Just knowing how to identify standard files and directories is good for now.)

The remaining nine characters can be broken into three groups: characters 2 through 4 are user permissions, 5 through 7 are group permissions, and 8 through 10 are “other” permissions – anyone with access to the system.

Within these groups of three, there are three different types of permissions: read, write, and execute. If a group has read permission, an r will appear in the position for that group: position 2 for user, 5 for group, 8 for other. Similarly, a w can appear for each group in positions 3, 6, and/or 9, and an x can appear in positions 4, 7, and 10. Therefore, a file that provided all permissions to all groups would have the following in the first column of a ls -l:

-rwxrwxrwx

What do these permissions mean? Put simply:

  • Read permissions allow a user to access and read the contents of a file. For a directory, the user can list the contents of the directory, looking at the filenames only.
  • Write permissions allow a user to write to or remove a file. For a directory, the user can add or remove files from the directory.
  • Execute permissions allow a file to be run as a program. We don’t actually execute directories, so this is more of a traverse permission.

Changing Permissions

There are two methods to changing permissions, both involving the chmod command. Let’s cover the hard way first, as it will quickly prove obsolete to the superior method (but is still expected to be known for exam purposes).

The Hard Way

Here’s the basic syntax:

chmod [who] [operation] [permissions]

  • who – this will be u (user), g (group), and/or o (other).
  • operation – this will be + (add), – (remove), or = (set).
  • permissions – this will be r (read), w (write), and/or execute.

Consider that we have the above permissions:

-rwxrwxrwx

Let’s say we want to remove all permissions for the other group. We can do this by using the following command:

chmod o-rwx filename.txt

The new permissions are:

-rwxrwx---

We can get more complex here as well:

chmod u=rwx,g-wx,o+r filename.txt

This will result in these permissions:

-rwxr--r--

The Easy Way

That’s fine and all, but there’s a much easier and quicker method. We’ll still use chmod and the filename, but instead of all the middle operations, we use a single three-digit number. The first digit handles read permissions for all groups, the second digit handles write permissions for all groups, and the third digit handles executables.

Each permission group is associated with a number: user is 4, group is 2, and other is 1. We then simply add those numbers to set permissions for that digit in our three-digit number. For example, giving all permissions to all users would use chmod 777 filename.txt. Giving no permissions to any users would use chmod 000 filename.txt. Giving read and write permissions to oneself but no permissions to anyone else would look like this: chmod 600 filename.txt. Here’s a few more examples:

-rw------- 1 danny CS-Class 0 Mar  3 19:49 test.txt
~/test> chmod 777 test.txt
~/test> ls -l
total 0
-rwxrwxrwx 1 danny CS-Class 0 Mar  3 19:49 test.txt
~/test> chmod 666 test.txt
~/test> ls -l
total 0
-rw-rw-rw- 1 danny CS-Class 0 Mar  3 19:49 test.txt
~/test> chmod 764 test.txt
~/test> ls -l
total 0
-rwxrw-r-- 1 danny CS-Class 0 Mar  3 19:49 test.txt

It’s hard to foresee a situation where it would be any better, easier, or faster to use the first method over this one, so I know which one I’ll be committing to memory past any exam.

Unix Text Editors

There are three major Unix text editors:

  1. nano – This is an updated version of the text editor pico (which is also still available). It is the most straightforward and easiest to learn.
  2. emacs – emacs offers a bit more power with the ability to download and use a GUI, but is more difficult to use. (In my opinion, its fatal flaw is that the typical use of the backspace key is performed by default with the delete key in emacs – very unintuitive. I’m personally least likely to use this editor. A good thing about emacs is that it has an undo command, which is fairly rare in Unix systems.)
  3. vi – Also known as “Vim”, this is a fairly powerful and sophisticated text editor that offers a plethora of keyboard shortcuts and commands to traverse text, make bulk changes based on patterns, move to specific places in a file, and more. There’s a lot of muscle memory involved, but vi’s proponents can often work more quickly in this editor than any modern GUI-supported text editor.

Here’s how to perform some basic functions in each of these editors:

Start the editor, to begin a blank file

  • nano opens a file with an empty buffer.
  • emacs opens emacs in “scratch mode”, with an empty buffer. This is not recommended in emacs as it will not prompt you to save your work in this mode, among other grief.
  • vi also opens a file with an empty buffer.

Open an existing text file in the editor

  • nano filename.txt opens a file named filename.txt in nano.
  • emacs filename.txt opens a file named filename.txt in emacs.
  • vi filename.txt opens a file named filename.txt in vi.

Edit/enter basic text (if this involves specific modes, like command versus input modes, know how to switch between them)

  • In nano, traverse using the arrow keys (and other available keyboard shortcuts), then simply type and delete where you want text to be.
  • emacs also uses the arrow keys (among other shortcuts) to traverse the document and insert text.
  • In vi, you will need to enter insert mode using the i key (or one of the other insert mode options). Once in insert mode, you use the arrow keys (and other shortcuts) to enter and remove text like the other editors. Press escape to exit insert mode.

Exit the editor

  • In nano, press control+x.
  • In emacs, first press control+x, which enters execute mode, then press control+c.
  • In vi, while in command mode, press the colon (:), then q, then the enter key.

Save a file under a specific filename

  • In nano, to save the file, one presses control+o (for “write Out”). The default name will be the open filename if one exists. If one does not exist or you want to save under a different filename, simply enter the filename in the input field after using this command.
  • In emacs, first press control+x, which enters execute mode, then press control+s. This will save a file under the current filename if one exists.
  • In nano, while in command mode, first press the colon (:), then press w, then the enter key. This will save the file under the same name as currently exists. To save under a new filename, use :w filename. To overwrite an existing file, use :w! filename.

Cut and/or copy text

  • In nano, use control+k to cut the entire line at the cursor’s location. To begin marking more than one line of text to cut, use control+shift+6 (^) to start marking text, then use the arrows to select the desired text. While there is not a copy feature in nano, you can use control+k (“kill”) to cut the text. With either method, if you would rather copy text instead of cutting it, simply paste it back in its original position and then also paste it where you wanted it copied (see the pasting method outlined below).
  • In emacs, control+k cuts text from the cursor to the end of its line. To mark more than one line to cut, use control+shift+2 (@) to start marking at the current cursor, then use the arrow keys to navigate, then use control+w to cut the marked text.
  • In vi, in command mode, you can use v to start marking text by the character or V to mark text by the full line. Once the desired text is selected, one can use d to cut the text or y to copy it.

Paste text (that has been copied/cut)

  • In nano, if you have cut/killed text using control+k, you can “uncut” that text (i.e., paste it back in) with control+u.
  • In emacs, cut text can be “yanked back” using control+y.
  • In vi, once you have text cut or copied, you can use p to paste after the cursor or P to paste before the cursor.

Input/Output Redirection and Piping

In computer programming languages, there are three standard streams that serve as input and output for communication channels: standard input, standard output, and standard error. These hold in a buffer input, output, and error messages that occurs during the execution of a program. By using special characters in our Unix commands, we can redirect input and output in various ways.

Output Redirection

Rather than printing the results of an operation to the shell, we can instead print them to a specified file. We do this using the > character followed by the desired filename. For example, a simple echo command will print a string to the shell:

~> echo "Hello, World!"
Hello, World!

We could instead put this in a file by using:

~> echo "Hello, World!" > hello.txt

Here we don’t get a response in the shell, because the input has been redirected to the file:

~> cat hello.txt
Hello, World!

This writes a new file in every case. To append to an existing file, simply use an additional > character:

~> echo "Hello Again, World!" >> hello.txt
dabrown@shell.cs.fsu.edu:~> cat hello.txt
Hello, World!
Hello Again, World!

Easy.

Input Redirection

Input redirection is useful when programming. Here’s a simple Python program that takes input from the user:

name = input("Enter your name: ")
print(f"Hello, {name}!")

When one runs this program in a Unix shell (assuming a Python interpreter is installed), the program will stop to prompt the user for input. Instead, we can use input redirection by using the < character. With any white space as a delimiter, Unix will input each argument in a named file in turn. Therefore, if we had a file named name.txt that contained only the text Danny, this is the result of running the following:

~> sample.py < name.txt
Hello, Danny!

Very cool.

Piping

A nice hack that allows one to take over standard input after all text in the specified file is used is a nice example of piping:

cat name.txt - | sample.py

Assuming there was a second input in our sample.py file, Unix would throw an error if we only had one piece of input text in our name.txt file. By using this syntax, we get to take over control of the input after all text is exhausted.

What is the | character? This is known as a pipe, and very simply, it takes the output of one command and sends it as input to a second command. So in our example above, we are taking the cat command, which prints the contents of a file, then piping that output as input into the sample.py program. It’s the - character that is important in returning control of the input to us, so don’t forget this!

A Command Table

Finally, I’m going to copy directly from my course notes a table of commands we are expected to know as well as flags we covered and explanations of what they do. This post is already rapidly approaching 3000 words, and this table is pretty self-explanatory with maybe a little research to clear up any confusion. So rather than type up another thousand words or two going through examples of each of these commands, enjoy this nice table instead:

Command Description Flag options
covered
You should
be able to
ls List files -l
-a
list the contents of a directory
do a “long listing” showing file details
list the invisible “dot-files” in a directory
cd Change directory change to another directory, using any style pathname
change to your home directory
cat Concatenate (to standard output) print contents of one or more files to screen
use with output redirection to concatenate files into a single file
more a viewer, to view file contents use this command to view a file
describe how it works
less a viewer, to view file contents use this command to view a file
describe how it works (and differs from more)
touch create a file, or update time stamp use this command to create an empty file
use this command to update a file’s time stamp
pwd Show current directory display the pathname of the current working directory
mkdir Make directory create a directory
rmdir Remove (empty) directory remove an empty directory
cp copy a file (or directory) -r copy a file to another filename
copy one or more files to another location
copy an entire directory with a single command
mv move a file (or directory) move a file or directory to another filename (i.e. rename it)
move one or more files to another location
move an entire directory to another location
rm delete (remove) -r delete one or more files
delete a directory and all of its contents<
wc Word count -c
-w
-l
-L
count the number of characters/words/lines in a file
print the length of the longest line in a file
passwd Change your password change your password
man Manual pages view the manual pages of other unix commands
date Date command print the current date
chmod Change permissions set permissions for files and directories,
using both forms of the command that were discussed
diff print the difference between two files -b
-w
-i
print the lines that differ between two text files,
with possible variations of treating multiple spaces as one,
ignoring any white space on lines, and/or
ignoring upper/lower case
cmp Compare two files byte by byte compare two files and print line and byte where first
difference occurs
gzip and gunzip compress/decompress a file gzip -d
(same as gunzip)
compress a file with gzip
decompress a file with gunzip
explain how this differs from the Windows .zip format
tar tape archive utility c
x
t
f
v
create a tar archive, packing up files and/or directories
unpack the contents of a tar archive file
list the table of contents of a tar archive file
describe the meaning of the flags given here

Color me prepared for this exam!

Posted In code
Tagged Unix

Post navigation

PreviousPrepping for the Python MTA Certification (Exam 98-381): Insights from the Official Practice Exam
NextTallahassee Neighborhood Latitude/Longitude List

Danny Brown

A Dev Blog with Some Tangents

About

Categories

  • code
    • APIs
    • Bash
    • CSS
    • Django
    • HTML
    • JavaScript
    • Python
    • S3
    • Selenium
    • Serverless
    • TypeScript
  • games
  • music
    • concert reviews
    • synthesizers
  • opinion
  • sports
  • tech
    • Bitbucket
    • Git
    • GitHub
    • MS Teams
    • WordPress
  • theater

Recent Posts

  • Open Pull Requests from the Terminal (One of My Favorite Dotfiles Scripts)
  • Dotfiles Script for a New TypeScript/Node Project
  • So I Told You to Go See a Broadway Play? Tips for Theater in New York
  • Build a Simple Microsoft Teams Bot Easily, No SDK Required
  • Creating a GUI for Conway’s Game of Life Using Pygame and Numpy

External Links

  • GitHub
  • LinkedIn

Recent Posts

  • Open Pull Requests from the Terminal (One of My Favorite Dotfiles Scripts)
  • Dotfiles Script for a New TypeScript/Node Project
  • So I Told You to Go See a Broadway Play? Tips for Theater in New York
  • Build a Simple Microsoft Teams Bot Easily, No SDK Required
  • Creating a GUI for Conway’s Game of Life Using Pygame and Numpy

Categories

  • code
    • APIs
    • Bash
    • CSS
    • Django
    • HTML
    • JavaScript
    • Python
    • S3
    • Selenium
    • Serverless
    • TypeScript
  • games
  • music
    • concert reviews
    • synthesizers
  • opinion
  • sports
  • tech
    • Bitbucket
    • Git
    • GitHub
    • MS Teams
    • WordPress
  • theater
Copyright © 2025. Danny Brown
Powered By WordPress and Meritorious