Upload
dinhdung
View
221
Download
0
Embed Size (px)
Citation preview
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
An Introduction to the UNIX command line
Vincent Voelz
Temple University
September 28, 2015
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Introduction
FilesDirectory structureFile permissions
Shell Basics
Inspecting and creating new files
Process control
File size and disk usage
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Resources for Learning UNIX
I Google :)
I http:
//www.doc.ic.ac.uk/~wjk/UnixIntro/ ←good!
I http://web.deu.edu.tr/doc/oreily/
unix/lrnunix/index.htm
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
What is UNIX?
I UNIX is an operating system, i.e. an interface between theuser and file access/execution
I UNIX is written in C
I UNIX is a collection of open-source programs that can bechained together to make tasks easily scriptable
I The shell1 is a program that responds to command-lineinstructions of the form:
command -options arguments
1bash (the Bourne-again shell), csh (the C Shell), tsch (TENEX C Shell)Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
How do I start up a Unix shell?
I On a Mac: Open Applications > Utilities > Terminal. (Putit in your Dock for easy acess!)
I On a PC: Download CygWin, a UNIX terminal emulator:https://www.cygwin.com
I In Linux: Launch the Terminal app
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Directory structureFile permissions
Directory structure
I pwd - your current directory
vvs-MacBook-Pro:~ vv$ pwd
/Users/vv
I when you log into a shell, you are (usually) put into yourhome directory
I ls - list contents of current directory
I ls ~ - list contents of home directory
I ls /Users - list contents of a specific directory
I ls -la - list contents, long view and hidden files
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Directory structureFile permissions
Directory structure
I ls -l - change directories to [dir]
I cd ../ - change one directory up
I mkdir - make a new directory
I rm - remove a file2
I rmdir - remove a directory (must be empty)
I mv [source] [destination] - move a file or directory
I cp [source] [destination] - copy a file or directory
I cp -r [source] [destination] - copy directory, withcontents, recursively
2Never ever ever use the command rm -rf /
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Directory structureFile permissions
File permissions
vvs-MacBook-Pro:~ vv$ ls -la
drwxr-xr-x 2 vv staff 68 Aug 25 09:58 testdir
-rw-r--r-- 1 vv staff 21110888 Mar 13 2015 traj0.xtc
drwxr-xr-x 6 vv staff 204 Aug 28 2014 untitled
I d[rwx][r-x][r-x] - directory yes/no, user permissions,group permissions, other permissions
I user is vv, group is staff
I rwx is read, write, execute
I chmod - change permission mode. Example:
I chmod -R ugo+rw [mydir] - recursively give read and writepermissions to users, groups and others.
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Directory structureFile permissions
What if I don’t have permissions to access files on my ownmachine?!
I sudo - execute acommand as the superuser(or any other user)
I typical when installingsoftware, etc.
http://xkcd.org
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Shell Basics
I man [command] - man(ual) pagesI history - show a list of all recent shell commandsI ls m* - list any file/directory that starts with m (* is a
substring wildcard)I ls m?file - list any file/directory with name where (? is a
single-character wildcard)I tab completion makes your life easier!I !$ is the last string you typed
vvs-MacBook-Pro:~ vv$ ls testdir
testfile
vvs-MacBook-Pro:~ vv$ ls !$
ls testdir
testfile
I ctrl-A (ctrl-E) moves cursor to beginning (end) of line
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Shell Basics
I All commands are programsI When you type a command, the shell searches a directory tree
for the program, and executes it.vvs-MacBook-Pro:~ vv$ echo $PATH
/opt/local/bin:/opt/local/sbin:/Users/vv/local/shiftx2-v107-mac-20120130: ...
I Where is the ls program?vvs-MacBook-Pro:~ vv$ which ls
/bin/ls
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Shell basics
I $PATH is an example of an environment variable.I These are parameters that your shell uses for various things
vvs-MacBook-Pro:~ vv$ echo $HOME
/Users/vv
vvs-MacBook-Pro:~ vv$ echo $SHELL
/bin/bash
vvs-MacBook-Pro:~ vv$ echo $HOSTNAME
vvs-MacBook-Pro.chem.temple.edu
I You can set environment variables3 in bash using (forexample)
I export PATH=/opt/local/bin:/opt/local/sbin:$PATH
3NOTE: when you log into a new shell, it will (typically) run all thecommands in ∼/.bash profile. These mostly define environment variables
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Shell scripting
I The shell has its own (basic) programming language, calledshell script
I Example: The ~/.bash_profile file can be thought of as ashell script, that the shell can execute like a program:
I : bash ~/.bash_profile
I It can be extremely useful for automating UNIX tasks, butgiven the choice, you should learn python scripting, which issuperior in most ways.
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Viewing the contents of files
I cat - dump the contents of a file (”catalog”)
I more - step through the contents
I less - a better version of more (”less is more”)
I head - show the first few lines of a file
I tail - show the last few lines of a file
I tail -n 30 - show the last 30 lines of a file
I grep [pattern] [file] - grab regular expression [pattern]
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Creating files
I touch [filename] - make an empty file
I echo hello > junk - direct output into a new file(overwrites)
I echo hello again >> junk - append the output to ’junk’
I use a text editor like vi or emacs (more on that next)
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Text Editors
I vi [filename] - start editing a file4
I if the file already exists, vi shows contents and starts editing
I if the file doesn’t exist, vi will make a new file
emacs is the other text editor in popular use (maybe easier tolearn?) You should pick one and learn it well.
4usually vi -> vim
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
vi reference guide
Inserting and typing text:
i insert text (and enter input mode)
$a append text (to end of line)
ESC re-enter command mode
J join lines
Cursor movement:
h left
j down
k up
l right
^ beginning of line
$ end of line
1 G top of document
G end of document
<n> G go to line <n>
^F page forward
^B page backward
w word forwards
b word backwards
Deleting and moving text:
Backspace delete character before cursor
(only works in insert mode)
x delete character under cursor
dw delete word
dd delete line (restore with p or P)
<n> dd delete n lines
d$ delete to end of line
dG delete to end of file
yy yank/copy line (restore with p or P)
<n> yy yank/copy <n> lines
Search and replace:
%s/<search string>/<replace string>/g
Miscellaneous:
u undo
:w save file
:wq save file and quit
ZZ save file and quit
:q! quit without saving
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Redirecting standard input, output, and error
I There are three kind of standard text streams that the shellcan interact with: (0) standard input, (1) standard output,and (2) standard error
I cat nonexistent 2>errors - write the error message to file’errors’
I cat nonexistent >& output - write both standard outputofand errors to the file ’output’
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Pipes
I The pipe ’|’ command directs the standard output of onecommand to the standard input of another
I cat junk | grep again - pipe the contents of ’junk’through grep to find lines containing ’again’
I cat junk | grep again | wc -l - .... additionally, countthe number of lines
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Foreground and background jobs
I To interrupt (i.e. kill) a job (process), use ctrl-C.
I Example: find / will take all day... kill it!
I To suspend (i.e. ”stop”, or pause) a job, use ctrl-Z.I You can see jobs that are running, or are stopped using
jobs -lvvs-MacBook-Pro:~ vv$ jobs -l
[1]+ 8347 Suspended: 18 find /
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Foreground and background jobs
I A foreground job interacts with and responds to the shell.Only one job at a time can be in the foreground.
I bg - puts job in the background,
I fg - puts job in the foreground
I fg 2 - puts job [2] in the foreground
I NOTE: background jobs will not respond to ctrl-C and ctrl-Z,even if they are producing lots of output!
I find / >& output & - the last ampersand means to run itin the background
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
Process Control
I ps - show all the processes that you started in all of your shells
I ps auwx - show all process currently running
I top - shows real-time processes and CPU, memory usage etc.(Example: top -o cpu)
I you can also kill processes by their process ID:
I kill 7472 - kill the process with ID 7472
Vincent Voelz An Introduction to the UNIX command line
IntroductionFiles
Shell BasicsInspecting and creating new files
Process controlFile size and disk usage
File size and disk usage
I du -sh ./* - show disk usage of files/directories in thecurrent directory, with human-readable units (not blocks)
I df -h ./* - show file system usage of mounted volumes,with human-readable units (not blocks)
Vincent Voelz An Introduction to the UNIX command line