Tuesday, July 1, 2008

LINUX-1

How Linux Organizes Data
In order to make the most effective use of your Linux system, you must understand how Linux organizes data. If you're familiar with Microsoft Windows or another operating system, you'll find it easy to learn how Linux organizes data, because most operating systems organize data in rather similar ways. This section explains how Linux organizes data. It also introduces you to several important Linux commands that work with directories and files.
4.3.1 Devices
Linux receives data from, sends data to, and stores data on devices. A device usually corresponds to a hardware unit, such as a keyboard or serial port. However, a device may have no hardware counterpart: the kernel creates several pseudodevices that you can access as devices but that have no physical existence. Moreover, a single hardware unit may correspond to several devices - for example, Linux defines each partition of a disk drive as a distinct device. Table 4.2 describes some typical Linux devices; not every system provides all these devices and some systems provide devices not shown in the table.

Table 4.2: Typical Linux Devices
Device
Description
atibm
Bus mouse
audio
Sound card
cdrom
CD-ROM drive
console
Current virtual console
fd n
Floppy drive ( n designates the drive; for example, fd0 is the first floppy drive)
ftape
Streaming tape drive not supporting rewind
hd xn
Non-SCSI hard drive ( x designates the drive and n designates the partition; for example, hda1 is the first partition of the first non-SCSI hard drive)
inportbm
Bus mouse
lp n
Parallel port ( n designates the device number; for example, lp0 is the first parallel port)
modem
Modem
mouse
Mouse
nftape
Streaming tape drive supporting rewind
nrft n
Streaming tape drive supporting rewind ( n designates the device number; for example, nrft0 is the first streaming tape drive)
nst n
Streaming SCSI tape drive not supporting rewind ( n designates the device number; for example, nst0 is the first streaming SCSI tape drive)
null
Pseudodevice that accepts unlimited output
printer
Printer
psaux
Auxiliary pointing device, such as a trackball, or the knob on IBM's Thinkpad
rft n
Streaming tape drive not supporting rewind ( n designates the device number; for example, rft0 is the first streaming tape drive)
scd n
SCSI device ( n designates the device number; for example, scd0 is the first SCSI device)
sd xn
SCSI hard drive ( x designates the drive and n designates the partition; for example, sda1 is the first partition of the firs SCSI hard drive)
sr n
SCSI CD-ROM ( n designates the drive; for example, sr0 is the first SCSI CD-ROM)
st n
Streaming SCSI tape drive supporting rewind ( n designates the device number; for example, st0 is the first streaming SCSI tape drive)
tty n
Virtual console ( n designates the particular virtual console; for example, tty0 is the first virtual console)
ttyS n
Modem ( n designates the port; for example, ttyS0 is an incoming modem connection on the first serial port)
zero
Pseudodevice that supplies an inexhaustible stream of zero-bytes
4.3.2 Filesystems
Whether you're using Microsoft Windows or Linux, you must format a partition before you can store data on it. When you format a partition, Linux writes special data, called a filesystem, on the partition. The filesystem organizes the available space and provides a directory that lets you assign a name to each file, which is a set of stored data. You can also group files into directories, which function much like the folders you create using the Microsoft Windows Explorer: directories store information about the files they contain.
Every CD-ROM and floppy diskette must also have a filesystem. The filesystem of a CD-ROM is written when the disk is created; the filesystem of a floppy diskette is rewritten each time you format it.
Microsoft Windows 95 lets you choose to format a partition as a FAT or FAT32. Linux supports a wider variety of filesystem types; Table 4.3 summarizes the most common ones. The most important filesystem types are ext2; which is used for Linux native partitions, msdos, which is used for FAT partitions (and floppy diskettes) of the sort created by MS-DOS and Microsoft Windows; and iso9660, which is used for CD-ROMs. Linux also provides the vfat filesystem, which is used for FAT32 partitions of the sort created by Microsoft Windows 9x. Linux also supports reading of Windows NT NTFS filesystems; however, the support for writing such partitions is not yet stable.

Table 4.3: Common Filesystem Types
Filesystem
Description
coherent
A filesystem compatible with that used by Coherent Unix
ext
The predecessor of the ext2 filesystem; supported for compatibility
ext2
The standard Linux filesystem
hpfs
A filesystem compatible with that used by IBM's OS/2
iso9660
The standard filesystem used on CD-ROMs
minix
An old Linux filesystem, still occasionally used on floppy diskettes
msdos
A filesystem compatible with Microsoft's FAT filesystem, used by MS-DOS and Windows
nfs
A filesystem compatible with Sun's Network File System
ntfs
A filesystem compatible with that used by Microsoft Windows NT's NTFS filesystem
sysv
A filesystem compatible with that used by AT&T's System V Unix
vfat
A filesystem compatible with Microsoft's FAT32 filesystem, used by Windows 9x
xenix
A filesystem compatible with that used by Xenix
4.3.3 Directories
If you've used MS-DOS, you're familiar with the concepts of file and directory, and with various MS-DOS commands that work with files and directories. Under Linux, files and directories work much as they do under MS-DOS.
4.3.3.1 Home and working directories
When you login to Linux, you're placed in a special directory known as your home directory. Generally, each user has a distinct home directory, where the user creates personal files. This makes it simple for the user to find files previously created, because they're kept separate from the files of other users.
The working directory - or current working directory, as it's sometimes called - is the directory you're currently working in. When you login to Linux, your home directory is your working directory. By using the cd command (which you'll meet in a moment) you can change your working directory.
4.3.3.2 The directory tree
The directories of a Linux system are organized as a hierarchy. Unlike MS-DOS, which provides a separate hierarchy for each partition, Linux provides a single hierarchy that includes every partition. The topmost directory of the directory tree is the root directory, which is written using a forward slash (/), not the backward slash (\) used by MS-DOS to designate a root directory.
Figure 4.3 shows a hypothetical Linux directory tree. The root directory contains six subdirectories: bin, dev, etc, home, tmp, and usr. The home directory has two subdirectories; each is the home directory of a user and has the same name as the user who owns it. The user named bill has created two subdirectories in his home directory: books and school. The user named patrick has created a single subdirectory in his home directory: school.
Figure 4.3: A hypothetical Linux directory tree
Each directory (other than the root directory) is contained in a directory known as its parent directory. For example, the parent directory of the bill directory is home.
4.3.3.3 Absolute and relative pathnames
Notice in the figure that two directories named school exist: One is a subdirectory of bill and the other is a subdirectory of patrick. To avoid confusion that could result when several directories have the same name, directories are specified using pathnames. Two kinds of pathnames exist: absolute and relative. The absolute pathname of a directory traces the location of the directory beginning at the root directory; you form the pathname as a list of directories, separated by forward slashes (/). For example, the absolute pathname of the unique directory named bill is /home/bill. The absolute pathname of the school subdirectory of the bill directory is /home/bill/school. The absolute pathname of the identically named school subdirectory of the patrick directory is /home/patrick/school.
When a subdirectory is many levels below the root directory, its absolute pathname may be long and cumbersome. In such a case, it may be more convenient to use a relative path name, which uses the current directory, rather than the root directory, as its starting point. For example, suppose that the bill directory is the current working directory; you can refer to its books subdirectory by the relative pathname books. Notice that a relative pathname can never begin with a forward slash, whereas an absolute pathname must begin with a forward slash. As a second example, suppose that the home directory is the current working directory. The relative pathname of the school subdirectory of the bill directory would be bill/school; the relative pathname of the identically named subdirectory of the patrick directory would be patrick/school.
Linux provides two special directory names. Using a single dot (.) as a directory name is equivalent to specifying the working directory. Using two dots (..) within a pathname takes you up one level in the current path, to the parent directory. For example, if the working directory is /home/bill, .. refers to the /home directory. Similarly, the path ../patrick/school refers to the directory /home/patrick/school.
4.3.4 Commands That Work with Directories
Now that you understand the fundamentals of how Linux organizes data, you're ready to learn some commands that work with directories. Rather than simply read this section, you should login to your Linux system and try the commands for yourself. Only by doing so will you begin to develop skill in working with shell commands.
4.3.4.1 Displaying the working directory
To display the current working directory, issue the pwd command. The pwd command requires no options or arguments.
root@desktop:/root#

pwd
/root
The pwd command displays the absolute pathname of the working directory.
4.3.4.2 Changing the working directory
To change the working directory, issue the cd command, specifying the pathname of the new working directory as an argument. You can use an absolute or relative pathname. For example, to change the working directory to the /bin directory, type:
root@desktop:/root#

cd /bin
[root@desktop /bin]#
Notice how the prompt changes to indicate that /bin is now the working directory.
You can quickly return to your home directory by issuing the cd command without an argument:
[root@desktop /bin]#

cd
root@desktop:/root#
Again, notice how the prompt changes to indicate the new working directory.
If you attempt to change the working directory to a directory that doesn't exist, Linux displays an error message:
root@desktop:/root#

cd nowhere
bash: nowhere: No such file or directory
4.3.4.3 Displaying directory contents
To display the contents of a directory, you use the ls command. The ls command provides many useful options that let you tailor its operation and output to your liking.
The simplest form of the ls command takes no options or arguments. It simply lists the contents of the working directory, including files and subdirectories (your own output will differ, reflecting the files present in your working directory):
root@desktop:/root#

ls
GNUstep firewall sniff
Xrootenv.0 linux ssh-1.2.26
audio.cddb mail ssh-1.2.26.tar.gz
audio.wav mirror support
axhome mirror-2.8.tar.gz temp
conf nlxb318l.tar test
corel openn test.doc
drivec.img scan tulip.c
dynip_2.00.tar.gz screen-3.7.6-0.i386.rpm win95
root@desktop:/root#
Here, the output is presented in lexical (dictionary) order, as three columns of data. Notice that filenames beginning with uppercase letters appear before those beginning with lowercase letters.
A more sophisticated form of the ls command that includes the -l option displays descriptive information along with the filenames, as shown in Figure 4.4.
Figure 4.4: Output of the ls command
The first line of the output shows the amount of disk space used by the working directory and its subdirectories, measured in 1K blocks. Each remaining line describes a single file or directory. The columns are:
Type
The type of file: a directory ( d), or an ordinary file ( -). If your system supports color, Linux displays output lines that pertain to directories in blue and lines that pertain to files in white.
Access modes
The access mode, which determines the users that can and cannot access the file or directory.
Links
The number of files or directories linked to this one.
Group
The group that owns the file or directory.
Size
The size of the file or directory, in bytes.
Modification date
The date and time when the file or directory was last modified.
Name
The name of the file or directory.
You'll learn more about access modes, links, and groups in subsequent sections of this chapter.
If a directory contains many files, the listing will fill more than one screen. To view the output one screen at a time, use the command:
ls -1 more
This command employs the pipe redirector (, explained in Chapter 13), sending output of the ls subcommand to the more subcommand, which presents the output one screen at a time. You can control the operation of the more command by using the following keys:
· Space moves you one page forward
· b moves you one page back
· q exits the program and returns you to the command prompt
If you want to list a directory other than the working directory, you can type the name of the directory as an argument of the ls command. Linux displays the contents of the directory, but does not change the working directory. Similarly, you can display information about a file by typing its name as an argument of the ls command. Moreover, the ls command accepts indefinitely many arguments, so you can type a series of directories and filenames as arguments, separating each with one or more spaces or tabs.
When the name of a directory or file begins with a dot (.), the output of the ls command does not normally include the directory or file, which is said to be hidden. To cause the output of the ls command to include hidden directories and files, use the -a option. For example, to list all the files and subdirectories in the current directory - including hidden ones - type:
root@desktop:/root#

ls -a -l
If you prefer, you can combine the -a and -l options, typing the command like this:
root@desktop:/root#

ls -al
A user's home directory generally includes several hidden files containing configuration information for various programs. For example, the .profile file contains configuration information for the Linux shell.
The ls command provides a host of additional useful options; see its manual page for details.
4.3.4.4 Creating a directory
You can create directories by using the mkdir command. Just type the name of the new directory as an argument of the command. Linux creates the directory as a subdirectory of the working directory. For example, this command creates a subdirectory named office:
root@desktop:/root#

mkdir office
If you don't want to create the new directory as a subdirectory of the working directory, type an absolute or relative pathname as the argument. For example, to create a directory named /root/documents, type:
root@desktop:/root#

mkdir /root/documents
This works regardless of the current working directory.
The name of a directory or file must follow certain rules. For example, it must not contain a slash (/) character. Directory and file names usually include letters (either uppercase or lowercase), digits, dots, and underscores (_). You can use other characters, such as spaces, but such names present problems, because the shell gives them special meaning. If you simply must use a name containing special characters, enclose the name within single quotes ( '). The quotes don't become part of the name that is stored on the disk. This technique is useful when accessing files on a Microsoft Windows filesystem; otherwise you'll have trouble working with files in directories such as My Documents, which have names containing spaces.
Most MS-DOS filenames contain a dot, but most Linux filenames do not. In MS-DOS, the dot separates the main part of the filename from a part known as the extension, which denotes the type of the file. For example, the MS-DOS file memo.txt would contain text. Most Linux programs determine the type of a file automatically, so Linux filenames don't require an extension.
4.3.4.5 Removing a directory
To remove a directory, use the rmdir command. For example, to remove unwanted, a subdirectory of the working directory, type:
root@desktop:/root#

rmdir unwanted
If the directory you want to delete is not a subdirectory of the working directory, remove it by typing an absolute or relative pathname.
You cannot remove a directory that contains files or subdirectories with rmdir; you must first delete the files in the directory and then remove the directory itself. However, the command rm -r will recursively remove the files in a directory and then remove the directory.
4.3.5 Working with Files
Directories contain files and other directories. You use files to store data. This section introduces you to several useful commands for working with files.
4.3.5.1 Displaying the contents of a file
Linux files, like Microsoft Windows files, can contain text or binary information. The contents of a binary file are meaningful only to skilled programmers, but you can easily view the contents of a text file. Simply type the cat command, specifying the name of the text file as an argument. For example:
root@desktop:/root#

cat /etc/passwd
displays the contents of the /etc/passwd file, which lists the valid system logons.
If a file is too large to be displayed on a single screen, the first part of the file will whiz past you and you'll see only the last few lines of the file. To avoid this, you can use the more command:
root@desktop:/root#

more /etc/passwd
This command displays the contents of a file in the same way the man command displays a manual page. You can use Space and the b key to page forward and backward through the file and the q key to exit the command.
4.3.5.2 Removing a file
To remove a file, type the rm command, specifying the name of the file as an argument. For example:
root@desktop:/root#

rm badfile
removes the file named badfile contained in the working directory. If a file is located elsewhere, you can remove it by specifying an absolute or relative pathname.
WARNING: Once you remove a Linux file, its contents are lost forever. Be careful to avoid removing a file that contains needed information.
The -i option causes the rm command to prompt you to verify your decision to remove a file. If you don't trust your typing skills, you may find this option helpful. Linux automatically supplies the -i option even if you don't type it.
4.3.5.3 Copying a file
To copy a file, use the cp command, specifying the name (or path) of the file you want to copy and the name (or path) to which you want to copy it. For example:
root@desktop:/root#

cp /etc/passwd sample
copies the /etc/passwd file to a file named sample in the working directory.
If the destination file already exists, Linux overwrites it. You must therefore be careful to avoid overwriting a file that contains needed data. Before copying a file, use the ls command to ensure that no file will be overwritten; alternatively, use the -i option of the cp command, which prompts you to verify the overwriting of an existing file. Linux automatically supplies the -i option even if you don't type it.
4.3.5.4 Renaming or moving a file
To rename a file, use the mv command, specifying the name (or path) of the file and the new name (or path). For example:
root@desktop:/root#

mv old new
renames the file named old as new. If the destination file already exists, Linux overwrites it, so you must be careful. Before moving a file, use the ls command to ensure that no file will be overwritten; or, use the -i option of the mv command, which prompts you to verify the overwriting of an existing file. Linux automatically supplies the -i option even if you don't type it.The mv command can rename a directory, but cannot move a directory from one device to another. To move a directory to a new device, first copy the directory and its contents and then remove the original

No comments: