230 likes | 406 Views
UNIX chapter 07 Files and File System Structure. Mr. Mohammad Smirat. Introduction . One of the remarkable features of the UNIX operating system is the concept of files used in it. In UNIX a file is a sequence of bytes of data that reside in semi permanent form on some stable medium .
E N D
UNIX chapter 07Files and File System Structure Mr. Mohammad Smirat
Introduction • One of the remarkable features of the UNIX operating system is the concept of files used in it. • In UNIXa file is a sequence of bytes of data that reside in semi permanent form on some stable medium. • This file can contains anything you can represent as a stream of bytes.So every thing including a network interface, a disk drive, a keyboard, a printer is treated as a file. • As a result, all input and output devices are treated as files in UNIX, as described under file types and file system structure.
Introduction (cont…) • File management in the UNIX system is extremely flexible and powerful. • It provides a hierarchical directory scheme. • A directory is a container for a group of files and directories, directories can be included in other directories. • For example dir1/goodbye refers to the file goodbye in the directory dir1.
Files and directories • A file is a sequence of bytes of data that resides on medium such as (disk..). • Text file are a simple example of this structure, there is an ASCII character called newline is used to delimit lines, which create a logical text structure on a file.
Types of Files • Simple/ordinary file, used to store info and data on secondary storage device, typically a disk. • It can contain a source program, an executable program such as compilers, DB tools, pictures, audio, graphics, and so on. • Unix does not treat any kind of these files differently from others. For example a C++ file is no different to UNIX than an HTML file, however the files are treated differently by C++ compiler and WEB browser.
Types of Files (cont…) • File names are limited to 14 chars in system V and 255 chars in BSD. • To avoid any problem use only letters, numbers, period and underscore. • Extensions have no meaning to UNIX, they are parts of files names, used by users to mark their files types. • The following are the common files extensions:.bmp bit-mapped picture file..c c source code..C, .cpp c++ source code.gif graphics/picture file..html file for a web page..o object file.ps post script code.z compressed file
Types of Files (cont…) • Directory files,directory file contains the name of other files and/or directories (sub directory). • Directories have names just like other files, and these names obey the same naming rules as normal files. • A directoryis a place within the file system as a whole that contains files. This is analogous to a file drawer in your desk, where you can put things. You can construct filenames that include a directory as part of the name. The slash ‘/’ char is used to separate the different components of a file name of this form.
Types of Files (cont…) • A directory is used to store a record for each file or subdirectory it contains. • This record (also called directory entry) consists of the file inode number and the file name. • inode (index node) is a 4 byte number and is an index for an array on the disk. • An element of this array contains file attributes such as file size.
Types of Files (cont…) • Link files, the link file points to the existing file, allowing you to rename an existing file and shareit without duplicating its contents. • A link is a way to establish connection between the file to be shared and the directory entries of the user who want to access the file. To establish that link you need to set up an appropriate permission to the file then issue the ln command to link the file to a directory. There is more in chapter 11 about link files.
Types of Files (cont…) • Special files (devices), it is a mean of accessing hardware device, including the keyboard, hard disk, CD-ROM drive…etc. • Each hardware device is associated with at least one special file. • A command or an application accesses the special file in order to access the corresponding device. • There are two types: a character special file for a char oriented devices such as for keyboard and block special file for block oriented devices such as a disk. • Usually they reside in the /dev directory.
File System Structure • The UNIX file system is structured hierarchically upside down treelike, so it start with root directory and can have any number of files or sub directories under it. A typical UNIX system contains hundreds of files and directories, organized as the following root bin tmp etc user sh ps passwd Proj a Proj b
File System Structure (cont…) • When you log on, the UNIX system puts you in a specific directory, called your home/logon directory.You can specify your home directory by using the tilde (~) when using C or Korn shell. The directory you are in at any particular time is call Present Working Directory pwd. Also called as current directory. The pwd is also denoted .(pronounced dot) the parent of the pwd is .. (dotdot).
Pathnames • Pathnames can be specified in three ways • Starting with root directory (/), absolute pathname. • Starting with pwd, relative pathname. • Staring with user home directory, relative pathname.
Standard Directories and Files • Root directory (/), at the top of the file system hierarchy and denote as a slash (/), it is the master cabinet which contains all. Try command $ls /. • /bin it is known as binary directory, it contains binary -executables- images of most UNIX commands, including cat, kill, mv, rm, echo, date, mkdir, ksh, pwd...etc. all file in this directory either executable files or symbolic link to executables files. Usually it is a symbolic link to /usr/bin directory. • /dev it is the device directory, contains files corresponding to the devices (terminals, disk drives..) connected to the computer. These are the special files mentioned earlier. Some files like lp (line printer), hd (hard drive), tty (terminal) are example of these files.
Standard Directories and Files (cont…) • /etc,contains commands and files for system administration. A typical user not allowed to use these files and commands. Files in this directory include login, passwd, and profile. • /lib, contains a collection of related files for a given language in a single file called an archive. Atypical UNIX system contains libraries for C, C++ and other languages. The archive file is used by application developed by that language. Some libraries are also stored in /usr/lib directory. • /lost+found, it contains all the files not connected to any directory. These files are found by a UNIX tool, fsck (file system check).
Standard Directories and Files (cont…) • /tmp, it contains temporary files, all files are deleted periodically so the disk does not get filled with temp files. The life of this is set by system administrator. • /users, normally hold the home directories of all users of the system. It may be located in many various part of the file system structure. • /usr,it may hold some directories which contains the UNIX utilities, tools, languages, libraries, and manual pages. Two of the most important sub directories in this directory are /bin and /lib. • /var, it contains among several other directories, the /var/spool/mail directory that receive and holds your incoming mail messages, when you read your mail, it comes from a file in this directory, once you have read the mail, it is put in a file in your home directory called mbox.
Standard Directories and Files (cont…) • /etc/passwd, contains one line for every user on the system and describes that following • user login name, user name known to the system and type. • password, encrypted version of the password. • user-id, integer between 0 and 65535 assigned to the user, 0 is for super user, 1 through 99 are reserved • group id, identify the group the user belong to and also is an integer from 0 to 65535. • user info, typically the user full name. • home directory, absolute path for the user home. • login shell, absolute path for the user login shell.
Navigating the file structure $echo $HOME /usr/local/yourid $pwd /usr/local/yourid • You can browse the file system by going from your directory to other directories in the file system structure and display a directory’s contents, provided that you have permission. $ cd[directory] • will change the pwd to directory, or to the home directory if no argument is specified.
The ls command • ls[option][pathname-list]to send the names of the files in the directories and files specified in pathname list to the display screen. Options are the following • -F display `/` after directories, * after binaries, and @ after symbolic. • -a display name of all the files including the hidden files. • -i display inode number. • -l display long list that includes access permissions, link count, owner, group, file size.
Creating Files and Directories • You can create files by using different tools and applications, such as editors (such as vi, emacs), and directories by using the mkdir command. • To create a directory use the command mkdir[options] dirnames options • -m modes, create a directory with the given access permission ( permissions in chapter 8) • -p create parent directories that do not exist in the pathnames specified in dirname. • To remove a directory us the command Rmdir[options] dirnameTo remove the empty directories specified in dirname.Option -p to remove empty parent directory as well.
Ls with –l option • $ls -ldrxwr-x--- 2 ymk faculty 512 feb 10 08:25 mid1drxwr-x--- 1 ymk faculty 512 feb 05 10:20 mid2 • -l option to get a long list of a directory that gives the attribute of files. • first letter is for file type. • access permission, user, group, and others. • number of links. • owner’s login name. • owner’s group name • file size in bytes. • date, time of last modification. • file name.
Other usefull commands • The ls -d for directory listing to list the name of a directory not its contents. • file [options][file-list] will classify all the files in the file list. (determine their types). options are –f FILE as a file or file list.
Standard Files • UNIX automatically opens three files for every command it executes. • The command reads its input from one file and sends its output and error messages to the other two files. • These files are called standard files: standard input files, standard output files, and standard error files. • These default files can be changed to other files using the redirection operators: < for input, > for output and errors redirection. • File descriptor is small integer associated with every open files in UNIX. The integer values 0, 1, 2 are the file descriptors STDIN, STDOUT, and STRERR respectively.