UNIX Essentials

Study Notes by Rahul Prabhudesai


This page is still in progress and new content will be added over time. Estimated completion is end of 2019, however, new content and/or edits will continue past completion.


1| Basic UNIX Commands

UNIX, like many operating systems contain a Command-Line Interface (CLI) which perform actions that are given through commands. Unlike some operating systems such as Windows, UNIX is case sensitive, and requires all commands to follow correct letter casing in order to execute.

Syntax for a command

$ command -option(s) argument(s)
$ ls -l /tmp

List of basic UNIX commands

Command & OptionDescription
$ man
$ whatis
Manual (Help) for UNIX commands
Display only the description from the manual page
$ uname
Displays information about the operating system.
Shows the name of the hardware platform.
Prints the hostname of the current system.
Shows the name of the operating system.
Shows the current operating system release level.
$ uptimeShows how long the system has been up for since last boot
$ topDisplays tasks and system statuses in UNIX
$ who
$ w
$ who am i
$ whoami
$ finger
Lists logged in user’s username, terminal no. and login time
Prints logged in users and what they are doing
Shows usernames of people currently at the terminal
Displays username associated with current (effective) user
Prints information about system users
$ cal
$ date
Displays a calendar for the current month and year.
Shows today’s date and current time
$ clearClears the terminal window.
$ ls
List files and directories
List all files and directories including hidden
Lists in long format
$ pwdPrint working directory shows current directory
$ passwdAllows the password to be changed
$ echo
$ banner
Simply prints whatever it is given
Prints characters in ASCII style big letters
$ bcA simple calculator
$ history
Shows an ordered list of all entered commands
Clears the current session history
$ mkdirMakes a new directory with provided name
$ touchCreates and opens a new empty ASCII text file
$ rm
Removes (deletes) selected file
Recursively deletes a directory and contained files
$ mv
Moves or renames files and directories
Display prompt if a file in destination may be replaced
$ cp
Makes a copy of files or entire directories to the destination
Recursively copies a directory and contained files
Display prompt if a file in destination may be overwritten
$ cdChange directory
$ alias
$ unalias
Makes a shortcut of any command
Prints the current values of all aliases
Removes the value for a specified alias
Removes all defines aliases
$ fileView the file type
$ catView contents and write to a file
$ chmod
$ chown
$ chgrp
Modify access permissions on a file using symbolic or octal modes
Change ownership and optionally the group ownership of a file
Recursively change ownership of a directory and contents
Change the group ownership of a file
$ umaskSet default permissions for new files and directories using octal
$ vi
$ view
$ viedit
Start the vi ASCII text editor
Open a file in read only mode
Open in read only mode; require save to new location
Open or create file with showmode enabled

Commands with arguments

$ command argument(s)
$ cal 7 2017

Entering multiple commands on a single line
The shell recognises a “;” semicolon as a command separator and executes commands from left to right once “Return” is pressed.

$ command -option(s) argument(s) ; command -option(s) argument(s)
$ uname -rs ; cal

Using online documentation
The “man” command displays information about the specified command and options. Press “q” to quit the manual page.

$ man command
$ man mkdir

$ whatis command
$ whatis mkdir

Using the echo and banner commands
The “echo” command simply prints whatever it is given. The “banner” command prints big characters in an ASCII style.

$ echo "character(s)"
$ echo "Hello World!"

$ banner "character(s)"
$ banner ":D"

Using the date command with formatting
The date command shows the current date and time. This can be formatted using the following syntax:

$ date +format
$ date +%d-%M-%Y
Abbreviated weekday name
Full weekday name
24 hour time
12 hour time
Day of year
Day of month

Using the change directory command
Users can change directory by using the “cd” command.

$ cd /destination
$ cd /home/documents
$ cdMoves to the home directory
$ cd /Moves current directory to the root directory
$ cd .Moves to the current directory
$ cd ..Moves into the parent directory

Using the alias and unalias commands
The “alias” command allows users to make a shortcut for a command. The “unalias” command removes the value of an alias.

$ alias name="command"
$ alias c="clear"
$ alias ll="ls -l"

$ unalias name
$ unalias c

2| UNIX File System

The UNIX file system is a methodology for logically organising and storing large quantities of data. In UNIX, the smallest unit of storage is a file. The base of UNIX is comprised of files for even device drivers such as keyboard, mouse, display, etc. The smallest unit of storage is a file.

All files in the UNIX file system are organised into directories which are organised in a tree-like structure.

Ordinary Files
Files that contain data, text, or program instructions which are located within a directory file.
Symbol: “-” (files)

Special Files
Used to represent real physical device with Input/Output (I/O) operations and also appear in a file system like any other file or directory.
Symbol: “c” (special files), “b” (block special files)

Files are stored in directories and are used to organise groups of files. This may contain various other types of files or directories.
Symbol: “d” (directories)

Symbolic Link
Symbolic links are used to reference another file. It is a text file comprising of the path to the file being referenced.
Symbol: “l” (links)

Directory Tree
The top level of the UNIX file structure is known as the “root directory”, which is represented by the “/” (forward-slash) symbol. Subdirectories under root include bin, dev, etc, lib, mnt, tmp and usr, most of which contain system files.

/binUser Binaries
Encoded files for storage and processing purposes.
/sbinSystem Binaries
Contains binary executables typically used for system administration or maintenance purposes.
/etcConfiguration Files
Contains configuration files for all programs as well as startup and shutdown scripts.
/devDevice Files
Contains files for terminal devices and any USB devices attached to the system
/procProcess Information
Contains information about system processes
/varVariable Files
Contains files whose contents are expected to grow, ie. logs, database files, etc.
/tmpTemporary Files
Contains temporary files created by the system and user which is deleted upon system reboot.
/usrUser Programs
Contains users’ programs
/homeHome Directories
Home for all users to store their data, ie. /home/carlos
/kernelKernel Components
Contains kernel components common to all platforms within a particular instruction set which are needed for booting the system.
/libSystem Libraries
Contains library files that support files in /bin and /sbin directories
/optOptional Add-On Applications
Contains add-on application from individual vendors
/mntMount Directory
Directory used for mounting file systems. Commonly used to access external devices such as USBs and Hard Drives.
/platformPlatform Definiton Files
Contains platform definition files
/volVolumes Directory
Disk volumes are mounted under this directory

Absolute and Relative Paths
Absolute path names start from the root directory, whereas relative path names begin from the current directory and are therefore relative to that directory.

# Absolute Path Name
$ /mnt/disk1/data/2019_pictures

# Relative Path Name (Given that current directory is "/mnt/disk1/data/")
$ 2019_pictures

Hidden Files
Certain special files in UNIX contain system information, and therefore are designed to be invisible by default. These files are called “hidden” files and are denoted by the “.” (dot) prefix.

File Command
Generally in the Solaris environment, the file type is not indicated by the extension unless it was generated by an application. The file command enables administrators to determine the type.

# Using the file command
$ file filename

The file command outputs one of the following:

  • Text – ASCII, english and command text as well as executable shell scripts.
  • Data – files created by programs.
  • Executable/Binary – executable files which are commands or programs.

Cat Command
The cat command is the short form for catenate. It is used to create a file and write outputs from commands to it. Press “ctrl+c” on a new line to exit. The more common use of the command is to view the contents of a file. It is advised to not attempt to read binary files as it is known to cause the terminal window to freeze.

# Write to file
$ cat > filename

# View contents of files
$ cat filename

3| File Permissions

Like any system, UNIX allows file access control, which enables users and administrators to define and control the permissions of files and directories. There are three levels of access permission which is the owner, group and others.

File Access Definitions

File Type
A single character defining the type of file, ie. “d”, “-“.

Symbols “r”, “w”, “x” and “-” outlining various permissions available to a file/directory.

PermissionDefinition for FileDefinition for Directory
r (read)The contents of the file can be read and copied.File names within the directory are visible, however, various details may be hidden.
w (write)The file can be modified and content can be written to it. Requires the read permission to function properly.Files can be added or removed from directory. The execute permission must be enabled for proper functionality.
x (execute)File can be executed and run as a process.The user can traverse within and change to the directory and potential access to files and subdirectories.
– (no permission)No read, write or execute permissions.No read, write or execute permissions.

Number of links within the file system which is pointing to the file or directory.

The user who created the file or directory.

A collection of users which can be given permissions as a whole.

The number of bytes used by the operating system to store the list of files in a directory.

The date which the file was last modified or written to. Alternatively, the “-u” option can be used to display the date it was last read or accessed.

The name of the file or directory.

Permission Option Definitions

Makes use of a combination of letters and symbols to add or remove permissions. It is also referred to as a relative mode.

An absolute or numeric mode which utilises a collection of numbers to represent file permissions.

Octal ValuePermissions

Conversion Table

00 + 0 + 0– – –No Permissions
10 + 0 + 1– – xExecute Only
20 + 2 + 0– w –Write Only
30 + 2 + 1– w xWrite and Execute
44 + 0 + 0r – –Read Only
54 + 0 + 1r – xRead and Execute
64 + 2 + 0r w –Read and Write
74 + 2 + 1r w xRead, Write and Execute

Change Mode Command
Allows access permissions of files and directories to be modified using either symbolic or octal modes.

# Set permissions for a file
$ chmod 754 myfile
$ chmod u=rwx,g=rx,o=r myfile
$ chmod ug+x myfile
$ chmod o-wx myfile

Default User Mask
The umask command can be used to change and set a default user mask. This mask is set as an octal value comprised of 3 digits. The default umask is 022, with file permissions being 666 and directory or executable file, 777. The umask sets permissions by subtracting the umask from default permissions.

# Set User Mask
$ umask 022

Set User ID
A special permission in UNIX allows ordinary users to execute programs as if it was done by the root user. It is useful for enabling system administration tasks to be performed without a requirement to gain direct access to the root account.

# Set user id
$ chmod 4755 filename # setuid bit is set with 4000 in octal mode
$ chmod u+s filename # lowercase "s" is added to user

Set Group ID
Group ID permissions is used in a similar manner to setting the user ID. This allows users in a certain group to have root execution permissions. This generally grants access to additional files.

# Set group id
$ chmod 2755 filename # setgid bit is set with 2000 in octal mode
$ chmod g+s filename # lowercase "s" is added to group

Sticky Bit Permission
The sticky bit permission prevents users from deleting files that they do not have ownership of in a shared directory. Generally, users with write permission for a file inherits the ability to delete that file, however, the sticky bit prevents deletion regardless of the file’s permission. Users without execute permissions to a file are not affected by sticky bit.

# Enable sticky bit on a file
$ chmod 1755 filename # sticky bit is set with 1000 in octal mode
$ chmod +t filename # lowercase "t" is added to group

4| Using Text Editors

Text editors are used to accomplish a variety of tasks ranging from writing shell scripts to customising the user’s work environment. A common use for them is for coding HTML web pages and computer programs. It is also used to modify system files.

The vi Editor

The vi or visual editor is an text editor to create or modify ASCII text files. ASCII is a format that consists of only characters and eliminates all formatting such as italics, underline, bold, etc.

This editor is very powerful as well as small in terms of system resource usage, and supports a large host of functions. The key distinguishing factor between vi and other editors is that unlike the name, there is nothing visual about it. It is a simple full screen window that allows characters to be written to a file without any mouse support.

The vi editor is a very crucial tool for system administrators as it may sometimes be the only available text editor. Furthermore, many remote server applications benefit from the availability and functionality of the vi editor.

# Starting up the improved visual editor
$ vim # vi improved

# Basic syntax of the vi command
$ vi filename

The vi Modes
Command mode is the default vi mode. Performing a command enters the edit mode.

Command ModeThe default mode for editing existing text. Commands are initiated from this mode, and pressing escape (esc) will enter this mode from other modes.
Entry (Input) ModeUsed to write new text. Using the “i” (insert), “a” (append) and “o” (open new line) commands will initiate entry mode. (Do not require return key to register command)
Last Line ModeUsed to save and quit vi editor. The colon (:) key will enter this mode, while colon+q (:q) followed by return will quit vi.