UNIX
Reference:
UNIX Concepts and Applications – 4th Edition
By
The File System
• The file is a container for storing information.
• We can treat it simply as a sequence of characters.
• Unlike the old DOS files, a UNIX file doesn’t contain the eof (end – of – file) mark.
The File System
The File System
• UNIX treats directories and devices as files as well.
• A directory is simply a folder where you store filenames and other directories.
• All physical devices like hard disk, memory, printer, and modem are treated as files.
The File System
• Ordinary file – also known as regular file. It contains only data as a stream of characters.
• Directory file – it’s commonly said that a directory contains files and other directories, but strictly speaking, it contains their names and a number associated with each name.
Ordinary (Regular) file
• Divided into two types –
– Text file
Ordinary (Regular) file
• Text file – contains only printable characters, and you can often view the contents and make sense out of them.
All C, java program sources, shell and perl scripts are text files.
Ordinary (Regular) file
Ordinary (Regular) file
• Binary file – contains both printable and unprintable characters that cover the entire ASCII range (0 – 255).
Most UNIX commands are binary files.
The object code and executables that you produce by compiling C programs are also binary files.
Directory file
• A directory contains no data, but keeps some details of the files and subdirectories that it contains.
• A directory file contains an entry for every file and subdirectory that it houses.
Directory file
• Each entry has two components:
– The filename
Directory file
• You can’t write a directory file, but you can perform some action that makes the kernel write a directory.
Device file
• All devices and peripherals are represented by files.
Device file
• Device filenames are generally found inside a single directory structure, /dev.
• A device file is indeed special; it’s not a stream of characters.
Device file
• The operation of a device is entirely governed by the attributes of its associated file.
What’s in (File)name?
• In UNIX, a filename can consists of up to 255 characters.
• Files may or may not have extensions, can consists of practically any ASCII character except the / and the NULL character (ASCII value 0).
What’s in (File)name?
• Example of valid filenames
– last_time
– list.
– ^V^B^D-++bcd
– {}[]
– @#$%*abcd
What’s in (File)name?
• Recommended that only the following characters be used in the filenames:
– Alphabetic characters and numerals
– The period (.), hyphen (-) and underscore (_).
The parent – child relationship
• All files in UNIX are “related” to one another.
The parent – child relationship
• There is a top, which serve as the reference point for all files.
• This top is called root and is represented by a / (front slash).
The parent – child relationship
• It is conceptually different from the user-id root used by the system administrator to log in.
• Every file, apart from root, must have a parent.
HOME variable
• When you log on to the system, UNIX automatically places you in a directory called the home directory.
• It is created by the system when a user account is opened.
HOME variable
• The shell variable HOME know your home directory.
$ echo $HOME /home/directory
• What we see above is an absolute pathname.
HOME variable
• The home directory is determined by the system administrator at the time of opening a user account.
HOME variable
• Its often convenient to refer to a file dd located in your home directory as $HOME/dd.
• Most shells (except Bourne) use the ~ symbol for this purpose.
HOME variable
• The ~ symbol is a little tricky to use because it can refer to any user’s home directory and not just your own.
HOME variable
• The principle is this:
pwd : Checking your current
directory
• To display the pathname to the current directory:
$ pwd
/home/dipankar
cd : Changing the current directory
• You an move around in the file system using the cd (change directory) command.
• When used with an argument, it changes the current directory to the directory specified as argument.
$ pwd
/home/dd
$ cd progs progs must be in current dir $ pwd
cd : Changing the current directory
Alternatively we can do$ pwd
/home/dd
$ cd /home/dd/progs $ pwd
cd : Changing the current directory
$ pwd
/home/dd/progs $ cd /bin
$ pwd /bin
cd : Changing the current directory
cd can be used without any arguments:
$ pwd
/home/dd/progs $ cd
$ pwd
/home/dd
cd used without arguments reverts to the home directory.
mkdir : Making directories
• Directories are created with mkdir (make directories) command.
• The command is followed by names of the directories to be created.
$ mkdir patch
mkdir : Making directories
• You can create a number of subdirectories with one mkdir command
$ mkdir patch1 dbs doc
mkdir : Making directories
• Following command creates a directory tree
$ mkdir pis pis/progs pis/data
mkdir : Making directories
$ mkdir pis/progs pis/data pis
mkdir : Making directories
• Sometimes, the system refuses to create a directory. The reasons are as follows:
– The directory may already exist
– There may be an ordinary file by that name in the current directory
rmdir : Removing directories
• The rmdir (remove directory) command removes directories.
$ rmdir pis
rmdir : Removing directories
• rmdir can also delete more than one directory
in one shot.
$ rmdir pis/data pis/progs pis
rmdir : Removing directories
• Two important rules when deleting directories
– You can’t delete a directory with rmdir unless it is empty.
mkdir & rmdir
• The mkdir and rmdir commands work only in directories owned by the user.
How files and directories are
created and removed
• A file (ordinary or directory) is associated with a name and a number, called inode number.
• When a directory is created, an entry comprising these two parameters is made in the file’s parent directory.
• The entry is removed when the directory is removed.
Absolute pathnames
$ cat /home/dd/login.sql
If the first character of a pathname is /, the file’s location must be determined with respect to root (the first /).
Such a pathname, as one above, is called an
Absolute pathnames
Since date resides in /bin (or /usr/bin), you can also use the absolute pathname:
Relative pathnames
A relative pathname is just a path that starts from your current directory, and thus, doesn't start with a slash (/) character.
• Any example relative path is: homeworks/test1
Using . and .. in relative pathnames
• UNIX offers a shortcut to the relative pathname – that uses either the current or parent directory as reference, and specifies a path relative to it.
– A single dot (.) represents the current directory
Using . and .. in relative pathnames
$ pwd
/home/kumar/progs/data/text $ cd .. (moves one level up)
$ pwd
/home/kumar/progs/data
Using . and .. in relative pathnames
$ pwd
/home/kumar/pis
$ cd ../.. (moves two levels up)
Using . and .. in relative pathnames
$ cp ../sharma/.profile .
A filename can begin with a .
(This copies the file .profile to the current directory (.))
ls : Listing directory contents
To list the contents of the current directory:$ ls
README bago.hlp helpex.hlp hyperhelp app-defaults helpex hoh.hlp
• This lists the contents of the current directory.
ls : Listing directory contents
To list each file's type and inode number:
$ ls -Fi
6752 Mail/ 102574 SGML/ 147215 UNIX_tips/
172802 Xapps/ 51200 bin/ 44801 dead.letter
44802 mbox 44809 test* 102733 tmp/
• This lists the contents of the current directory.
• Directories are shown with a trailing '/';
ls : Listing directory contents
To list the contents of a directory using wildcard characters:
$ ls *.txt
udi1.txt wais_x500_www.txt
ls : Listing directory contents
To list files in another directory:$ ls ../Mail
ls : Listing directory contents
• To list each subdirectory and its contents, repeating this recursion:
$ ls -R uunet
ls : Listing directory contents
Option Action
-a list hidden files
-d list the name of the current directory -F show directories with a trailing '/' executable files with a trailing '*'
-g show group ownership of file in long listing
ls : Listing directory contents
Option Action
-l long listing giving details about files and directories
The UNIX file system
• /bin and /usr/bin
• /sbin and /usr/sbin
• /etc
• /dev
• /lib and /usr/lib
• /usr/include
• /usr/share/man
• /tmp
• /var
The UNIX file system
• /bin and /usr/bin: here all the commonly used UNIX
commands are found. PATH variable always shows these directories in its list.
• /sbin and /usr/sbin: if there is a command that you
can not execute but the system administrator can, then it would probably be in one of these directories.
• /etc: this directory contains the configuration files of
The UNIX file system
• /dev: this directory contains all device files. These
files don’t occupy space on disk. There could be more subdirectories like pts, dsk and rdsk in this directory.
• /lib and /usr/lib: contains all library files in binary
form. You will need to link your c programs with files in this directory.
• /usr/include: contains the standard header files used
The UNIX file system
• /usr/share/man: here the man pages are stored.
• /temp: here the users are allowed to create
temporary files. These files are wiped away regularly by the system.
• /var: the variable part of the file system. Contains all
your print jobs and your outgoing and incoming mails.
• /home: on many systems the users are housed here.