Unix Handouts Shantanu N Kulkarni Abstract These handouts are meant to be used as a study aid during my class. They are neither complete nor sincerely accurate. The idea is that the participants should concentrate more on the session, than on writing. 1 History The timeline of Unix can be very briefly summarised as below. The key people who played a leading role in initial development of Unix were, Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, Rob Pike and Joe Ossanna. 1960s - MIT, Bell Labs GE worked on Multics 1971 - Unix, 1 st edition 1972 - Unix written in C 1975 - Unix version 4, 5 and 6 released 1983 - FSF founded by RMS 1991-386BSD (BSD on intel) and Linux released 2 Introducing the Shell The shell is a program that interprets commands and acts as an intermediary between the user and the kernel. sh (Bourne shell, S. R. Bourne) csh (C-shell, Bill Joy) tcsh (TENEX C-shell) ksh (David Korn, AT&T) bash (Bourne again shell, Brian Fox) 3 Command Syntax and Getting Help The basic syntax of a command is, command -switch arguments Unix comes with excellent help system in form of man (short for manual) pages. One invokes this system by giving at the prompt man name-of-the-command. # displays manual of the cat command $ man cat The echo command writes arguments to the standard output. The who command displays who is logged in. # Display text $ echo Hello world Hello world # Current shell $ echo $0 -bash echo $0 and echo $SHELL shows the name of the shell. The symbol indicates that a space is required. 1
5 FILE AND DIRECTORY COMMANDS $ who am i shantanu tty7 Aug 19 16:10 (:0) $ who shantanu tty7 Aug 19 16:10 (:0) charuhas pts/0 Aug 19 16:11 192.168.1.2 The date command is used to display or set date and time. $ date Fri Aug 19 17:32:37 IST 2011 $ date -u Fri Aug 19 12:02:47 UTC 2011 $ date +%d 19 $ date +%m 08 $ date +%y 11 $ date +%d%m%y 190811 4 Directory Structure / : This is the root directory. /bin : contains executable programs /sbin : like /bin, but which are usually not executed by normal users /usr : hold only sharable, read-only data /var : spool and log files /home : home directories for users are usually beneath this directory /root : home directory of root user /etc : contains configuration files /dev : special or device files, which refer to physical devices /tmp : temporary files 5 File and Directory Commands The touch command changes file access and modification times and the ls command lists directory contents. The rm command removes directory entries. $ touch foo $ touch bar car $ ls bar car foo $ rm foo $ rm -i foo rm: remove regular file foo'? $ ls -l total 0 -rw-r--r-- 1 shantanu shantanu 0 Aug 19 18:41 bar -rw-r--r-- 1 shantanu shantanu 0 Aug 19 18:41 foo $ ls -l /etc total 1588 Only the superuser may change the date.
5 FILE AND DIRECTORY COMMANDS drwxr-xr-x 5 root root 4096 Apr 29 2010 ConsoleKit -rw-r--r-- 1 root root 2964 Nov 7 2009 Muttprintrc drwxr-xr-x 2 root root 4096 Jan 21 2011 Muttrc.d... The mkdir command is used to create directories, while the rmdir command is used to delete empty directories. The cd built-in is used to change to directories and pwd command shows the present working directory. $ mkdir dir1 $ rmdir dir1 $ mkdir a/b/c mkdir: cannot create directory a/b/c': No such file or directory $ mkdir -p a/b/c $ rmdir a/ rmdir: failed to remove a': Directory not empty $ rm -r a/ $ cd dir1/ /dir1 $ $ cd.. $ cd /etc/ /etc $ cd - $ cd /usr/local/ /usr/local $ cd ~/notes/ The cp and the mv commands are used to copy and move (rename) files respectively. $ cp foo bar $ cp /etc/passwd myfile $ cp /etc/passwd./ $ cp myfile myfile2 $ cp myfile2 dir1/ # Copies entire directory $ cp -r dir1/ dir2/ $ mv bar car Commands to view file contents The cat command is used to print file contents. The more or less commands are useful to view files screen-by-screen. $ cat /etc/passwd $ more /etc/passwd $ less /etc/passwd Aliases A way to execute a command with (usually) shorter name. $ alias c=clear $ alias dnow='date +%d%m%y' $ dnow 19082011 $ alias dnow alias dnow='date +%d%m%y' $ alias alias c='clear' alias dnow='date +%d%m%y' $ unalias dnow Aliases in csh and tcsh do not use = symbol.
6 FINDING FILES Links An inode stores all the information about a regular file, directory, or other file system object, except its data and name. A file s inode number can be found using the ls -i command. The ln command is used to make hard and symbolic links to files. # makes a hard link $ ln foo foo2 $ ls -li foo foo2 459486 -rw-r--r-- 2 shantanu shantanu 2322 Aug 20 19:17 foo 459486 -rw-r--r-- 2 shantanu shantanu 2322 Aug 20 19:17 foo2 # makes a soft links $ ln -s foo foo3 $ ls -li foo foo2 foo3 459486 -rw-r--r-- 2 shantanu shantanu 2322 Aug 20 19:17 foo 459486 -rw-r--r-- 2 shantanu shantanu 2322 Aug 20 19:17 foo2 459513 lrwxrwxrwx 1 shantanu shantanu 3 Aug 20 19:19 foo3 -> foo File Owerships and Permissions The chown command is used to change file ownership.the chmod command is used to change file permissions. u=user(owner), g=group, o=others, a=all three r=read, w=write, x=execute -rw-r--r-- 2 shantanu shantanu 0 Aug 19 17:34 foo $ chmod u+x foo -rwxr--r-- 2 shantanu shantanu 0 Aug 19 17:34 foo $ chmod g+w foo -rwxrw-r-- 2 shantanu shantanu 0 Aug 19 17:34 foo $ chmod ug-wx foo -r--r--r-- 2 shantanu shantanu 0 Aug 19 17:34 foo $ chmod ugo+r foo $ chmod a+r foo $ chmod 644 foo -rw-r--r-- 2 shantanu shantanu 0 Aug 19 17:34 foo $ chmod 123 foo ---x-w--wx 2 shantanu shantanu 0 Aug 19 17:34 foo 6 Finding Files The syntax of find command is, find path(s) expression(s) On many Unix systems only the superuser may use the chown command.
7 FILTERS # Find by name find./ -name 'passwd' find./ -name 'hosts*' # By type find./ -type d find./ -type f -a -name 'hosts*' # by size find./ -size +10000c find./ -size +10000c -o type d # by time find./ -mtime +7 find./ -newer sample 7 Filters Filters in Unix are those commands which show specific parts of the file (or input). The basic filters include, head and tail The head and tail utilities show by default first ten and last ten lines of a given file respectively. $ cat sample.txt $ head -n 2 sample.txt $ tail -n 2 sample.txt cut The cut utility selects portions of each line vertically. -d delim : use delim as delimiter $ cut -d ',' -f1 sample.txt archis shantanu anil $ cut -d ',' -f1,3 sample.txt archis,python shantanu,python anil,perl -f list : show fields specified in list $ cut -d ',' -f1-3 sample.txt archis,shell,python shantanu,shell,python anil,php,perl $ cut -d ',' -f3- sample.txt python,c,198 python,make,234 perl,make,789 sort The sort utility sorts text files by lines. -r : reverse sort -t char : use char as field separator $ sort sample.txt -n : numeric sort -k pos : sort by key position $ sort -t ',' -k2 sample.txt
8 VI EDITOR $ sort -t ',' -r -k2 sample.txt $ sort -t ',' -n -k5 sample.txt grep The grep utility searches any given input files, selecting lines that match one or more patterns. -c : count of selected lines -i : case insensitive matching -v : not matching lines $ grep 'python' sample.txt $ grep -v 'python ' sample.txt $ grep -c 'python' sas mple.txt 2 8 VI Editor The VI editor is a screen-oriented text editor. The first thing to note about the VI editor is that it has two modes: command and insert. The command mode allows the entry of commands to manipulate text and the insert mode puts anything typed on the keyboard into the current file. To leave insert mode, one has to press the ESCAPE key. Basic survival commands are given below. Open and Close vi new.txt edit new.txt :w/:wq save/save and quit :w new2.txt save as new2.txt :q! quit do not save Cursor Movements h/j/k/l left,down,up,right w/b word forward/backward ^/$ start/end of line ng n th line Cut-Copy-Put x delete char dw/yw delete/copy word dd/yy delete/copy line p put (paste)