linux tools shell script.. computer science
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-1 Directory Structure
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-2 Parent and Child Relationship
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.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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
/home/faculty/jomer
/home/STUDENTS/nonmajors/maamci
$ cd /usr/bin
$ ls
Blue: Directory
Green: Executable or recognized data file
Sky Blue: Linked file
Yellow with black background: Device
Pink: Graphic image file
Red: Archive file
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
/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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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
jomer@cs10:~$ echo $HOME
/home/faculty/jomer
jomer@cs10:~$ pwd
/home/faculty/jomer
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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.
jomer@cs10:~$ echo $HOME
/home/faculty/jomer
jomer@cs10:~$ pwd
/home/faculty/jomer
- Figure 5.3 is not the standard UNIX file structure
- Your login name and your home directory name are usually the same
and are assigned by the system administrator - The root directory is present in all UNIX file structures
- The name of the root directory is always the forward slash (/)
4
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-3 Directories, Subdirectories, and Files
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.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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
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.
- The forward slash (/) at the very beginning of a pathname stands for the root directory /usr/david/myfirst.
- The other slashes serve to separate the names of the other directories and files /usr/david/myfirst.
- 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
4
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-4 Pathnames in a Directory Structure
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.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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.
4
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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.
4
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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.
- Most modern Linux and UNIX limit filename to 255 characters. However, some older version of UNIX system limits filenames to 14 characters only.
- 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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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.
4
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-5 An Example of a Directory Structure
5.4 DIRECTORY COMMANDS
Figure 5.5 is your directory structure, and your home directory is david.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
4
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-6 Your Directory Structure at the Beginning
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.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-7 Your Directory Structure After Adding the memos Subdirectories
Before
After
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-8 Your Directory Structure After Adding the memos and important Subdirectories
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-9 Creating the source Directory
Figure 5.9 shows how to create a directory called source under your HOME directory.
Figure 5.10 shows your directory structure after adding the source subdirectory.
A directory structure can be created according to your specific needs.
4
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-10 Your Directory Structure After Adding the source Directory
Before
After
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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.
Figure 5.11 depicts the directory structure after this command sequence has been
applied.
--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.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-11 Your Directory Structure After Adding the Three-Levels Deep Subdirectories
Before
After
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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.
4
Omer, Jalal Sheikh (OJS)
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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).
4
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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.
- Remember, a directory listing contains only the names of the files and
subdirectories. - If no directory name is specified, the default is your current directory.
- A filename does not indicate whether it refers to a file or a directory.
- By default, the output is sorted alphabetically.
4
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-12 The Directory Structure Used for Command Examples
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Table 5.1 The ls Command Options
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.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
4
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-13 The ls Command and the -l Option
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.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-14 The ls Command-Long Format
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.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-15 The ls Command and the -r Option
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.
O
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-16 The ls Command and the -C Option
Option: -C
To display the contents of your current directory in column format, type:
ls -C [Return]
4
The columns are alphabetically sorted down two columns. This is the default output format.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-17 The ls Command and the -m Option
Option: -m
To display the contents of your current directory separated by commas, type ls -m and press [Return].
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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.
4
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Options: -m -p
List your HOME directory across the screen and indicate each directory name with a
slash (/).
4
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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:
-F in Unix
In Linux:
ls --classify
4
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Options: -arC
List all the files in your HOME directory, in column format, in reverse order:
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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.
4
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Options: -a -x -s
List all files (including hidden files) in david in column format and also show the file sizes:
4
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Options: -R -C
Show the directory structure under david in column format:
4
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.
O
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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].
4
O
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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.
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)
4
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.
O
There is a space between the command (lp) and the argument (filename).
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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.
Table 5.4 The lpr Command Options
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.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
The following command sequences shows examples of using the lpr options:
$ lpr –p lp2 myfirst [Return] . . . Print myfirst on lp2 printer.
$ lpr –m myfirst[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.
4
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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 [Return]. . . . . . Cancel the current requests on the printer lp1.
request “lp1-6889” canceled
4
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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!
O
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-18 The Directory Structure After the File Deletions
Before
After
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Table 5.5 The rm Command Options
rm Options
Like most UNIX commands, rm options modify the capabilities of the rm command.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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.cpp; 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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-18 The Directory Structure After the File Deletions
After
Before
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
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!
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.
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.
O
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.
4
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 5-19 The Directory Structure After the Entire Structure Is Removed
Before
After
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
O
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 Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
8.4 FILE MANIPULATION COMMANDS
Continuation of the file manipulation commands that were discussed in Chapter 5.
8.4.1 Copying Files: The cp Command
The cp (copy) command is used to create a copy (duplicate) of a file.
You can copy files from one directory to another or make a backup copy of a file.
Figure 8.2 shows your directory structure before and after application of the cp
command.
If the target file already exists, then its contents are destroyed.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
The following command sequences show how the cp command works:
$ ls [Return] . . . . . . . . . . . List the current directory files.
memos REPORT
$ cp REPORT REPORT.COPY [Return]
This copies REPORT to REPORT.COPY.
$ ls [Return] . . . . . . . . . . . See the list of files.
. . . . . . . . . . . . . . . . . . . REPORT.COPY is in the list.
memos REPORT REPORT.COPY
$ cp REPORT REPORT [Return] . . . Source and target filenames are the same.
File cannot be copied onto itself
0 file(s) copied
$_ . . . . . . . . . . . . . . . . . Ready for the next command.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Copy a file from your current directory to another directory.
Figure 8.3 shows your directory structure before and after the cp command.
$ cp REPORT memos [Return]
This creates a copy of REPORT in memos.
$ ls memos [Return] . . . . . . . . . . . . .List files in memos directory, and
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .REPORT is there, as you expected.
REPORT
When the target file is a directory name, then the source file is copied in the specified directory with the same filename as the source filename.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Suppose you have files called names and scores under your current directory (david) and you want to copy them to the memos directory.
Figure 8.4 shows your directory structure before and after the cp command.
$ cp names scores memos [Return]
This copies names, scores in current directory to the memos directory.
1. The files called names and scores are in your current directory.
2. The filenames on the command line are separated by at least one space.
3. The last filename must be a directory name. In this case, memos is a directory name.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
cp Options
Table 8.4 summarizes the cp command options.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Using cp Option: -b
Move REPORT to memos and create a backup of REPORT if it already exits in memos. Figure 8.5 shows your directory structure before and after the cp -b command.
$ cp -b REPORT memos [Return]
$ ls memos [Return]. . . . . . . . List files under memos.
REPORT REPORT~
Notice that REPORT is backed up and the backup filename is REPORT~.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Using cp Option: -i
The -i option protects you from overwriting an existing file. It asks for confirmation
if the target file already exists. If your reply is yes, it copies the source file, overwriting
the existing file. If your answer is no, then it quits, and your existing file remains
intact.
$ cp -i REPORT memos [Return] . . . . . Make a copy of REPORT under memos.
Target file already exists overwrite? . . . . Shows confirmation prompt; Press
[y] [Return] for yes or [n] [Return] for no.
Using cp Option: -r
- Copy files and subdirectories in david to another directory called david.bak
using the -r option.
- You can use cp with the -r option to copy directories and all their contents
into a new directory.
$ cp -r ./memos ./david.bak [Return]
This copies memos directory and all the files in it to david.bak.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
- If david.bak exists in your current directory, then files and directories in memos are copied into david.bak.
- If david.bak does not exist in your current directory, then it is created
and all the files and directories including memos itself are copied into david.bak. Now the pathname for files in memos under david.bak is ./david.bak/memos.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Practicing Linux Alternative cp Options
$ cp --interactive REPORT memos [Return]
(Same as cp -i REPORT memos.)
$ cp --recursive ./memos ./david.bak [Return]
(Same as cp -r /memos/david.bak.)
$ cp --help [return] . . . . . . . . . . . . . . . Display the help page.
$ cp -v names scores memos [Return] . . . . . Copy names and scores in
the current directory to the
memos directory.
names -> memos/names
scores -> memos/scores
$_ . . . . . . . . . . . . . . . . . . . . . . . Ready for the next command.
Notice the feedback from the -v option that shows what file is
copied and where it is copied.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
8.4.2 Moving Files: The mv Command
Use mv to move a file from one place to another or to change the name of a file or a directory.
$ mv REPORT REPORT.OLD [Return] . . . . . . . Change REPORT in the current
directory to REPORT.OLD.
Figure 8.7 shows your directory structure before and after application of the mv
command to rename REPORT.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Move REPORT to the memos directory:
$ mv REPORT memos [Return] . . . . . . Move REPORT to memos.
Figure 8.8 shows your directory structure after application of the mv command to
move REPORT.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
mv Options
Table 8.5 shows some of the mv command options.
The following command lines show the usage of the mv command options.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Using mv Option: -b (backup)
$ mv -b REPORT memos [Return] . . . . . . Move REPORT to memos and create
a backup of REPORT if it already exits
in memos.
$ ls memos [Return]. . . . . . . . Check files under memos.
REPORT REPORT~
$_ . . . . . . . . . . . . . . . . . Ready for the next command.
Notice that REPORT is backed up and the file name is REPORT~.
$ mv -v REPORT memos [Return] . . . . . . Move REPORT to memos.
REPORT -> memos/REPORT . .
$_ . . . . . . . . . . . . . . . . . Ready for the next command.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Linux Alternative mv Options
$ mv --backup REPORT memos [Return] . . . . Same as mv -b REPORT memos.
$ mv --interactive REPORT memos [Return] . . Same as mv -i REPORT memos.
$ mv --verbose REPORT memos [Return] . . . . Same as mv -v REPORT memos.
$ mv --version [Return] . . . . . . . . . . . . . . . Display version information.
$ cp --help [Return] . . . . . . . . . . . . . . . . . . Display the help page.
Read the help page and familiarize yourself with other options
available for the mv command.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
8.4.3 Linking Files: The ln Command
You can use the ln command to create additional names for an existing file and refer to the same file with different names. For example:
$ ln REPORT RP [Return]
This creates a file named RP in your current directory and links that name to REPORT. Now, REPORT and RP are two names for a single file.
Figure 8.9 shows your directory structure before and after the ln command application. At first glance, this looks like the cp command, but it is not!
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
cp physically copies the file into another place, and you have two separate
files. Whatever changes you make in one are not reflected in the other.
ln just creates another filename for the same file; no new file is created.
If you change anything in any of the linked files, the changes are there in
the file regardless of the name you use to refer to it.
To experiment with the ln command, try the following command sequence:
$ cat > xxx [Return]
This creates a file called xxx. Now type the following line in the file:
Line 1:aaaaaa
$ [Ctrl-d]
This signals end of the input.
$ ln xxx yyy [Return]
This links yyy to xxx.
$ cat yyy [Return]
This displays the contents of the xxx, using the filename yyy. The output is, as you expected, the contents of xxx:
Line 1:aaaaaa
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Figure 8.10 shows your directory structure before and after application of the ln
command.
When you create a file, you also establish a link between the directory and the file. Thus, the link count for every file is at least one, and subsequent use of ln adds to the number of links.
*
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008
Chapter 5: Introduction to the UNIX File System
* of 85
Linux Alternative Options for the ln Command
Use the Linux alternative options for the ln command, as suggested in the following command lines:
$ ln --version [Return]
This displays version information.
$ ln --help [Return]
This display the help page.
Read the help page and familiarize yourself with other options available for
the ln command.
Final Words
The three commands cp, mv, and ln all affect the filenames and work in a similar manner, but they are different commands and are used for different purposes:
• cp creates a new file.
• mv changes the filename or moves files from one place to another.
• ln creates additional names (links) for an existing file.
*