Introduction to UNIX
*nix Linux, Solaris, BSD, macOS… We will be using Ubuntu and centOS primarily
UNIX OS 3 parts: Kernel Shell Programs
Kernel Interacts with hardware directly Manages memory Handles filestore and communications
The Shell Interface between user and kernel Command line interpreter Commands are programs The Shell is also a program
Programs Everything is a process or a file Process – executing program Each process has a unique process identifier (PID) File – collection of data created by users
Filesystem
Basic Commands - Syntax Example: ls -a /home The Shell expects tokens to be separated by spaces Typically first token is your command Subsequent tokens are arguments Arguments that start with - or -- are options Non-option arguments passed to command
Basic Commands ls (list) touch myfile.txt Options: -a, -l, -h… Argument: /home/username/ touch myfile.txt Creates a blank file named myfile.txt
Basic Commands cd (change directory) pwd If no destination is provided it defaults to home directory . is your current directory .. will take you to the parent directory pwd Print working directory (where you are in the filesystem)
Basic Commands mkdir (make directory) rmdir (remove directory) Must provide new directory name rmdir (remove directory) rm (remove) Delete a file Common to see rm –rf /path/to/file Be careful, -r is recursive and -f is force
Basic Commands cp (copy) mv (move) cp source destination ex: cp myFile /home/anotheruser/ mv (move) mv source destination Use this to rename files
Basic Commands cat vim (or emacs) View contents of file Edit file There are built in tutorials for both of these vimtutor Start emacs, type “Ctrl –h” followed by “t”
Basic Commands For more information on any command type “man <command>” Shows command usage Shows all options with explanations
Basic Commands Ctrl C – kill process Ctrl D – end of file Ctrl Z – suspend process Enter “bg” to resume process in background Enter “fg” to bring it to foreground again
Other Useful Commands find /home -name <filename> df -h Find a file in your home directory by name df -h Display how much disk space has been used
Filters head tail grep Show first n lines (10 by default) Show last n lines (10 by default) grep Search for a given pattern
Wildcards * ? [ ] Zero or more characters ls *.png (list all pngs in current directory) ? Single character ls ?a* (list all files whose 2nd letter is “a”) [ ] Range of characters ls [a-dj]*.png (list all pngs that start with a,b,c,d, or j)
Redirecting Output cat list1.txt list2.txt > bothLists.txt redirects output of cat to a text file called bothLists.txt cat list1.txt list2.txt >> bothLists.txt appends output of cat to bothLists.txt command1 | command2 called a pipe redirects output of command1 to input of command2 sort < names.txt redirects input to come from file sorts content of names.txt
Combining Commands cal 2017 ; ls -a runs two commands in sequence sudo apt-get update && sudo apt-get upgrade downloads the list of available updates installs them if the previous command was successful ./myscript || echo “Script Failed!” runs myscript prints “Script Failed!” if script failed Added combination of commands on one line, since it was in Welcome Week exercises. Feel free to skip if nesessary. Max
SSH Secure Socket Shell ssh username@ip Remote access Login to another box on your network
Path Path – location of file/directory Absolute path – location of file/directory from root directory /usr/local/bin ~/Desktop (~ will refer to your home directory) Relative path – location of file/directory relative to another directory ../home/anotheruser
Path cont. $PATH /usr/bin/python (invoke python interpreter) Shell will search for a disk utility, if command is not built-in /usr/bin/python (invoke python interpreter) ./a.out (run program compiled in current directory)
Access Control Every file is owned by a user and a group Permissions allow users/groups to read/write/execute files Root can read/write/execute anything Other users can get root privileges with “sudo” “su” allows a user to change the login session owner
User Info - /etc/passwd Each line contains login info for a single user Format: Username Password Placeholder User ID Group ID Comment field Home directory User shell
User Info - /etc/shadow Each line contains hashed password for single user Format: Username Salt and hashed password Last password change Days of warning prior to password expiration Last 3 fields denote days before account is made inactive
Permissions File type, mod bits Number of hard links Owner Group Size Modification time filename
Permissions r – read (4) w – write (2) x – execute (1) To change: chmod <modes> <files> chmod 754 myScript.sh 7 – rwx for user (file’s owner) 5 – rx for group (member’s of file’s group) 4 - r for other (everyone else)
Bash Extension of Bourne Shell When the Shell gets a command it There are others, like Korn Shell When the Shell gets a command it Expands aliases Checks if command is a shell built in If not, assumes command is a program
Bash Scripting – Hello World
Bash Scripting #!/bin/bash Need to “chmod 755 hello.sh” Invoke appropriate interpreter Need to “chmod 755 hello.sh” Executable for user, group and other can read/write To run “./hello.sh”
References OneNote https://files.fosswire.com/2007/08/fwunixref.pdf