UNIX Basics
UNIX Basics
CIS 218
Oakton Community College
History
• UNIX was invented in 1969 at AT&T Bell Labs
• Ken Thompson and Dennis Ritchie are credited as the original architects and developers of “C”.
• Written in the C programming language in 1973
• Resold under UNIX System License (USL)
• Current USL version SystemV R4
• Different commercial flavors: Solaris (Sun), AIX (IBM), Irix (SGI), HPUX (Hewlett Packard), SCO
• Academic version – BSD (Mac OS X base)
• UNIX Variants – LINUX,
• UNIX “ports” – Android, MAC OSX
Unix Design
• Modularity
– Each part of the Unix system is a system unto itself
• printing, networking, mail, file management,
accounting, etc.
– Many Unix systems can be run in different modes. This
allow for a certain set of modules to be run based on usage
(workstation, server).
– UNIX utilities are independently developed packages
UNIX Design
UNIX Layers
• UNIX – everything is a file
• UNIX is case sensitive, DOS is not
• UNIX searches $PATH for executables, DOS starts
in current directory, then PATH.
• UNIX is multi-user, DOS is not. Windows is (kind
of)
• UNIX GUI sits “on top of” the OS - same as
Windows thru ME). This changed After NT.
• UNIX directory has one starting point – root “/”.
Windows has multiple A:, C:, D: etc.
UNIX Versus DOS/Windows
• DOS UNIX Command Description
• CD pwd Display current directory
• CD \ cd / Moves to the root directory
• CD .. cd .. Moves up one level in the directory structure
• CD path cd path Sets the current directory to a new directory
• CHKDSK du Displays disk usage
• CLS clear Clear the screen
• COMMAND sh, csh .. Start a new shell (command processor)
• COMP cmp Compares two files and shows the difference
• COPY cp Copy a file
• DATE date Displays the date
• DEL or ERASE rm Deletes a file
• DELTREE rm -r Deletes a directory tree ( CAREFUL ! ! ! )
• DIR ls -l Lists the directory in a long format
• DIR /W ls Lists the directory in a short format
• ECHO echo Displays a message or a variable
• EXIT exit End a shell
• FC diff Displays differences between two files
• FIND grep Displays lines matching a given pattern
• HELP man Display information about a command
• MD or MKDIR mkdir Creates a directory
• MORE more Displays text a page at a time
• MOVE mv Moves a file
• PRINT lpr Prints a file
• RD or RMDIR rmdir Removes a directory
• REN mv Renames a file
• SET set Sets a variable to a given value
• SORT sort Sorts a data
• TIME date Displays the time
• TYPE cat Displays a file
UNIX Versus DOS/Windows
Shells
• Shell – “sh” original interface to UNIX
• Bourne Shell added some desirable features but has different syntax for some things
• Newer Bourne Shell variants are Korn Shell (ksh) and Born Again Shell (bash)
• C Shell – C program language syntax, tcsh,
• Perl – Program Extract and Reporting Language
• Some systems allow user to chose which shell you prefer with “chsh”. The full path to the shell must be given
The Shell
• When you log in to interact with UNIX, you see a
“shell prompt” ($ or %)
• A “word” is a text string separated by white
space – space, Tab or IFS
• The shell is a program that runs constantly and
executes the commands you give it
• A command is the first “word” on a line. All
following words are command line parameters
• Commands terminators: “<LF>” – Enter or
Return, “;”, “|”, “>”
• You interact with the Unix by entering commands
thru thee shell. The line is parsed as text sepaated by
whitspace caracters into words or tokens. The first
word is always a command.
• The basic form of any Unix command is:
Command Option(s) Argument(s)
• Most commands provide integral help or on-line
Manual (man page)
• Options or flags modify the way that a command
works. They usually consist of a hyphen followed by
a single letter.
UNIX command line
• d: Directory - holds other files or directories.
• -: Normal Files Text files - text that is “human readable”.
Binary files - executable files
• l: Link - allows space efficient copying of files/directories.
Symbolic or “soft” - may span file systems.
Hard - is indistinguishable from the original file/directory.
• .: Hidden File - any file that begins with a “.” (dot).
• c,b: Character or Block Special Files – hardware
interface.
• “virtual” files – pipes, semaphores, sockets.
Unix file types (ls –l)
Files and Directories
• UNIX uses a single hierarchy to store files
beginning with root “/”
• Files are a named collection of bytes
• Directories contain other files (or not)
File1
juliana
Project
File2 File3
Basic Directory Terms • Home directory (~ ) - top of your file tree:
• Root directory (/ ) - top of a file tree:
• Current directory (.) - directory you’re in now (pwd)
• Dot (.) - current directory
• Dot-dot (..) - parent of current directory
• Path - Where to find file or directory
– Absolute path: /fs/scd/home0/juliana/myfile
– Relative path: ./myfile
• “Standard files” – STDIN, STDOUT, STDERR
Managing Files and Directories
• cd - change directories (built into shell)
• pwd – present working directory
• mkdir - creates a directory
• rmdir - removes a directory (must be empty first)
• rm myfile1 - remove a file
• rm -fr - removes directory and everything below
• touch <filename> - creates an empty file
• cat <filename> - displays a file on screen
• more <filename> - to see a screenful at a time
• mv myfile1 myfile2 - renames (moveas) a file
Tar and Compress - Tar stands for “tape archive” but is more often used to gather
files into one bundle. Equivalent of a ZIP file, without the compression.
- cd to the directory above the one you want to archive
tar cvf - diry_name > diry.tar
- To “list” the archive, use
tar -xvf diry.tar
- To “untar” the archive, use
tar -xvf diry.tar
- Compressed files take up less space. To compress a tar file, use
compress diry.tar or gzip diry.tar
- To uncompress, use
uncompress diry.tar.Z or gunzip diry.tar.gz
- Note equivalent gzip commands: zcat, zgrep, zmore, zdiff
File Permissions
• You “own” your files and directories
• user-group-other read/write/execute permissions • -rw-r--r-- 1 jsmith staff 164870 27 Feb 17:58 6.bmp
• -rw-r--r-- 1 jsmith staff 164870 27 Feb 17:57 5.bmp
• + to add, - to remove alphabetic permissions
• r=4, w=2, x=1 so rwx: 4+2+1=7 for numeric
• chmod command changes permissions
chmod 604 6.bmp
chmod g-rw 6.bmp -rw----r-- 1 jsmith staff 164870 27 Feb 17:58 6.bmp
• Access rights on files.
r : indicates read permission (or otherwise), that is, the presence or absence
of permission to read and copy the file
w : indicates write permission (or otherwise), that is, the permission (or
otherwise) to change a file
x : indicates execution permission (or otherwise), that is, the permission to
execute a file, where appropriate
• Access rights on directories.
r : allows users to list files in the directory;
w : allows users to delete files from the directory or move files into it;
x : allows users the right to access files in the directory (e.g. cd into
directory), read files in the directory provided you have read permission on
the individual files.
File Permissions
Symbol Meaning
u user
g group
o other
a all
r read
w write (and delete)
x execute (and access directory)
+ add permission
- take away permission
File Permissions
User Commands for Files
What’s in my directory?
• ls db5-24 nwade.txt schumacher5-19.txt
kirsh5-12.txt nwade5-13a.txt sheu5-20.txt
• ls -alt -rw-r--r-- 1 jsmith staff 712 24 May 09:12 db5-24
-rw-r--r-- 1 jsmith staff 1660 12 May 12:12 kirsh5-12.txt
-rw-r--r-- 1 jsmith staff 3610 11 May 08:16 ming.txt
-rw-r--r-- 1 jsmith staff 2444 10 May 16:53 mingrep
• Wildcard - * stands for any (# of) character(s)
• Wildcard - ? stands for any single character
• [abcde…] selection set, any single character within the brackets
• wc - word, line, character, and byte count
• echo - echo characters back (print)
• sort - sort lines of a text file
• uniq – remove consecutive duplicats lines
• cut/paste/join – manipulate data by physical location in a record or file
• sed – stream editor, vi commands in a batch file
• awk – string manipulation
• tr – translate from one character to another
• strings – review a binary file for text
• od – examine a file for binary contents
• head, tail – display begin and end of a file
• cat – copy to stdout
• cp – copy a file
User Commands for Files
Finding and Searching • diff – find differences between two text files
• find - a tricky but useful command
find . -name myfile -print
find . -name ‘*name*’ –print –exec “cmd” {} \;
• grep - stands for “general regular expression print” Search all files for a pattern:
grep -i “lightning system” *
grep -v “lightning system” *
grep -c “gee whiz” *
• Wildcard characters can be used to represent many other
characters in listing filenames. – Use them whenever you need to define a string of characters, such as a
filename, for use with a command.
– Useful wildcards are:
* matches any characters, zero or more.
? match any single character.
[...] matches any character in the enclosed list or range.
Examples:
– ls *.txt # Lists all the files in the current directory
# that have the extension .txt.
– cat memo? # Concatenate files that start with “memo”
# and are followed by any one character.
– rm part[1-3] # Remove files: part1, part2 and part3
Wildcard characters
Input/Output (I/O) and Redirection
• By default, a terminal keyboard is the standard input file (STDIN) from which to read in information
• Terminal is also the standard output file (STDOUT) to which information is sent from the command.
• Input can be given from the command line, or from a file: a.out < inputfile
• Output goes to your screen, or you can “redirect” it to a file: echo “hi there” > out.file
• To append, use >>
echo “hi back at you” >> out.file
• “here” document for inline stdin << EOF
• Use the “tee filename” command to redirect out put to both STDOUT and a file.
• “<& m” Duplicates standard input from file descriptor
m
• “[n] >& m” Duplicates standard output or file
descriptor n if specified from file descriptor m i.e. 2>&1
• “[n] <&-” Closes standard input or file descriptor n if
specified
• “[n] >&-” Closes standard output or file descriptor n if
specified.
• “|” or pipe (prior slide) is also redirection. It redirects
STDOUT from the preceding command to STDIN of
the following command. The general term for the
following command is called a filter as it modifies or
filters the output of the preceding command..
Input/Output (I/O) and Redirection
Pipes
• You can string commands together into a single
command using pipes ( | )
ps | grep juliana - what am I running now
• Save the results in a file:
“who | wc > usercount”
• Used with file manipulation commands as “filters”
“who | wc” - count how many users on the system
System Info Commands
• System Uptime
uptime
• What time is it?
date
• Where is a command?
which ls
whereis ls
• How do I use a command?
man ls
System Info Commands
• Where am I?
pwd - print working directory
cwd - current working directory
• Who am I?
whoami
• Who is on the system? What’s running?
who, w
ps –ef, -aux
More Useful Commands, cont.
• passwd - change password
• chsh – change default shell
• lpr - print a file (printer at OCC)
• history - (C shell builtin) - list of previous commands
• alias - create a pseudonym or shorthand for a command (behaves differently for C versus Bourne shell)
Editing - vi
• Front end to ed
• Universal among UNIX and it’s variants
• vi somefilename (copies file into memory
buffer)
• Three modes – command mode, text
mode(Esc), operator line(:) determines
how keystrokes are interpreted by vi
• i – insert text
• x – delete a character
• r – replace a character
• R – replace text
• yy p – copy a line
• dd – delete a line
• /text – locate text
• / - repeat last find
• <Enter> or <Return> - ends a line
• :wq – save results and quit (:wq! – force overwrite of file)
• :q - quit (no save)
• Navigation: arrow keys or HJKL
Editing – vi (commands)