0% found this document useful (0 votes)
2 views

Ch5.FileSystemFileHierarchy_35

This document provides an overview of the UNIX file system, focusing on its structure, mounting, and commands for file management. It explains the hierarchical nature of UNIX file systems, the advantages of disk partitioning, and various options for the 'ls' command to list files and directories. Additionally, it introduces the 'find' command for searching files within the directory tree.

Uploaded by

viptaha.2001
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Ch5.FileSystemFileHierarchy_35

This document provides an overview of the UNIX file system, focusing on its structure, mounting, and commands for file management. It explains the hierarchical nature of UNIX file systems, the advantages of disk partitioning, and various options for the 'ls' command to list files and directories. Additionally, it introduces the 'find' command for searching files within the directory tree.

Uploaded by

viptaha.2001
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Chapter-5:

File System, and File Hierarchy

Prepared by: Dr. Adnan Rawashdeh


Ref. UNIX Lecture Notes –Prof. Stewart Weiss
Overview
• Looking at UNIX file systems from the
programmer's perspective.
• The primary objective is to be able to write
programs that use the part of the UNIX API
concerning the file system and its components.
• Although it is not necessary to understand how it
is implemented to write programs that use it, but
it will help in understanding performance
considerations and limitations.
File System Abstraction
• A file system is an abstraction that supports the
creation, deletion, and modification of files, and
organization of files into directories. It also supports
control of access to files and directories and
manages the disk space associated to it.
• We tend to use the phrase “file system” to refer to
a hierarchical, tree-like structure whose internal
nodes are directories and whose external nodes are
non-directory-files (or perhaps empty directories),
but a file system is actually the flat structure sitting
on a linear storage device such as a disk partition.
File System Mounting
• Some operating systems treat the file systems on these
devices as independent entities. In Microsoft's DOS, and
systems derived from it, for example, each separate disk
partition has a drive letter, and the file hierarchy on each
separate drive or partition is separate from all others attached
to the computer. In effect, DOS has multiple trees whose roots
are drive letters (C:\ , D:\ , E:\ etc.).
• In UNIX there is a single file hierarchy. It is a tree if you think
of the leaf nodes as file-names. Every accessible file is in this
single file hierarchy, no matter how many disks are attached.
There is no such thing as the "C" drive" or "E" drive" in UNIX.
 The mount command without arguments displays a list
showing all of the file systems currently mounted on the file
hierarchy.
UNIX Disk Partitions
• Partitioning a disk has many advantages:
1. More control of security ̶ different user groups could
be placed into different partitions.
2. More efficient use of the disk ̶ different partitions
could use different block sizes and file size limits.
3. More efficient operation ̶ shorter seek distances
would improve disk access times.
4. Improved back-up procedures ̶ backups could be
done on partitions, not disks.
5. Improved reliability ̶ damage could be restricted to a
single partition rather than the entire disk.
Notice
The biggest disadvantage of partitioning a
disk is that partitions can not be increased in
size.
The file systems in separate physical partitions
are connected to each other by virtue of their
being mounted on directories of the one and
only directory hierarchy rooted at "/", but
they are otherwise unrelated.
• Each separate file system has its own pool of
free blocks, its own set of files, and its own i-
node table.
Partitions of UNIX File System
Mount a File System
Listing Files & Pattern Matching
Listing Files
In its simplest form, the ls command without arguments
displays the names of the files and directories in the current
working directory in alphabetical order by name.

Example:
$ ls
_____________________ output _______________________
21x LINES.dat LINES.idx PAGES.dat PAGES.idx

acct.pds marsha.pds p11 t11 users


__________________________________________________
Flag Options with ls Command
Option Description
-a show dot-files (i.e. hidden files).
-l display a long listing, with file type, permissions, number of links,
owner, group, size in bytes, time of last modification.
-lu same as ls -l, except uses the last time the file was read instead of
modified.
-s show the file size in blocks.
-t sort the files by timestamp specified (u for access, modification by
default).
-F show file types.
-i show the i-number of the file.
-d if ls has a directory argument, show the attributes of the
directory, instead of its contents.
-R recursively descend any directories or their subdirectories, applying
any other options listed.
ls Command Flags (cont.)
•  The ls command can also accept a filename
as a command line parameter.
• $ ls marsha.pds <RT>
________ Output shows that the file is there!_____
marsha.pds
_____________________________________
List the Contents of a Directory
 If the command line parameter is a directory name, all the files in
that directory are listed.
Example (Given that users is a directory):
$ ls users
___________________Output_________________________
4accounts Ahmad dave marsha mike
__________________________________________________

 Notice that the files are listed in order. Files beginning with
numbers come first; files beginning with uppercase characters
come next; and files beginning with lowercase characters come
last. Also notice that although this format displays your
filenames in a compact fashion, it doesn't give information
about the file’s attributes.
Long Listing
 Get more detail about files by a long listing (-l option).
It is know as Long –Listing.
 Displays seven columns of information about each file.
Example,
$ ls –l
_______________________Output________________________________
-rwxr-xr— 1 asm adept 512 Dec 14 16:16 21x
-rw-rw-r— 1 marsha adept 1024 Jan 20 14:14 LINES.dat
-rw-rw-r— 1 marsha adept 256 Jan 20 14:14 PAGES.dat
-rw-rw-r— 1 marsha acct 240 May 5 1992 acct.pds
-rw-rw-r— 1 marsha adept 1024 Nov 22 15:42 marsha.pds
-rwxrwxr— 4 root sys 43072 Aug 22 1991 p11
-rwxrwxr— 4 root sys 256041 Aug 22 1991 t11
drw-rw-r— 1 marsha adept 3072 Oct 12 11:42 users
___________________________________________________________
The First Column of in the Listing
- indicates the file's type (See Table on next slide).
-rwxr-xr— indicates the file's permissions.
1 indicates the number of links to the file.
asm is the user ID of the file's owner.
adept is the group ID of the group that the
owner belongs to.
512 is the size of the file in bytes.
Dec 14 16 :16 is the time stamp—the last
modified date & time.
21x is the name of the file.
Type of File Indicator Table
- Ordinary file
d Directory file

l Link file
c Special /character file
p Named pipe special file
b Special block file
Showing Hidden Files with -a
 The ls option doesn't normally list files that begin with a period.
Suppose that the directory displayed in the previous section also
contained a file named .profile. In that case, you would see:
$ ls -a

_______________________Output____________________________
. .. .profile 21x LINES.datLINES.idx PAGES.dat

PAGES.idx acct.pds marsha.pds p11 t11 users


_________________________________________________________

 Note that the files . and .. represent the current and parent
directories, respectively.
You can combine Options
Example (Long listing and Hidden Files):
$ ls –al <RT>
_______________________Output_______________________

-rw-r—r— 1 marsha adept 2156 Jul 19 1991 .


-rw-r—r— 1 marsha adept 2246 Jul 19 1991 ..
-rw-r—r— 1 marsha adept 117 Jul 19 1991 . profile
-rwxr-xr— 1 asm adept 512 Dec 14 16:1 21x
-rw-rw-r— 1 marsha adept 1024 Jan 20 14:14 LINES.dat
___:_________:________:______:_______:_____________:__
Showing File Types with -F
The flag -F, distinguishes directory, executable, link from
ordinary files.
Directory: / executable: * link: @
Ordinary File: {without symbol}
Example:
$ ls –F <RT>
_____________________Output____________________
21x* LINES.dat LINES.idx PAGES.dat PAGES.idx
acct.pds marsha.pds p11* t11@ users/
______________________________________________
Listing Files Names Contain Nonprintable Char.s with -q
• When a file is created, the filename can by accident acquire
nonprintable characters. Suppose that a filename contained a
backspace character (represented here as ^H). The file named
abcd^Hefg would display in a normal ls command as abcdefg.
Because you cannot see the backspace character, you might be
confused about the actual filename.

• With the ls -q option, this filename would display as abcd?efg.

•  Even if you don't know what the mystery character is, you can still
work with the file by using filename substitution (discussed in the
next section). If you need to know the exact nature of the mystery
character, you can use the -b option, which causes the nonprintable
character to print in octal mode. With the -b option, the filename
would display as abcd\010efg, in which \010 is the octal
representation of a backspace.
Additional ls options include:
-u Used with -l, causes the last access time stamp to
be displayed; instead of the last modification time.
-s Used with -l, gives the file size in blocks instead of
bytes.
-t Sorts the output by time stamp instead of name,
also used with -u sorts the output by access time.
-r Reverses the order of the output. By itself,
displays the output in reverse alphabetic order.
-x Forces the output into multicolumn.
Pattern Matching on a Single Character
 In a filename substitution, the question mark ? stands for any
single character.

Consider the contents of the current directory:


$ ls _______Output__________________
21x LINES.dat LINES.idx PAGES.dat PAGES.idx
acct.pds marsha.pds p10 p101 p11 t11 z11
_________________________________________________

You can use the question mark ? in any position. For example:
$ ls ?11 _______Output___________________
p11 t11 z11
_________________________________________________
Multiple Question Marks ??
Using multiple question marks in a single substitution.

Examples:

$ ls p??
______________________Output_______________________
p10 p11
___________________________________________________

The following command gives you all three-character filenames:


$ ls ???
______________________Output_______________________
21x p10 p11 t11 z11
___________________________________________________
More Examples
List all files that begin with LINES. It can be done successfully with:
$ ls LINES.???
______________________Output_________________
LINES.dat LINES.idx
_____________________________________________

Finding files that end in .pds -Below two commands illustrate how to do this:

$ ls ????.pds
_________________Output______________
acct.pds
_____________________________________

$ ls ?????.pds
____________________Output__________________
marsha.pds
____________________________________________
Pattern Matching on a Group of Characters
 In the previous example ($ ls ????.pds), to list all files ending in .pds
using single character substitution, you would have to know exactly how
many characters precede the period. To overcome this problem, you use
the asterisk *, which matches a character string of any length, including
a length of zero.
 Examples:
$ ls *.pds
______________________Output________________
acct.pds marsha.pds
____________________________________________

$ ls p10*
______________________Output________________
p10 p101
____________________________________________
Multiple Asterisk **
Multiple asterisk * can be used in a single substitution.
Example:
$ ls *.*
__________________Output______________________
LINES.dat LINES.idx PAGES.dat PAGES.idx acct.pds
marsha.pds
______________________________________________
Meta-Characters
• Combining character sets with meta-characters. To list
the names of all files that begin with p or t:

• $ ls [pt]*
____________Output_____________________
p10 p101 p11 t11
_______________________________________
More About Character Set
 Now suppose that you wanted a list of all filenames that begin with
an UPPERCASE alphabetic character. You could use

$ ls [ABCDEFGHIJKLMNOPQRSTUVWXYZ]*
______________________Output________________________
LINES.dat LINES.idx PAGES.dat PAGES.idx
___________________________________________________
 If you're guessing that there might be a better way to do this, you're
right. When the characters in a character set substitution are in
sequence, you can use a - (hyphen) to denote all of the characters in
the sequence. Therefore, you can abbreviate the previous command
in this way:

$ ls [A-Z]*
__________________The Same Output Above____________
Broken Sequence of Character Set
If a character sequence is broken, you can still use the
hyphen for the portion of the character set that is in
sequence. For example, the following command lists
all the three-character filenames that begin with p, q,
r, s, t, and z:

$ ls [p-tz]??
______________________Output__________________
p10 p11 t11 z11
_____________________________________________
_
Listing a Full Tree
Listing The FULL Sub-Tree From the Current Directory

$ ls *
______________________Output_________________

Tree listing; nested directories and their contents up to


the leaves
_____________________________________________
Pattern Matching on Character Sets
• Another way to do filename substitution is by
matching on character sets.
• A character set is any number of single alphanumeric
characters enclosed in square brackets—[ and ].
• Suppose that you wanted a list all filenames that
start with p or t followed by 11.
• Answer command:
$ ls [pt]11
_____________________Output_______________
p11 t11
__________________________________________
The find Command
• The find command designed to traverse the
entire tree at a given directory, without having
to specify any special recursive option.
• $ find [options] path ... Expression
 path ... means one or more directories,
 expression is composed of options, tests,
and actions joined by various operators.
Example:
• $ find . -name '*.c' –print
 Searches for all files in the tree rooted at '.'
matching the pattern *.c, printing out the
matching pathnames. The -print action can be
omitted because -print is the default action.
The -name test will return true if a file
matches the given pattern. When the test is
true, the next action will be applied, which is
how the file name is printed.
More Examples:
• $ find . -amin -30
 Searches for all files in the tree rooted at '.'
that have been accessed within the past 30
minutes and prints them.
• $ find . -mmin -120
 Searches for all files in the tree rooted at '.'
that have been modified within the past 120
minutes and prints them.
Thank you

You might also like