Terminal

Terminal Emulation

Terminal emulation is negotiated between your client machine and the UNIX server.

Some Environment Variables

Special Control Keys

Shell and its Environment

Shell: User interface to the operating system.

Files can configure the environment upon login, e.g.,

Things you can do with a shell:

$ cowsay -f bong "I love systems programming"
 ____________________________ 
< I love systems programming >
 ---------------------------- 
         \
          \
            ^__^ 
    _______/(oo)
/\/(       /(__)
   | W----|| |~|
   ||     || |~|  ~~
             |~|  ~
             |_| o
             |#|/
            _+#+_

The UNIX Prompt

After you log in and the shell startup files have run, the shell will display a prompt.

Your Home Directory

Note: Hidden files begin with . and don’t show up by default.

Commands

Standard Command Format

Format: command [options] <arguments>

Note: Commands are case-sensitive, and spaces must be inserted between commands, options, and arguments.

Manual Pages (man)

Format: man <command>
Format: man -k <keyword>

man: Utility to display the manual page for a shell command, system program, library function, etc.

Set Terminal Type (stty)

stty: Command to set terminal type options.

Examples:

# List all terminal settings
stty -a
# Make the erase key <Ctrl>h
stty erase ^h

More Commands

On User Information

On Communicating with Others

On Manipulating Files and Directories

More on ls (list files in a directory):

Format: ls [-alRF...] <file-list>

More on cp (copying files):

Format: cp [-ir...] <file1> <file2>
Format: cp [-ir...] <file-list> <directory>
Format: cp [-ir...] <directory> <directory>

Examples:

# Change directory to parent
cd ..
# Change directory to current working directory
cd .

More on mv (moving/renaming files and directories):

Format: mv [-i...] <file1> <file2>
Format: mv [-i...] <file-list> <directory>
Format: mv [-i...] <directory> <directory>

More on rm (deleting files)

Format: rm <file-list>
Format: rm -r <directory>
Format: rm i <file>

More on file (display file type)

Example:

# Determine file type
file *

On Viewing Files

Note: head and tail both default to x=10

On Misc.

Tip: You can use ! to execute commands from history.

# Execute 6,000th command in history
!6000
# Execute previous command
!!
# Execute previous command that began with "echo"
!echo

bc Features:

Example:

$ echo "10-5" | bc
5

Example: Using xargs

# Remove all pdf files in /tmp
find /tmp -name "*.pdf" | xargs rm
# Print disk usage of man utility
which man | xargs du

tar

Format: tar [-cxzvf...] <archive> <file>

tar: Utility to creates a tape archive and can also compress with gzip.

Example: Creating a compressed archive of your home directory

$ tar -czvf archive.tar.gz ~

Process Subsystem Utilities

Example: Using ps

$ ps
  PID TTY          TIME CMD
 2937 pts/13   00:00:00 zsh
 2949 pts/13   00:00:00 zsh
 2950 pts/13   00:00:00 zsh
 2959 pts/13   00:00:00 gitstatusd-linu
 2993 pts/13   00:00:00 sleep
 5314 pts/13   00:00:00 ps
 5315 pts/13   00:00:00 xsel
$ kill 2937
$ ps
  PID TTY          TIME CMD
 2949 pts/13   00:00:00 zsh
 2950 pts/13   00:00:00 zsh
 2959 pts/13   00:00:00 gitstatusd-linu
 2993 pts/13   00:00:00 sleep
 5314 pts/13   00:00:00 ps
 5315 pts/13   00:00:00 xsel

kill signals:

Sending Processes to the Background

Method 1. Ampersand (&):

Add an ampersand (&) to the end of a command to make it run in the background.

Example:

sleep 1h &

Method 2. bg and fg:

If you have a process in the foreground you’d like to send to the background, suspend it with <Ctrl>z.

Then, run bg to continue the process in the background.

Shell Metacharacters

SymbolMeaning
>Output redirection (overwrite)
>>Output redirection (append)
<Input redirection
*File substitution wildcard, zero or more characters
?File substitution wildcard, one character
[]File substitution wildcard, any character between brackets
cmdCommand substitution
$(cmd)Command substitution
|The pipe
;Command sequence
||OR conditional
&&AND conditional
()Group commands
&Run command in the background
#Comment
$Expand value of variable
\Prevent or escape interpretation of next character
<<Input direction

Examples: Output redirection

$ echo "hi" > file.txt
$ date -u > file.txt
$ cat >> file.txt
Dave: Open the pod bay door, HAL.
HAL: I'm sorry Dave. I'm afraid I can't do that.
^D

Example: Input redirection

cat < file.txt

Examples: File substitution wildcards

$ ls –l foo*
$ ls –l foo?
$ ls –l foo[1-3]
$ ls –l foo[23]
$ ls [!f-z]???

On Escaping Metacharacters

Warning: Forgetting to escape certain metacharacters can have disastrous results (e.g., accidentally expanding a * in a rm command)

There are three ways to pass metacharacters without interpreting them:

  1. Backslash (\): Put a backslash in front of them.
  2. Single Quotes (''): Surround a string with single quotes to protect all characters except the backslash
  3. Double Quotes (""): Surround a string with double quotes to protect all characters except the backslash, dollar sign, and grave accent.

Remember: The difference between single and double quotes is that we can do expansion with $ and ` in double quotes.

Example: Escaping metacharacters in three different ways

$ echo 5 \> 3
5 > 3
$ echo '`date`'
`date`
$ echo "`date`"
Thu Dec 28 04:16:54 PM PST 2023

On Variable Expansion

Remember: You can view environmental variables with env

You can expand the value of variables with $.

Example: Variable expansion

$ echo $TERM
xterm-256color
$ my_variable="hello" && echo $my_variable
hello

On Command Sequences

Two ways to control the order commands are executed:

  1. Semicolon (;): Executes left-to-right, e.g.,
$ date; pwd; ls
  1. Parenthesis (()): Groups commands together, e.g.,
$ (date; pwd; ls) > out.txt

On Conditional Execution

We can use the exit codes of commands to conditionally execute other commands.

Examples: Conditional execution

$ gcc hello.c && ./a.out
$ gcc hello.c || notify-send "Compilation failed"

On Command Substitution

`command` and $(command) gets replaced by the output of command in the prompt.

Example: Command substitution

$ echo `date`
Thu Dec 28 01:31:19 PM PST 2023
$ echo date # Same as above, but without command substitution
date

A Simple UNIX Trick

Running ^x^y in the terminal will run the previous command with all instances of x replaced with y.

$ ly
Command not found
$ ^y^s
foo.txt bar.txt

Auto Completion in Shells

Most shells can complete a filename, command name, username, or shell variable based on what you’ve typed when you hit the <TAB> key.

Understanding Links

Directories are lists of files and directories.

Hard LinksSoft Link (Symbolic Link)
Target must existTarget can exist or not exist
Allowed within one file systems onlyAllowed between different file systems
Links directly to the place the file is storedLinks to the entry in the file system table (node)
Removing the link means removing the whole fileRemoving the link only removes the node, not the file itself.

On Soft Links: Soft links can be thought of as directory entries that merely point to the name of another file.

Example: Creating soft link with ln (making vi links to nvim)

$ ln -s /usr/bin/vi /usr/bin/nvim