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

UNIX Unbounded 5 Edition: Amir Afzal

Uploaded by

Osei Banning
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
257 views

UNIX Unbounded 5 Edition: Amir Afzal

Uploaded by

Osei Banning
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 69

UNIX Unbounded 5th Edition

Amir Afzal

Chapter 5
Introduction to the
UNIX File System

Copyright ©2008 by Pearson Education, Inc.


Upper Saddle River, New Jersey 07458
All rights reserved.

Amir Afzal
UNIX Unbounded, 5th Edition 1 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Chapter 5: Introduction to the UNIX File System

This is the first of two chapters that discuss the file structure of the
UNIX system; Chapter 8 is the second.
Chapter 5 describes the basic concepts of files and directories and their
arrangement in a hierarchical tree structure. It defines the terminology
used in the UNIX file system. It discusses commands that facilitate the
manipulation of the file system, explains the naming conventions
for files and directories, and shows a practical view of the file system
and its associated commands in the terminal session exercises.

Amir Afzal
UNIX Unbounded, 5th Edition 2 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.1 DISK ORGANIZATION
• UNIX allows you to divide your hard disk into many units (called directories),
and subunits (called subdirectories), thereby nesting directories within
directories.
• UNIX provides commands to create, organize, and keep track of directories
and files on the disk.

5.2 FILE TYPES UNDER UNIX


UNIX has three categories of files:
Regular Files
Regular files contain sequences of bytes that could be programming
code, data, text, and so on.
Directory Files
The directory file is a file that contains information (like the file name) about
other files. It consists of a number of such records in a special format
defined by your operating system.
Special Files
Special files (device files) contain specific information corresponding to
peripheral devices such as printers, disks, and so on.

Amir Afzal
UNIX Unbounded, 5th Edition 3 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.3 ALL ABOUT DIRECTORIES

• Directories are an essential feature of the UNIX file system

• The directory system provides the structure for organizing files on a disk

• In UNIX, the directory structure is organized in levels and is known as a


hierarchical structure

• The highest level directory is called the root and all other directories
branch directly or indirectly from it

• Figure 5.1 shows the root and some other directories

Amir Afzal
UNIX Unbounded, 5th Edition 4 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Figure 5-1 Directory Structure

Amir Afzal
UNIX Unbounded, 5th Edition 5 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
The terms parent and child describe the relationship between levels of the hierarchy.
Figure 5.2 shows this relationship. Only the root directory has no parents. It is the
ancestor of all the other directories.

Figure 5-2 Parent and Child Relationship

Amir Afzal
UNIX Unbounded, 5th Edition 6 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.3.1 Important Directories
Following are summaries of some of the more important directories on your UNIX System:
/
This is the root directory. It is the highest-level directory and all other directories
branch from it

/usr
This directory holds users’ home directories. In other UNIX systems including
Linux, this can be the /home directory

/usr/docs
This directory holds various documents

/usr/man
This directory holds man (online manual) pages

/usr/games
This directory holds game programs

/usr/bin
This directory holds user-oriented UNIX programs

Amir Afzal
UNIX Unbounded, 5th Edition 7 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
/usr/spool
This directory has several subdirectories such as mail, which holds mail files,
and spool, which holds files to be printed

/usr/sbin
This directory holds system administration files

/bin
This directory holds many of the basic UNIX program files

/dev
This directory holds device files. These are special files that represent the physical
computer components such as printer or disk

/sbin
This directory holds system files that usually are run automatically by the UNIX
system.

/etc
This directory and its subdirectories hold many of the UNIX configuration files

Amir Afzal
UNIX Unbounded, 5th Edition 8 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.3.2 The Home Directory

• The system administrator creates all user accounts on the system and
associates each user account with a particular directory

• This directory is the home directory

• The log on process places you into your home directory

• From your home directory you can expand your directory structure according
to your needs

• You can add as many subdirectories as you like and dividing subdirectories
into additional subdirectories

Amir Afzal
UNIX Unbounded, 5th Edition 9 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.3.3 The Working Directory

While you are working on the UNIX system, you are always associated with a
directory. The directory you are associated with or working in is called the working
directory

Figure 5.3 shows that the directory called usr has three subdirectories called david,
daniel, and gabriel. The directory david contains three files, but the other directories
are empty.

 1. Figure 5.3 is not the standard UNIX file structure

2. Your login name and your home directory name are usually the same
and are assigned by the system administrator

3. The root directory is present in all UNIX file structures

4. The name of the root directory is always the forward slash (/)

Amir Afzal
UNIX Unbounded, 5th Edition 10 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Figure 5.3 shows that the directory called usr has three subdirectories called david, daniel,
and gabriel. The directory david contains three files, but the other directories are empty.

Figure 5-3 Directories, Subdirectories, and Files

Amir Afzal
UNIX Unbounded, 5th Edition 11 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.3.4 Understanding Paths and Pathnames

• Every file has a pathname. The pathname locates the file in the file system

• You determine a file’s pathname by tracing a path from the root directory to the
file, going through all intermediate directories

 1. The forward slash (/) at the very beginning of a pathname stands for the
root directory
2. The other slashes serve to separate the names of the other directories
and files

3. The files in your working directory are immediately accessible. To access


files in another directory you need to specify the particular file by its
pathname

Figure 5.4 shows a hierarchy and the pathnames of its directories and files.
For example, using Figure 5.4, if your current directory is root, then the path to a
file (say, myfirst) under the david directory is /usr/david/myfirst.

Amir Afzal
UNIX Unbounded, 5th Edition 12 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Figure 5.4 shows a hierarchy and the pathnames of its directories and files.
For example, if your current directory is root, then the path to a file (say, myfirst) under
the david directory is /usr/david/myfirst.

Figure 5-4 Pathnames in a Directory Structure

Amir Afzal
UNIX Unbounded, 5th Edition 13 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Absolute Pathname
• An absolute pathname (full pathname) traces a path from the root
to the file.
• An absolute pathname always begins with the name of the root directory,
forward slash (/). For example, if your working directory is usr, the absolute
pathname of the file called myfirst under the directory david is /usr/david/myfirst.

 1. The absolute pathname specifies exactly where to find a file. Thus, it can
be used to specify file location in the working directory or any other
directory.
2. Absolute pathnames always start from the root directory and therefore
have a forward slash (/) at the beginning of the pathname.

Amir Afzal
UNIX Unbounded, 5th Edition 14 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Relative Pathname
• A relative pathname is a shorter form of the pathname. It traces a path
from the working directory to a file

• Like the absolute pathname, the relative pathname can describe a path
through many directories.
For example, if your working directory is usr, the relative pathname to
the file called REPORT under the david directory is david/REPORT

 There is no initial forward slash (/) for a relative pathname. It always


starts from your current directory.

Amir Afzal
UNIX Unbounded, 5th Edition 15 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.3.5 Using File and Directory Names
• Every ordinary and directory file has a filename.

• UNIX gives you much freedom in naming your files and directories.

• All UNIX systems allow a filename to be at least 14 characters.

• You name a file using a combination of characters and/or numbers.


Avoid using the following characters in filenames:
< > …………… less than and greater than signs
() …………… open and close parentheses
[] …………… open and close brackets
{} …………… open and close braces asterisk or star
? …………… question mark
" …………… double quotation mark
' …………… single quotation mark
– …………… minus sign
$ …………… dollar sign
^ …………… caret
Amir Afzal
UNIX Unbounded, 5th Edition 16 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Choose characters for filenames from the following list:
(A–Z) …………… uppercase letters
(a–z) …………… lowercase letters
(0–9) …………… numbers
(_) …………… underscore
(.) …………… dot (period)
Filename Extensions
• The filename extension helps to further categorize and describe the contents
of a file
• Filename extensions are part of the filename following a period and in most
cases are optional
• In Figure 5.5, first.c and first.cpp in the source directory have typical file
extensions (.c and .cpp for the C and C++ programming languages,
respectively)
• The following examples show some filenames with extensions:
report.c report.o
memo.04.10

 The use of more than one period in a file extension is allowed in UNIX.

Amir Afzal
UNIX Unbounded, 5th Edition 17 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.4 DIRECTORY COMMANDS
Figure 5.5 is your directory structure, and your home directory is david.

Figure 5-5 An Example of a Directory Structure

Amir Afzal
UNIX Unbounded, 5th Edition 18 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.4 DIRECTORY COMMANDS
In the following examples, assume that your login name is david, Figure 5.5 is
your directory structure, and your home directory is david.

5.4.1 Displaying a Directory Pathname: The pwd Command


The pwd (print working directory) command displays the absolute pathname of your
working (current) directory.

Log in and show the pathname of your home directory:


login: david [Return] . . Enter your login name (david)

password:. . . . . . . . . . .Enter your password.


Welcome to UNIX!

$ pwd [Return] . . . . . . Display your HOME directory path.

/usr/david
$_ . . . . . . . . . . . . . . . . .Prompt for next command.

Amir Afzal
UNIX Unbounded, 5th Edition 19 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008

1. /usr/david is your home directory pathname.

2. /usr/david is also your current or working directory pathname.

3. /usr/david is an absolute pathname because it begins with /, tracing


the path of your home directory from the root.

4. david is your login name and your home directory name.

Amir Afzal
UNIX Unbounded, 5th Edition 20 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.4.2 Changing Your Working Directory: The cd Command
To change your working directory to the source directory, use the following
command sequence:
$ pwd [Return] . . . . . . . . . . Check your current directory.
/usr/david

$ cd source [Return] . . . . . .Change to source directory.

$ pwd [Return] . . . . . . . . . . Display your working directory.


/usr/david/source

$_ . . . . . . . . . . . . . . . . . . . . . Prompt for the next command.

Amir Afzal
UNIX Unbounded, 5th Edition 21 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Assuming you have permission, you can change your working directory to /dev
by using the following command sequence:

$ cd /dev [Return] . . . . Change to /dev directory.

$ pwd [Return] . . . . . . Check your working directory.

/dev . . . . . . . . . . . . . . . Your current directory is /dev.

$_ . . . . . . . . . . . . . . . . Prompt for next command.

Amir Afzal
UNIX Unbounded, 5th Edition 22 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.4.3 Creating Directories
The very first time you log on to the UNIX system, you begin work from your home
directory, which is also your working directory.

Advantages of Creating Directories


The following lists some of the advantages of using directories:
• Grouping related files in one directory makes it easier to remember and
access them.

• Displaying a shorter list of your files on the screen enables you to find a
file more quickly.

• You can use identical filenames for files that are stored in different
directories.

• Directories make it feasible to share a large-capacity disk with other users


with a well-defined space for each user.

• You can take advantage of the UNIX commands that manipulate directories.

Amir Afzal
UNIX Unbounded, 5th Edition 23 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Directory Structure
Let’s start with the directory structure presented in Figure 5.6. Depending on your system
configuration and administration requirements, you might have other files or subdirectories in
your HOME directory.
Figure 5-6 Your Directory Structure at the Beginning

Amir Afzal
UNIX Unbounded, 5th Edition 24 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.4.4 Directory Creation: The mkdir Command
The mkdir (make directory) command creates a new subdirectory under your working
directory or any other directory you specify as part of the command.

Create a directory called memos under your HOME directory:


$ cd [Return] . . . . . . . . . . . . . . . Make sure you are in your HOME directory.
$ mkdir memos [Return] . . . . . . . Create a directory called memos.

$ pwd [Return] . . . . . . . . . . . . . . Check your working directory.


/usr/david

$ cd memos [Return]. . . . . . . . . .Change to memos directory.


$ pwd [Return] . . . . . . . . . . . . . . Check your working directory.
/usr/david/memos . . . . . . . . .... Your current directory is memos.

$_ . . . . . . . . . . . . . . . . . ...... Prompt for next command.

Amir Afzal
UNIX Unbounded, 5th Edition 25 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Figure 5-7 Your Directory Structure After Adding the memos Subdirectories

Before

After

Amir Afzal
UNIX Unbounded, 5th Edition 26 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
While you are in your HOME directory, create a new subdirectory called important in
the memos directory.

$ cd [Return] . . . . . . . . . . . .
. . . Make sure you are in your HOME directory.
$ mkdir memos/important [Return] . . . Specify the important directory
pathname.
$ cd memos/important [Return] . . . .Change to important directory.
$ pwd [Return] . . . . . . . . . . . . . . . .Check your working directory.
/usr/david/memos/important
$_ . . . . . . . . . . . . . . . . . . . . . . . . . Now your working directory is important.
Figure 5.8 shows your directory structure after adding memos and important
subdirectories.

Amir Afzal
UNIX Unbounded, 5th Edition 27 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Figure 5-8 Your Directory Structure After Adding the memos and important Subdirectories

Amir Afzal
UNIX Unbounded, 5th Edition 28 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Figure 5.9 shows how to create a directory called source under your HOME directory.

Figure 5-9 Creating the source Directory

Figure 5.10 shows your directory structure after adding the source subdirectory.

 A directory structure can be created according to your specific needs .

Amir Afzal
UNIX Unbounded, 5th Edition 29 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Figure 5-10 Your Directory Structure After Adding the source Directory

Before

After
Amir Afzal
UNIX Unbounded, 5th Edition 30 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
The mkdir Command: -p Option
The -p option creates levels of directories under your current directory.

Create a directory structure three levels deep, starting in the HOME directory:
$ cd [Return] . . . . . . . . . . . . . . . . . . Make sure you are in your HOME
directory.

$ mkdir -p xx/yy/zz [Return] . . . . . . Create a directory called xx; in xx create


a directory called yy, and in yy create a
directory called zz.
$_ . . . . . . . . . . . . . . . . . . . . . . . . . . Ready for next command.
--parents Option The alternative option in Linux. Like the -p option, --parents
creates levels of directories under your current or the specified directory.

The command line for using the --parents is:


$ mkdir --parents xx/yy/zz [Return] . . . . . Create a directory called xx;
in xx, create a directory called yy;
and in yy, create a directory called zz.
Figure 5.11 depicts the directory structure after this command sequence has been
applied.

Amir Afzal
UNIX Unbounded, 5th Edition 31 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Figure 5-11 Your Directory Structure After Adding the Three-Levels Deep Subdirectories

Before

After

Amir Afzal
UNIX Unbounded, 5th Edition 32 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.4.5 Removing Directories: The rmdir Command
The rmdir (remove directory) command removes (deletes) the specified directory.
However, it removes only empty directories - directories that contain no subdirectories

Remove the important directory from your memos directory:


$ cd [Return] . . . . . . . . . . . . . . . . Make sure you are in your HOME directory.
$ cd memos [Return] . . . . . . . . . . Change your working directory to memos.
$ pwd [Return] . . . . . . . . . . . ... Make sure you are in memos.
/usr/david/memos

$_ . . . . . . . . . . . . . . . . . . . . . . . . Yes, you are in memos.


$ rmdir important [Return] . . . . . Remove the important directory.
$_ . . . . . . . . . . . . . . . . . . . . . . . . Ready for next command.

 1. You were able to remove the important subdirectory because it was an


empty directory.
2. You must be in a parent directory to remove a subdirectory.

Amir Afzal
UNIX Unbounded, 5th Edition 33 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
From the david directory, remove the source subdirectory:
$ cd [Return] . . . . . . . . . . . . . . Change to david directory.
$ rmdir source [Return] . . . . . . Remove the source directory.
rmdir: source: Directory not empty

$ rmdir xyz [Return] . . . . . . . . Remove a directory called xyz.


rmdir: xyz: Directory does not exist

$_ . . . . . . . . . . . . . . . . . . . . . . Ready for next command.

 1. You could not remove the source subdirectory because it was not an
empty directory.
2. rmdir returns an error message if you give a wrong directory name
or if it cannot locate the directory name in the specified pathname.
3. You must be in the parent directory or a higher level of directory
to remove subdirectories (children).

Amir Afzal
UNIX Unbounded, 5th Edition 34 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.4.6 Listing Directories: The ls Command
The ls (list) command is used to display the contents of a specified directory.

• It lists the information in alphabetical order by filename


• The list includes both filenames and directory names
• When no directory is specified, the current directory is listed
• If a filename is specified, ls shows the filename with any other information
requested.
Figure 5.12 is used in the examples and command sequences as the directory
structure, and subsequent figures show the effect of the example commands on the
files and directories.

 1. Remember, a directory listing contains only the names of the files and
subdirectories.
2. If no directory name is specified, the default is your current directory.
3. A filename does not indicate whether it refers to a file or a directory.
4. By default, the output is sorted alphabetically.

Amir Afzal
UNIX Unbounded, 5th Edition 35 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Figure 5-12 The Directory Structure Used for Command Examples

Amir Afzal
UNIX Unbounded, 5th Edition 36 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Assuming your current directory is david, show the contents of your HOME
directory by typing ls [Return].

$ ls
123
Draft_1
REPORT
memos
myfirst
phones
source
xx
$_

Amir Afzal
UNIX Unbounded, 5th Edition 37 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
In some systems, the output of the ls command is not vertical in one column and
the default format is set to display filenames across the screen.

$ ls
123 Draft_1 REPORT memos myfirst phones source xx
$_

Amir Afzal
UNIX Unbounded, 5th Edition 38 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
You may want to list the contents of directories other than your current directory.
While in your HOME directory david, list files in the source directory:

$ cd [Return] . . . . . . . . . . . Make sure you are in the david directory.


$ ls source [Return] . . . . . .While in david, display list of files in the
source directory.
first.c
first.cpp
$_ . . . . . . . . . . . . . . . . . . .Ready for next command.

While in your HOME directory, check whether first.c exists in the source directory:
$ ls source/first.c [Return]. . . . . . Display the first.c filename in the source
directory to see whether it exists.
It does
exist, so the file-name is displayed.
source first.c
$ ls xyz [Return] . . . . . . . . . . . . . Display a file called xyz if it exists. If
it does not exist, you get the error
message.
xyz: No such file or directory
$_ . . . . . . . . . . . . . . . . . . . . . . . . . .You get the prompt sign again.
Amir Afzal
UNIX Unbounded, 5th Edition 39 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
ls Options
When you need more information about your files or you want the listing in a different
Format, use the ls command with options.

Table 5.1 The ls Command Options

Amir Afzal
UNIX Unbounded, 5th Edition 40 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008

1. Every option letter is preceded by a minus sign.
2. There must be a space between the command name and the option.
3. You can use pathnames to list files in a directory other than your
working directory.
4. You can use more than one option in a single command line.

Let’s use some of these options and observe their outputs on the screen.

Amir Afzal
UNIX Unbounded, 5th Edition 41 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Option: -l
The most informative option is the -l (long format) option. The listing produced by
the ls command and -l option shows one line for each file or subdirectory and
displays several columns of information for each file.

Figure 5-13 The ls Command and the -l Option

Amir Afzal
UNIX Unbounded, 5th Edition 42 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Figure 5.14 gives you a general idea about what is in each column. Look at each
column and see what type of information it conveys.

Figure 5-14 The ls Command-Long Format

Amir Afzal
UNIX Unbounded, 5th Edition 43 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Option: -r
To display the names of the files in your HOME directory in reverse order, type ls -r and
press [Return]

 Notice the option is the lowercase r.

Figure 5-15 The ls Command and the -r Option

Amir Afzal
UNIX Unbounded, 5th Edition 44 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Option: -C
To display the contents of your current directory in column format, type:
ls -C [Return]
Figure 5-16 The ls Command and the -C Option

 The columns are alphabetically


sorted down two columns. This is the
default output format.

Amir Afzal
UNIX Unbounded, 5th Edition 45 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Option: -m
To display the contents of your current directory separated by commas, type ls -m
and press [Return].

Figure 5-17 The ls Command and the -m Option

Amir Afzal
UNIX Unbounded, 5th Edition 46 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Using Multiple Options
You can use more than one option in a single command line.
For example:
To list all files, including invisible (-a option) files, in long format (-l option), and with the
filenames in reverse alphabetic order (-r option), you type:
ls -alr or ls -a -l –r [Return]

 1. You can use one hyphen to start options, but there should be no space
between the option letters.
2. The sequence of the option letters in the command line is not important.
3. You can use one hyphen for each option, but there must be a space
between option letters.

Amir Afzal
UNIX Unbounded, 5th Edition 47 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Options: -m -p
List your HOME directory across the screen and indicate each directory name with a
slash (/).

 Two options are used:


-m to produce filenames across
the screen
-p to place a slash (/) at the end
of the directory filenames

Amir Afzal
UNIX Unbounded, 5th Edition 48 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Options: -amF
Show all filenames, separated by commas, and to indicate the directory files with
a slash and executable files with an asterisk, do the following:

 1. Tree options are used:


-a to show hidden files
-m to produce filenames across the screen separated by columns
-F to indicate directories and executable files by placing a slash (/)
or an asterisk at the end of the filenames respectively
2. The two invisible files (. and ..)are directory files, indicated by the slash
at the end of the filenames

Amir Afzal
UNIX Unbounded, 5th Edition 49 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Options: -arC
List all the files in your HOME directory, in column format, in reverse order:

Amir Afzal
UNIX Unbounded, 5th Edition 50 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Options: -s -m
List the files in david, separated by commas, and show the size of each file:

 1. The first field (total 11) shows


total size of files, usually in blocks of
512 bytes.
2. The option -s produces the file size;
each file is at least 1 block
(512 bytes), regardless of how small
the file may be.

Amir Afzal
UNIX Unbounded, 5th Edition 51 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Options: -a -x -s
List all files (including hidden files) in david in column format and also show the file sizes:

 1. The total size is 13 blocks, since the size of


the two hidden files is added.
2. The -x option formats the columns in a slightly
different manner than -C. Each column is
alphabetically sorted across rather than down
the page.

Amir Afzal
UNIX Unbounded, 5th Edition 52 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Options: -R -C
Show the directory structure under david in column format:

 1. The -R option lists the filenames in the current directory david, which has
three subdirectories: memos, source, and xx.
2. Each subdirectory encountered is shown by its pathname followed by the (:)
(./memos:), and then lists the files in that directory.
3. The pathnames are relative pathnames, starting from your current directory
(the current directory sign is the dot at the beginning of the pathnames).

 The command’s options in this example are the uppercase letters R and C.

Amir Afzal
UNIX Unbounded, 5th Edition 53 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Linux Options
As with many other Linux commands, you can use the --help option to get the list
of options for the ls command:
$ ls --help [Return] . . . . . . . . . . Display list of options.
list of options will be displayed
$ _ . . . . . . . . . . . . . . . . . . . Back to the prompt.
The following command sequences show examples using the Linux alternative:

$ ls --all [Return] . . . . . . . . . . . . . . . . . . . . .Same as ls -a


$ ls --classify [Return] . . . . . . . . . . . . . . . . Same as ls -F
$ ls --format=single-column [Return] . . . . Same as ls -l
$ ls --format=commas [Return]. . . . . . . . . Same as ls -m
$ _ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Prompt for your next command.

Amir Afzal
UNIX Unbounded, 5th Edition 54 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.5 DISPLAYING FILE CONTENTS
You can always print a file to obtain a hard copy of its contents, or use the vi editor to
open a file and look at it on the screen. You also can use the cat command for this
purpose.

5.5.1 Displaying Files: The cat Command


You can use the cat (for concatenate) command to display a file (or files), to create files,
and to join files. For example you type:

$ cat myfirst [Return] . . . . . . . . . Display the myfirst file.

$ cat myfirst yourfirst [Return] . . . Display the myfirst and yourfirst files.

 1. Filenames on the command line are separated by at least one space.


2. The cat command is usually used to display small (one screen) files.

You can stop the file’s scrolling process by pressing [Ctrl-s].


To continue scrolling, press [Ctrl-q].

 Every [Ctrl-s] must be canceled by a [Ctrl-q].

Amir Afzal
UNIX Unbounded, 5th Edition 55 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.6 PRINTING FILE CONTENTS
UNIX provides commands to send your file to the printer, give you the status of
your print job, and let you cancel your print job if you change your mind.
5.6.1 Printing: The lp Command
The lp command sends a copy of a file to the printer. For example you type:
$ lp myfirst [Return] . . . . . . . . . . print myfirst file.
request id is lp1-8054 (1 file) . . . UNIX confirms your request.

 There is a space between the command (lp) and the argument (filename).

You can specify several files on one command line.


$ lp myfirst REPORT phone [Return] . . Print myfirst, REPORT, and phone.
request id is lp1-6877 (3 files)

 1. Filenames are separated by at least one space.

2. Only one banner page (first page) is produced for this request. However,
each file is printed beginning at the top of a page.

3. The files are printed in the order in which they appear on the command line.

Amir Afzal
UNIX Unbounded, 5th Edition 56 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
lp Options
Table 5.3 shows the options you can use to make your print request more
specific.

Table 5.3 The lp Command Options

Amir Afzal
UNIX Unbounded, 5th Edition 57 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.6.2 Printing: The lpr Command in Linux
Linux is based on BSD (Berkeley Software Distribution) and some of the utilities
and commands provided are different from UNIX.
For example, the lpr command is used to print specified files.

lpr Options
The lpr command provides some of the same options available for the lp
command and some different options. Use the man command to obtain a full
list of the options.

Table 5.4 The lpr Command Options

Amir Afzal
UNIX Unbounded, 5th Edition 58 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
The following command sequences shows examples of using the lpr options:

$ lpr –p lp2 myfirst [Return] . . . Print myfirst on lp2 printer.


$ lpr –m [Return] . . . . . . . . . . . . Print the myfirst file and send
mail at the completion of the
print request.
$ lpr -#3 myfirst [Return] . . . . . .Print three copies of myfirst file.
$ lpr -T hello myfirst [Return] . . Print myfirst and print “hello”
on the banner page.
$_. . . . . . . . . . . . . . . . . . . . . . . . . Ready for the next command.

 1. Most UNIX and Linux systems provide both lp and lpr commands.

2. Whenever possible, the use of the lp command is preferred.

Amir Afzal
UNIX Unbounded, 5th Edition 59 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.6.3 Canceling a Printing Request: The cancel Command
• The cancel command cancels requests for print jobs made with the
lp command.
• To use the cancel command, you need to specify the ID of the printing
job, which is provided by lp, or the printer name.
The following command sequences illustrate the use of the cancel command:
$ lp myfirst [Return] . . . . . . . . . . Print myfirst on the default printer.
request id lp1-6889 (1 file)

$ cancel lp1-6889 [Return]. . . . . . Cancel the specified printing request.


Request “lp1-6889” canceled

$ cancel lp1-6889 [Return]. . . . . . Cancel the current requests on the printer lp1.
request “lp1-6889” canceled

 1. Specifying printing request ID cancels the printing job even if it is currently


printing.
2. Specifying the printer name only cancels the request that is currently printing
on the specified printer. Your other printing jobs in the queue will be printed.
3. In both cases, the printer is freed to print the next job request.

Amir Afzal
UNIX Unbounded, 5th Edition 60 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.6.2 Printing: The lpr Command in Linux
Linux is based on BSD (Berkeley Software Distribution) and some of the utilities
and commands provided are different from UNIX.
For example, the lpr command is used to print specified files.

lpr Options
The lpr command provides some of the same options available for the lp
command and some different options. Use the man command to obtain a full
list of the options.

Table 5.4 The lpr Command Options

Amir Afzal
UNIX Unbounded, 5th Edition 61 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.7 DELETING FILES
Use the rm (remove) command to delete files that you do not want to keep
anymore. You specify the filename to delete the file from your working directory,
or specify the pathname to the file you intend to delete if it is in another
directory.
The following command sequence shows how to use the rm command:
$ cd [Return] . . . . . . . . . . . . . . . . . Change to your HOME directory.
$ rm myfirst [Return] . . . . . . . . . . Delete myfirst from your HOME
directory.
$ rm REPORT phones [Return] . . .Delete two files, REPORT and
phones.
$ rm xyz [Return] . . . . . . . .Delete xyz; if the file does not exist, the
system complains by showing an error
message.
rm: file not found

 The rm command does not give you any warning, and when a
file is deleted, it is deleted for good!

Amir Afzal
UNIX Unbounded, 5th Edition 62 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Figure 5-18 The Directory Structure After the File Deletions

Before After

Amir Afzal
UNIX Unbounded, 5th Edition 63 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
rm Options
Like most UNIX commands, rm options modify the capabilities of the rm
command.

Table 5.5 The rm Command Options

Amir Afzal
UNIX Unbounded, 5th Edition 64 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
rm Option: -i
The following command sequence shows examples of the use of the -i
option:
$ pwd [Return] . . . . . . . . . . . Check where you are.
/usr/david
$ ls source [Return]. . . . . . . . List files in the source directory.
first.c first.cpp
$ rm -i source/first.c [Return] . . . . . . . Delete first.c; the system
displays the confirmation
prompt.
Press [y] for yes.

rm: remove first.c? y

$ rm -i source/first.cpp [Return] . . . . .Delete first.c; the system


displays the confirmation
prompt.
Press [y] for yes.
rm: remove first.cpp? y

$ ls source [Return]. . . . . . . . Check whether the file was deleted.


$_ . . . . . . . . . . . . . . . . . . . . . . .No files in the source directory.

Amir Afzal
UNIX Unbounded, 5th Edition 65 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Figure 5-18 The Directory Structure After the File Deletions

Before After

Amir Afzal
UNIX Unbounded, 5th Edition 66 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
rm Option: -r
• You can delete an entire directory structure using rm with the -r option.
• Commands like this are what make UNIX an operating system for grownups!


If you want to try the rm -r * command, make sure that you are not in
one of the top-level directories and that you have copied your files into
other directories.

Command sequence using the -r option.


In this example the asterisk sign (*) is a metacharacter and means “all files.”
$ cd [Return] . . . . . . . Change to HOME directory.
$ rm -r * [Return] . . . .. Remove all there is under david (HOME) directory.

$ ls [Return] . . . . . . . . List files in david.

$_ . . . . . . . . . . . . . . . . .Sorry, nothing under david; the files are all deleted.

 1. Use the -i option to get the confirmation prompt.

2. Use the -r option sparingly, and only when it is absolutely necessary.

3. Use rmdir to remove directories.


Amir Afzal
UNIX Unbounded, 5th Edition 67 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
Figure 5-19 The Directory Structure After the Entire Structure Is Removed

After
Before

Amir Afzal
UNIX Unbounded, 5th Edition 68 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008
5.7.1 Before Removing Files
• Under UNIX, deleting files and removing directories is quite easy.
However, UNIX does not give you any feedback or warning messages.
• Before you know it, the files are deleted, and the remove command is
irreversible.

 Before typing rm, consider the following points:

1. Make sure it is not two o’clock in the morning when you start a major
delete operation.
2. Make sure you know which file you want to delete, and what the content
of that file is.
3. Think twice before pressing [Return] to complete the command.

Amir Afzal
UNIX Unbounded, 5th Edition 69 of 69
Chapter 5: Introduction to the UNIX File System
Copyright ©2008

You might also like