Terminal emulation is negotiated between your client machine and the UNIX server.
pts/0
) and type (e.g., vt100
) are negotiated when you first connect and login./home/inevitabby/.local/share/pnpm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/lib/llvm/16/bin:/usr/lib/llvm/15/bin:/etc/eselect/wine/bin:/opt/cuda/bin
xterm-256color
/bin/zsh
<CTRL>c
: Interrupt<CTRL>d
: Halt or EOF<CTRL>g
: Bell<CTRL>h
: Backspace<CTRL>l
: Redraw screen<CTRL>u
: Kill line<CTRL>w
: Kill word<CTRL>z
: Suspend<CTRL>s
: Stop the screen from scrolling<CTRL>q
: Continue scrollingShell: User interface to the operating system.
Files can configure the environment upon login, e.g.,
~/.profile
: Bourne/Korn shells~/.login
: C shell~/.zshrc
: Zsh shellThings you can do with a shell:
- Command history
- Command line editing
- File expansion (tab completion)
- Command expansion
- Key bindings
- Spell correction
- Job control
- This:
$ cowsay -f bong "I love systems programming" ____________________________ < I love systems programming > ---------------------------- \ \ ^__^ _______/(oo) /\/( /(__) | W----|| |~| || || |~| ~~ |~| ~ |_| o |#|/ _+#+_
After you log in and the shell startup files have run, the shell will display a prompt.
$
, #
, and %
..
: Current working directory..
: Parent directory.login
: login script file (csh).profile
: login script file (sh/ksh).logout
: logout script file.plan
: finger resource file.cshrc
: resource configuration script file for C shell.bashrc
: resource configuration script file for Bourne Again shell.exrc
: resource configuration script file for viNote: Hidden files begin with
.
and don’t show up by default.
Format:
command [options] <arguments>
[options]
: Modify how the command works
- aka: Flags
- Single letters prefixed with a dash (
-
)- Combined or separated
- e.g.,
-a -l
is the same as-al
- Come before arguments
<arguments>
: Define command scope.
- Can be optional or mandatory
- Some commands assume a default argument if none are given
- Usually files or directories
- Stuff in brackets is optional
- Boldface words are literals (must be typed as is)
Note: Commands are case-sensitive, and spaces must be inserted between commands, options, and arguments.
man
)Format:
man <command>
Format:man -k <keyword>
man
: Utility to display the manual page for a shell command, system program, library function, etc.
<space>
: Move forward a pageb
: Move backwards a page/
: Begin searchn
: See next search resultN
: See previous search resulth
: More commandsq
: Quit.stty
)stty
: Command to set terminal type options.
Examples:
# List all terminal settings stty -a # Make the erase key <Ctrl>h stty erase ^h
who
: Prints who is logged on, when and wherewho am i
: Identifies your username and terminal ID.who mom likes
finger
: Prints more login information than who
uname
: Print name of operating systemid
: Displays user id and all group names and ids.write
: One-way messaging.talk
: Interactive messagingmail
: Simple and old email programmailx
: Improved mail
ls
: Lists files in a directoryrm
: Removes files and directoriescp
: Copies files and directoriesmv
: Moves/renames files (and directories)cd
: Changes directoriesmkdir
: Make empty directoriesrmdir
: Remove empty directorypwd
: Display name of present working directory:file
: Display file typedu
: Display disk usagedf
: Display free disk spacewc
: Count lines, word, and bytes in a filewhich
: Looks for an executable in your $PATH
ln
: Create a linksort
: Sort lines of text filesuniq
: List unique items.find
: Find files.diff
: Compare lines among filescmp
: Test two files for equality, returning location of first differing byte.comm
: Reads two already-sorted files and outputs three columns:More on
ls
(list files in a directory):Format:
ls [-alRF...] <file-list>
-a
: List all files including the dot (hidden) files-l
: Long format (show file type, permissions, links, owner, etc)-R
: Recursively list subdirectories-F
: List directories with file type
More on
cp
(copying files):Format:
cp [-ir...] <file1> <file2>
Format:cp [-ir...] <file-list> <directory
>
Format:cp [-ir...] <directory> <directory>
-i
: Interactive, prompt whenever a file will be overwritten-r
: Recursive, copy a whole directory treeExamples:
# 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 *
cat
: Concatenate and print to screen (<CTRL>s
and <CTRL>q
to stop/start)head -x
: Display first x lines of filetail -x
: Display last x lines of filepage
: Page file to the screenmore
: Display part of file to screenNote:
head
andtail
both default to x=10
history
: Prints command history.touch
: Update file timestamps (creates empty file if it doesn’t exist).date
: Print date and time.echo
: Display command line input to screen.env
: Print all environment variables.clear
: Clears the terminalcal
: Prints calendar for any year and month.bc
: A calculatorxargs
: Run each line of input as an argument to a specified commandTip: 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
- … and much more
bc
Features:
- Arithmetic operators
- Increment/decrement operators
- Assignment operators
- Comparison or relational operators
- Logical or boolean operators
- Math functions
- Conditional statements
- Iterative statements
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.
-c
: Create an archive-x
: Extract an archive (untar)-z
: Compress the archive with gzip-v
: Display progress in the terminal (verbose mode)-f
: Specify filename of the archiveExample: Creating a compressed archive of your home directory
$ tar -czvf archive.tar.gz ~
ps
: Monitors status of processeskill
: Send a signal to a pidwait
: Parent process wait for one of its children to terminatenohup
: Makes a command immune to the hangup and terminate signalsleep
: Sleep in secondsnice
: Run processes at low priorityExample: 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:
1
: HUP (hang up)2
: INT (interrupt)3
: QUIT6
: ABRT (abort)9
: KILL (non-catchable, non-ignorable)14
: ALRM (alarm clock)15
: TERM (software termination)
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.
fg
to send the process back to the foreground.Symbol | Meaning |
---|---|
> | 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 |
cmd | Command 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
- Stores “hi” in file.txt
$ date -u > file.txt
- Stores current date (UTC) in 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
- Appends some text to file.txt
Example: Input redirection
cat < file.txt
- Prints contents of file.txt by running
cat
and using the contents offile.txt
asstdin
.
- You could also use
cat file.txt
sincecat
accept files as arguments, not juststdin
, but this is an example of the<
character.
Examples: File substitution wildcards
$ ls –l foo*
- List all files that begin with the word “foo” followed by anything else
- e.g., “foo”, “foo1”, and “foo.txt” will be listed, but not “bar.txt”.
$ ls –l foo?
- List all files that begin with the word “foo” followed by any single character
- In other words, it lists all files that begin with “foo” and are 4 characters long.
- e.g., “foo1” will be listed, but not “foo.txt”.
$ ls –l foo[1-3]
- List all files that begin with the word “foo” followed by any single character between number 1 and 3 (inclusive)
- The
-
is specifying range.- e.g., “foo1” and “foo2” will be listed, but not “foo4”.
$ ls –l foo[23]
- List all files that begin with the word “foo” followed by any single character which is either 2 or 3
- The
[]
without a range means exclusive or.- e.g., “foo2” and “foo3” will be listed, but not “foo1”.
$ ls [!f-z]???
- List all files that begin with the characters a through e and followed by any three characters
- The
!
is a negation, meaning that we’re matching everything that’s not between f and z/
Warning: Forgetting to escape certain metacharacters can have disastrous results (e.g., accidentally expanding a
*
in arm
command)
- Which is why we double-quote a variable references whenever possible.
There are three ways to pass metacharacters without interpreting them:
\
): Put a backslash in front of them.''
): Surround a string with single quotes to protect all characters except the backslash""
): 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
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
Two ways to control the order commands are executed:
;
): Executes left-to-right, e.g.,$ date; pwd; ls
()
): Groups commands together, e.g.,$ (date; pwd; ls) > out.txt
We can use the exit codes of commands to conditionally execute other commands.
Examples: Conditional execution
$ gcc hello.c && ./a.out
- Run compiled code only if compilation was successful.
$ gcc hello.c || notify-send "Compilation failed"
- Send notification only if gcc fails.
`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
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
Most shells can complete a filename, command name, username, or shell variable based on what you’ve typed when you hit the <TAB>
key.
<TAB>
a second time will show a list of possible completions.Directories are lists of files and directories.
Hard Links | Soft Link (Symbolic Link) |
---|---|
Target must exist | Target can exist or not exist |
Allowed within one file systems only | Allowed between different file systems |
Links directly to the place the file is stored | Links to the entry in the file system table (node) |
Removing the link means removing the whole file | Removing 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.
- Soft links don’t contribute to the link count.
- Soft links also work on directories, unlike hard links.
Example: Creating soft link with
ln
(makingvi
links tonvim
)$ ln -s /usr/bin/vi /usr/bin/nvim