A Unix-like system is one that behaves in amanner similar to a Unix system.Examples of Unix-like systems are:��Introduction1.1 What is a Unix-like system?HP-UX,Solaris (mixed open source/proprietary),Mac OS X,BSD (FreeBSD, OpenBSD etc.), andGNU/Linux (Ubuntu, Debian, Fedora etc.)�����PROPRIETARYFREE
IntroductionThe Open Group, an industrystandards consortium, owns theUNIX trademark.Dennis Ritchie, one of the originalcreators of Unix, expressed hisopinion that Unix-like systemssuch as Linux are de facto Unixsystems.��1.1 What is a Unix-like system? (cont’d)
Unix ﬁlesystem and shell (I)2.1 Unix system structureA computer systemconsists of hardware andsystems software (OS)that together provide aworking environment forrunning general purposeapplication programs.HardwareKernelShell & utilitiesUser space
Diﬀerent UNIX variants have diﬀerent implementations of diﬀerentsubsystems.Unix ﬁlesystem and shell (I)2.2 Kernel subsystemsProcess managementMemory managementFilesystemNetwork stack (TCP/IP)
���A process is a running instance of a program.At any given point in time, the CPU is operatingwithin the execution context of a speciﬁc process.The process manager is responsible for processscheduling, context-switching, inter-processcommunication etc.Process managementUnix ﬁlesystem and shell (I)2.2 Kernel subsystems (cont’d)
��Memory managerUnix ﬁlesystem and shell (I)2.2 Kernel subsystems (cont’d)Responsible for controlling process access to thehardware memory resources.Memory virtualization separates logical memoryfrom physical memory.Provides each process with a uniform address space.Protects the address space of each process from corruptionby other processes.��
���Manages access to data and metadata of the ﬁles,and organizes the available space of the device(s)which contain it.A Unix ﬁle is an information container structuredas a sequence of bytes.Most Unix-like systems support several ﬁlesystems.FilesystemUnix ﬁlesystem and shell (I)2.2 Kernel subsystems (cont’d)
Network stack (TCP/IP)Unix ﬁlesystem and shell (I)2.2 Kernel subsystems (cont’d)Unix-like operating systems are known for richEthernet networking support.Unix implements the TCP/IP protocol stack.The Berkeley sockets API provides a high levelinterface to the TCP/IP stack.���
Unix ﬁlesystem and shell (I)2.3 The shellA command line interface to the operating system.Used to execute other programs, manage ﬁles andprocesses.Users typically interact with a Unix shell using aterminal emulator, e.g., xterm or OS X Terminal.Graphical user interfaces for Unix are sometimesreferred to as graphical shells.����
Unix ﬁlesystem and shell (I)2.4 Simple command examplespwdlsaddusertopmkdirpswhodfcdPrint name of current/working directoryList directory contentsAdd a user to the systemDisplay processesMake directoriesReport a snapshot of the current processesShow who is logged onReport ﬁle system disk space usageChange directory
Unix ﬁlesystem and shell (I)2.5 Shell scriptingA shell script is a regular text ﬁle that conatains shell orUnix commands.Useful for automating tasks and storing commandsequences for later execution. Makes life easier for theuser.Some shell scripts run at system startup or shutdown.The /etc/init.d directory contains shell scripts usedto start, stop, or restart system daemons.����
Unix ﬁlesystem and shell (I)2.6 Options and parameters$ ls -l /binParameters are pieces of information (e.g., a ﬁlename) that the command needs in order to do its job.Flags (or options) are typically composed using ahyphen (-) followed by one or more letters. A ﬂaginstructs the shell to perform the command in aparticular way.
man:whatis:Display entries from Unix onlinedocumentation.example: man mkdirDisplays a single line information about acommand.Unix ﬁlesystem and shell (I)2.7 Getting help$ whatis ifconfigifconfig (8) - configure a network interface
Unix ﬁlesystem and shell (I)2.7 Getting help (cont’d)$ man mkdirMKDIR(1) USER COMMANDS MKDIR(1)NAMEmkdir - make a directorySYNOPSISmkdir [ -p ] dirname...DESCRIPTIONmkdir creates directories. Standard entries,`.,for thedirectory itself, and `.. for its parent, are made automat-ically.The -p flag allows missing parent directoriesto be created as needed.With the exception of the set-gid bit, thecurrent umask(2V) setting determines the mode in whichdirectories are created. The new directory inherits the set-gidbit of the parent directory. Modes may be modified aftercreation by using chmod(1V).mkdir requires write permission in the parent directory.SEE ALSOchmod(1V), rm(1), mkdir(2v), umask(2V)
Each user owns some quota of the total disk spacefor storage of personal ﬁles, mail etc. This portionof the ﬁlesystem is visible only to its owner.Users are identiﬁed by a unique number, calleduser id.��Unix ﬁlesystem and shell (II)3.1 Security fundamentalsA multi-user system is one that allows concurrentaccess by multiple users of a computer.
Unix ﬁlesystem and shell (II)3.1 Security fundamentals (cont’d)���A set of users can form a group.Each user is a member of one or more groups.Groups enable users to share material with otherusers. Like users, every group has a uniqueidentiﬁer, called group id.Groups
Unix ﬁlesystem and shell (II)3.1 Security fundamentals (cont’d)��Any Unix-like operating system has a special usercalled root (or sometimes superuser).The root user can access every ﬁle on the systemand interfere with the activity of running userprograms.The root user
Can a given ﬁle be read or written to by aparticular user?What users can run a speciﬁc program? Can a userterminate a running process?��Unix ﬁlesystem and shell (II)3.1 Security fundamentals (cont’d)Users and groups are used to determine if ﬁle orprocess operations can be performed.
Unix ﬁlesystem and shell (II)3.2 Unix ﬁle hierarchyFiles and directories areorganized in ahierarchical treestructure.In Unix, all ﬁles anddirectories appear underthe root directory "/"./(root)/bin /home /usr/home/bin /lib/ls /local/lc
Unix ﬁlesystem and shell (II)3.3 Working directoryEvery running process has a current workingdirectory.A nickname for the current working directory of aprocess is “.” (a single period).The pwd (present working directory) commanddisplays your current working directory.���
Unix ﬁlesystem and shell (II)3.4 File path (relative vs. absolute paths)To identify a speciﬁc ﬁle, the operating system uses apathname, which consists of slashes alternating with asequence of directory names that lead to the ﬁle.If the ﬁrst item in the pathname is a slash, the pathnameis said to be absolute. Its starting point is the rootdirectory.If the ﬁrst item is a directory name or ﬁlename, the pathis said to be relative. Its starting point is the currentdirectory.���
Unix ﬁlesystem and shell (II)3.5 The ﬁle abstractionUnix follows the mantra: “everything is a ﬁle.” As mentionedearlier, a Unix ﬁle is simply a sequence of bytes.Unix ﬁles may have one of the following types:Regular ﬁleDirectorySymbolic linkBlock-oriented device ﬁleCharacter-oriented device ﬁlePipe and named pipe (also called FIFO)Socket�������
Unix ﬁlesystem and shell (II)3.6 DirectoriesA directory is a list of ﬁles with associatedinformation.Every user has a home directory.(/home/<username>)��Creation dateAttributesetc.���
Unix ﬁlesystem and shell (II)3.7 Tilde expansionMost shells support the ~ operator (called tilde).~ expands to my home directory~user expands to user’s home directoryUseful since home directory locations varybetween diﬀerent machines.����~/file /home/<username>/file~alice/file2 /home/alice/file2
Unix ﬁlesystem and shell (II)cat3.8 The cat, more and less commandsCopies the contents of a ﬁle to the terminal. Wheninvoked with a list of ﬁle names, it concatenatesthem. Use the -n ﬂag to show line numbers.more/lessmore shows ﬁle contents, one page at a time. lessallows both forward and backward navigation.Programs of this sort are called pagers.
Unix ﬁlesystem and shell (II)3.9 Common utilitiespwdlsaddusertopmkdirrmdirpswhodfcdvinanormmvcptouchwcfilemountumountdateechotarpasswdkillnice&
Unix ﬁlesystem and shell (III)4.1 File permissionsUnix ﬁle permissions010204010204010204suidsgidstickyreadwriteexecuteuser group others
Example:Unix ﬁlesystem and shell (III)4.1 File permissions (cont’d)readwriteexecuteuser group others01040401010204557A common method forrepresenting Unixpermissions is usingoctal (base-8) notation.With three-digit octalnotation, each numeralrepresents a diﬀerentcomponent of the permissionset: user, group, and othersrespectively.
Unix ﬁlesystem and shell (III)4.2 Directory permissionsSimilar to ordinary ﬁle permissions.Read means that the process may read directorycontents.Write means that the process can add/remove ﬁles.Execute permission on a directory means you cancd to that directory and access its ﬁles.����
Unix ﬁlesystem and shell (III)4.3 Reading ls output-rwxr--r-- 1 root root 4096 Sep 10 21:11 fooPermissions for User (Owner) of ﬁleDirectory ﬂag; d if a directory, - if a normal ﬁle etc.Permissions for GroupPermissions for Otherowner group ﬁlenamelink count ﬁle sizeThe ls command shows the permissions and groupassociated with ﬁles when used with the -l option:
Unix ﬁlesystem and shell (III)4.4 Manipulating ﬁle attributeschown� Used to change ﬁle owner.chgrp� Used to change ﬁle group.chmod� Used to change ﬁle permissions.
Unix ﬁlesystem and shell (III)4.5 File system internalsProcess table��The process table is a data structure in thecomputer’s main memory that holds informationabout the processes currently handled by theoperating system.Each entry in the process table contains a table ofopen ﬁle descriptors for that process.
Unix ﬁlesystem and shell (III)4.5 File system internals (cont’d)Open ﬁle table��The open-ﬁle table contains an entry for each openﬁle for every process.File descriptors are unique to a process – a ﬁle thathas been opened by several processes may beassigned diﬀerent descriptors for each process.
Unix ﬁlesystem and shell (III)4.5 File system internals (cont’d)pid #10 fd #0fd #1fd #2fd #3fd #4statusoﬀsetetc.pid #11pid #12statusoﬀsetetc.statusoﬀsetetc.Processtablefd #0fd #1fd #2fd #3fd #4fd #5fd #6fd #0fd #1fd #2fd #3fd #4 memory disk or other deviceOpen-ﬁle tableActual ﬁle entriesin v-node tableFileFile
Unix ﬁlesystem and shell (III)4.6 Commands for ﬁle/process informationShows a report of all open ﬁles and theprocesses that opened them.Selects the listing of ﬁles for theprocesses whose ID numbers are in thecomma-separated set s.List information about every processnow running.lsoflsof -p sps -e
Unix ﬁlesystem and shell (IV)5.1 The standard in/out/errEntry 0: Standard input is data (often text) goinginto a program.Entry 1: Standard output is the stream where aprogram writes its output.Entry 2: Standard error is typically used byprograms to output error messages or diagnostics.The ﬁrst three entries in the ﬁle descriptor tableare preset:���
Unix ﬁlesystem and shell (IV)5.2 DevicesIn Unix, devices are treated just like ordinary ﬁles.Entries in the /dev ﬁle system are device driverinterfaces. Applications interact with the deviceusing standard I/O system calls./dev/tty represents the terminal for the currentprocess.Some special devices, called pseudo-devices do notcorrespond to physical devices (e.g. /dev/null).����
Unix ﬁlesystem and shell (IV)5.3 RedirectionBefore a command is executed, the input and output can be changed(redirected) from the terminal to a ﬁle:Append output:$ ls > file$ date >> file$ cat < fileRedirect standard outRedirect standard in
Unix ﬁlesystem and shell (IV)5.4 Mounting ﬁle systemsWhen the system starts, the directory treecorresponds to the ﬁle system located on a singledisk called the root device.File systems created on other devices are attachedto the original directory hierarchy using the mountmechanism.The commands mount and umount are used tomount and unmount devices.���
Unix ﬁlesystem and shell (IV)5.4 Mounting ﬁle systems (cont’d)/ab//aabbab//a/bDevice Mount pointroot device external device
Unix ﬁlesystem and shell (IV)5.5 LinksA directory is a list of ﬁles and directories.Each directory entry links to a ﬁle on the disk.Two diﬀerent directory entries can link to thesame actual ﬁle.Moving a ﬁle (changing its location) does notactually move any data around.����Creates a link in new location.Deletes link in old location.��In same directory or across diﬀerent directories.�
5.5 Links (cont’d)The ln command is used to create a regular link,often called hard link.Symbolic links (soft links) are created using ln -s.The main diﬀerences are that:���Hard links don’t work across ﬁle system boundaries.Hard links only work for regular ﬁles, not directories.��$ ln filename linknameUnix ﬁlesystem and shell (IV)
Unix ﬁlesystem and shell (IV)5.5 Links (cont’d)Hard vs. symbolic linksdirectoryentryﬁle contentsdirectoryentrydirectoryentryhard linkhard linksymboliclinkﬁlecontents
The Unix ﬁnd command is used to locate ﬁles.This will search the whole system for any ﬁles named foo anddisplay their pathnames.�$ find / -name fooYou can specify as many places to search as you wish.$ find /tmp /var/tmp . -name fooUnix ﬁlesystem and shell (IV)5.6 Find utilitysyntax: ﬁnd where-to-look what-to-do
Prints an entry for a directory called ./src/misc (if one exists).find . -path ./sr*sc-path pattern�Find every ﬁle under the directory /home owned by the user joe.find /home -user joe-user name�Unix ﬁlesystem and shell (IV)5.6 Find utility (cont’d)
Search for ﬁles which permissions matches the provided expression.find . -perm 664-perm p�Same as previous except that this will ignore presence of any extrapermission bits. This will match a ﬁle which has mode 0777, for example.find . -perm -664Unix ﬁlesystem and shell (IV)5.6 Find utility (cont’d)
find . -mtime 0 # find files modified between now and 1 day ago# (i.e., within the past 24 hours)find . -mtime -1 # find files modified less than 1 day ago# (i.e., within the past 24 hours, as before)find . -mtime 1 # find files modified between 24 and 48 hours agofind . -mtime +1 # find files modified more than 48 hours agofind . -mmin +5 -mmin -10 # find files modified between# 6 and 9 minutes ago-mmin n -mtime n�Unix ﬁlesystem and shell (IV)5.6 Find utility (cont’d)