Module I
Module I
Module I
All files in Unix are “related” to one another. The file system in Unix is a collection of all the related
files ( ordinary, directory and device files) organized in a hierarchical ( an inverted tree ) structure.
The implicit feature in every Linux/Unix file system is that there is a supreme, which serves as the
reference point for all files. This top is called root, but is represented by a / ( front slash).
Root is actually a directory file and it has a number of sub-directories ( or branch) under it. These sub-
directory in turn, have more sub-directories other files(“ Leaves”) under them.
For instance temp & usr are two directories directly under root, while bin & kumar are sub-directories
under usr.
Note that it is possible for directories to have the same name. Every file, apart from root, must have a
parent, and it should be possible to trace the ultimate parentage of a file to root. Thus the usr
directory is the parent of kumar while root is the parent of usr & the grand parent of kumar. Once the
relationship between various files is established in this manner, Unix provides a simple mechanism of
accessing a file.
The login directory Kumar can also be specified as /usr/kumar. It is called a pathname consist of a
sequence of directory name, separated by /‟s then it indicated that the file is location must be
determined with respect to root.
Such pathname are called absolute pathname when we have more than one / in a pathname for each
such / we have to level in the file system.
A collection of files stored on secondary storage device is called as file system. Linux/Unix creates a
file system on the disk & store file. A physical view of file system is a collection of blocks, where each
block is a multiple of 1024. A file system is further divided into four areas.
1. Boot Block - This is the first block in the file system. It contains code to load operating system.
2. Super block - Contains information relating to file system. It has number of blocks, number of
free block etc in the file system.
3. I-Node List – Each file in Unix has an I-node. I-node stand for information node. I-node of file
contains the following information
The location of the item's contents on the disk, if any
The item's type (e.g., file, directory, symbolic link)
The item's size, in bytes, if applicable
The time the file's inode was last modified (the ctime)
The time the file's contents were last modified (the mtime)
The time the file was last accessed (the atime) for read ( ), exec ( ), etc
A reference count: the number of names the file has
The file's owner (a UID)
The file's group (a GID)
The file's mode bits (also called file permissions or permission bits)
Each I-nodes in the list has a number called as I-nodes number. When Unix deal with a file, first
gets I-node number then file & then it will get the information regarding the file.
4. Data Area – Area where the data of file is actually stored. Unix first accesses I-nodes of file
and then it comes to know about the address of blocks in data area. Then it will be data from
these blocks.
File naming conventions(Filenames in UNIX)
On a UNIX system, a filename can consist of up to 255 characters. Files may or may not have
extensions and can consist of practically any ASCII character except the / and the Null character. You
are permitted to use control characters or other nonprintable characters in a filename. However, you
should avoid using these characters while naming a file. It is recommended that only the following
characters be used in filenames:
Alphabets and numerals.
The period (.), hyphen (-) and underscore (_).
UNIX imposes no restrictions on the extension. In all cases, it is the application that imposes that
restriction. Eg. A C Compiler expects C program filenames to end with .c, Oracle requires SQL scripts
to have .sql extension.
A file can have as many dots embedded in its name. A filename can also begin with or end with a dot.
UNIX is case sensitive; cap01, Chap01 and CHAP01 are three different filenames that can coexist in
the same directory.
The names in a directory are called filenames. The only two characters that cannot appear in a
filename are the slash character ( / ) and the null character. The slash separates the filenames that
form a pathname and the null character terminates a pathname.
Two filenames are automatically created whenever a new directory is created: . (called dot) and . .
(called dot-dot). Dot refers to the current directory and dot-dot refers to the parent directory.
Unix Directories:
A directory is a file whose sole job is to store file names and related information. All files whether
ordinary, special, or directory, are contained in directories.
UNIX uses a hierarchical structure for organizing files and directories. This structure is often referred
to as a directory tree . The tree has a single root node, the slash character ( /), and all other
directories are contained below it.
Home Directory:
The directory in which you find yourself when you first login is called your home directory. You will be
doing much of your work in your home directory and subdirectories that you'll be creating to organize
your files.
You can go in your home directory anytime using the following command:
$cd ~
Here ~ indicates home directory. If you want to go in any other user's home directory then use the
following command:
$cd ~username
To go in your last directory you can use following command:
$cd -
Absolute/Relative Pathnames:
Directories are arranged in a hierarchy with root (/) at the top. The position of any file within the
hierarchy is described by its pathname.
Elements of a pathname are separated by a /. A pathname is absolute if it is described in relation to
root, so absolute pathnames always begin with a /.
These are some example of absolute filenames.
/etc/passwd
/users/abc/xyz/notes
/dev/rdsk/OS
A pathname can also be relative to your current working directory. Relative pathnames never begin
with /. Relative to user „abc' home directory, some pathnames might look like this:
xyz/notes
personal/res
To determine where you are within the file-system hierarchy at any time, enter the command pwd to
print the current working directory:
$pwd
/user0/home/abc
Basic commands for working with Directories
pwd
Print the present working directory. At any time we can determine where we are in the file system
hierarchy with the pwd, print working directory command.
Syntax: pwd [Option]
Option
-L display the logical current working directory.
-P display the physical current working directory (all symbolic links resolved)
Example:
$ pwd
cd
Change directory. We can change to a new directory with the cd, change directory, command. cd will
accept both absolute and relative path names.
Syntax: cd directory-name
Example: Change working directory to dir1
$ cd dir1
cd: Changes the current location.
cd.. To go one level back
cd../.. To go two levels back
cd To change user‟s home directory.
Mkdir
Creates a directory/ make a directory
Syntax: mkdir [option] <Directory-name>
Option
-m (mode) set the file permission bits of the final created directory to the specified mode.
Example
$mkdir xyz
The system may refuse to create a directory due to the following reasons:
1. The directory already exists.
2. There may be an ordinary file by the same name in the current directory.
3. The permissions set for the current directory don‟t permit the creation of files and directories by the
user.
Rmdir
Remove a directory. A directory needs to be empty before you can remove it. If it‟s not, we need to
remove the files first. Also, we can‟t remove a directory if it is our present working directory; we must
first change out of that directory.
Syntax: rmdir [option] directory-name
Option
-p remove directory and its ancestors
-v output a diagnostic for every directory processed.
$ rmdir dir1
ls
Displays the contents of a directory/ list directory contents. The command to list your directories and
files is ls. With options it can provide information about the size, type of file, permissions, dates of file
creation, change and access.
Syntax
$ls [options]
Options
When no argument is used, the listing will be of the current directory.
-a Lists all files, including those beginning with a dot (.).
-d Lists only names of directories, not the files in the directory
-u Sorts filenames by last access time
-t Sorts filenames by last modification time
-i Displays inode number
-l Long listing:
Meta Character and Wild Card Character
Some special characters, wild card characters, or Meta characters may be used to specify multiple
files-names. These are used to match filenames or parts of file-names
* This character is used to indicate any character(s).
Example:-
$ ls a* Displaying files start with „a‟.
$ ls i* Start with „I‟
$ ls *g list out end with „g‟ only
$ rm i* removes start with i
? this character replaces any one character in filename or It matches any single character in the
given file.
Example
$ ls ? Display single character files.
$ ls ?? Two character files
$ ls a??? list four character files but first one is „a‟.
$ rm ?? Removes two character files.
[] These are used to specify a range of characters. It matches any single characters in the given
list.
Example
$ ls [aeiou] Displays given matching character files.
$ ls [aeiou] * Displays start with a,e,I,o,u files.
$ rm [aeiou] * Removing start with a,e,I,o,u
$ ls [a-f]* Displays start with a,b,c,d,e,f
$ ls [a-f, o-v] * Display start with a-e & o-v
$ rm [a-f] * Removes a-f
4 4 4
2 2 2
1 1 1