An Introduction to the Unix Command Line
What is Unix?
A fully featured modern operating system It is available in a variety of “flavors.”
It’s comprised of simple tools that perform a single function well.
These tools can be used together to perform complex tasks.
Unix is Made Up of
Processes
• Running Programs
User owned System owned
Files
• Regular Files: – Data
– Executables <-- usually start a process
• Directory Files
– Contain other files and directories
• Special Files
Our View of the World as Users
Your Shell
Unix
Shell
A shell is a process that acts as an interface to the OS. It allows the user to run programs individually and together to accomplish a task.
Use the command line
While graphical user interfaces are available and often used on Unix systems you must have a working knowledge of the standard command line utilities, pipes, and I/O redirection to benefit from the considerable strengths of Unix.
Simple Unix Directory Structure
/ usr etc home grad bin ugrad ... var ...
local bin ... class
mmscott jpeckhar...
Your First Command
Man(manual) -- Documentation is your friend
• Syntax: man topic • man provides online documentation on nearly every standard command and configuration file. • Optional Syntax: man -k keyword • man man for more details
Special Directories
Home Directory
• /home/grad/jpeckhar • ~jpeckhar
• ~
• A user generally has permission to freely manipulate files within this directory and its children. • Users start with their home directory as their pwd when they login.
Changing Directories
• The cd(Change Directory) command is used to change directories • cd path
• Paths can be relative or absolute
• pwd reports present working directory • cd when entered by itself sets the pwd to the user’s home directory.
Your Prompt Helps you Navigate
The default prompt on your CS account tells you what directory is your pwd(where you are at the moment).
Some Examples:
– csy12:~/bin> – csy12:/home – csy12:/usr/man
Other File System Utilities
• ls • cp • mv • rm • mkdir • rmdir Lists all files in a directory Copies files Moves files Deletes files Makes directories Removes directories
Basic Syntax
• ls • cp • mv • rm • mkdir • rmdir ls cp source dest OR cp source … dir mv source dest OR mv source … dir rm file mkdir new directory name rmdir directory to be removed
Other Simple Commands
Other Comands
Every Unix distribution comes with hundreds of other standard commands that can be used for everything from viewing and searching files to developing software. Look through /bin, /usr/bin, and /usr/local/bin on most systems system and look at the man pages for the programs in those directories for more information.
Using Other Commands
Syntax: command file
• cat • grep • more • less
Echos file contents to the screen Searches a file for a string Echos a file a line at a time Same as more but more features
• wc
• sort
Counts the words in a file and more
Sorts the contents of a file
Pipes and Redirection
The Unix “philosophy” involves a set of generic tools which each do one thing well. Using these tools together allows you to appreciate the power of the Unix command. In Unix everything (including hardware devices) is treated as a File that can be read from and written to.
Pipes
• Pipes connect stdout of one command to stdin of another comand. i.e.
• ls | less
• cat student_list | grep senior | sort
I/O Redirection
• I/O redirection allows the user to change where input to a command or output from a command goes to/comes from.
• cat student_list > outfile
• program < infile
• program < infile > outfile
Standard File Handles
Every Unix process automatically comes with three file handles or descriptors. These are:
– Standard Input (stdin)
• Keyboard
– Standard Output (stdout)
• Display
– Standard Error (stderr)
• Display (unbuff)
Three Default Open Files
Program stdout Buffer
stdin
stderr
Standard File Handles
All standard shells provide a facility to remap these three file handles to other devices and files in addition to other commands. This allows one to use several simple Unix commands to perform a complex task.
Pipes
Program 1 stdout Pipe stdin
Program 2
stdout Pipe stdin Program 3
Shell/Environment Variables
Shell Variables
Parent Process
VAR=1
Environment Variables
Parent Process
VAR=1
Child Process
VAR=????
Child Process
VAR=1