Tuesday, July 1, 2008

LINUX-2

Finding a file
If you know the name of a file, but do not know what directory contains it, you can use the find command to locate the file. For example:
root@desktop:/root#

find . -name 'missing' -print
attempts to find a file named missing, located in (or beneath) the current working directory (.). If the command finds the file, it displays its absolute pathname.
If you know only part of the file name, you can surround the part you know with asterisks (*):
root@desktop:/root#

find / -name '*iss*' -print
This command will find any file whose name includes the characters iss, searching every subdirectory of the root directory (that is, the entire system).
4.3.5.6 Printing a file
If your system includes a printer, you can print a file by using the lpr command. For example:
root@desktop:/root#

lpr /etc/passwd
sends the file /etc/passwd to the printer.
If a file is lengthy, it may require some time to print. You can send other files to the printer while a file is printing. The lpq command lets you see what files are queued to be printed:
root@desktop:/root#

lpq
lp is ready and printing
Rank Owner Job Files Total Size
active root 155 /etc/passwd 1030 bytes
Each waiting or active file has an assigned print job number. You can use the lprm to cancel printing of a file, by specifying the print job number. For example:
root@desktop:/root#

lprm 155
cancels printing of job number 155. However, only the user who requested that a file be printed (or the root user) can cancel printing of the file.
4.3.5.7 Working with compressed files
To save disk space and expedite downloads, you can compress a data file. By convention, compressed files are named ending in .gz; however, Linux doesn't require or enforce this convention.
To expand a compressed file, use the gunzip command. For example, suppose the file bigfile.gz has been compressed. Typing the command:
root@desktop:/root#

gunzip bigfile.gz
extracts the file bigfile and removes the file bigfile.gz.
To compress a file, use the gzip command. For example, to compress the file bigfile, type the command:
root@desktop:/root#

gzip bigfile
The command creates the file bigfile.gz and removes the file bigfile.
Sometimes it's convenient to store several files (or the contents of several subdirectories) in a single file. This is useful, for example, in creating a backup or archive copy of files. The Linux tar command creates a single file that contains data from several files. Unlike the gzip command, the tar command doesn't disturb the original files. To create a tar file, as a file created by the tar command is called, a command like this:
tar -cvf
tarfile files-or-directories
Substitute tarfile with the name of the tar file you want to create and files-or-directories with a list of files and directories, separating the list elements by one or more spaces or tabs. You can use absolute or relative pathnames to specify the files or directories. By convention, the name of a tar file ends with .tar, but Linux does not require or enforce this convention.
For example, to create a tar file named backup.tar that contains all the files in all subdirectories of the directory /home/bill, type:
tar -cvf backup.tar /home/bill
The command creates the file backup.tar in the current working directory.
You can list the contents of a tar file by using a command that follows this pattern:
tar -tvf
tarfile more
The more causes the output to be sent to the more command, so that you can page through multiple pages. If the tar file holds only a few files, you can omit the more.
To extract the contents of a tar file, use a command that follows this pattern:
tar -xvf
tarfile
This command expands the files and directories contained within the tar file as files and subdirectories of the working directory. If a file or subdirectory already exists, it is silently overwritten.
The tar command provides a host of useful options; see its manual page for details.
It's common to compress a tar file. You can easily accomplish this by specifying the options -czvf instead of -cvf. Compressed tar files are conventionally named ending with .tgz. To expand a compressed tar file, specify the options -xzvf instead of -xvf.
The tar command doesn't use the common ZIP method of compression common in the Microsoft Windows world. However, Linux can easily work with, or even create, ZIP files. To create a ZIP file that holds compressed files or directories, issue a command like this one:
zip -r
zipfile files_to_zip
where zipfile names the ZIP file that will be created and files_to_zip specifies the files and directories to be included in the ZIP file.
To expand an existing ZIP file, issue a command like this one:
unzip
zipfile
4.3.5.8 Working with links
Microsoft Windows 9x supports shortcuts, which let you refer to a file or directory (folder) by several names. Shortcuts also let you include a file in several directories or a subdirectory within multiple parent directories. In Linux, you accomplish these results by using the ln command, which links multiple names to a single file or directory. These names are called symbolic links, soft links, or simply links.
To link a new name to an existing file or directory, type a command that follows this pattern:
ln -s
old new
For example, suppose that the current working directory contains the file william. To be able to refer to this same file by the alternative name bill, type the command:
root@desktop:/root#

ln -s william bill
The ls command shows the result:
root@desktop:/root#

ls -l
lrwxrwxrwx 1 root root 7 Feb 27 13:58 bill->william
-rw-r--r-- 1 root root 1030 Feb 27 13:26 william
The new file ( bill) has type l, which indicates it's a link, rather than a file or directory. Moreover, the ls command helpfully shows the name of the file to which the link refers ( william).
If you omit the -s option, Linux creates what's called a hard link. A hard link must be stored on the same filesystem as the file to which it refers, a restriction that does not apply to symbolic links. The link count displayed by the ls command reflects only hard links; symbolic links are ignored.
4.3.5.9 Working with file permissions
Unlike Windows 98, but like other varieties of Unix and Windows NT, Linux is a multi-user operating system. Therefore, it includes mechanisms that protect data from unauthorized access. The primary protection mechanism restricts access to directories and files, based on the identity of the user who requests access and on access modes assigned to each directory and file, which are often called permissions.
Each directory and file has an associated user, called the owner, who created the directory or file. Each user belongs to one or more sets of users known as groups. Each directory and file has an associated group, which is assigned when the directory or file is created.
Groups can be used to let users other than root perform system administration tasks or other restricted tasks. For example, Debian GNU/Linux defines the group dialout; only members of this group - and root, of course - can access the system's modem and initiate a dial-up connection. By allowing only the members of a particular group access to a program file, you can establish a flexible, yet effective security policy.
To restrict access to a file or directory, you set its permissions. Table 4.4 lists the possible permissions and explains the meaning of each. Notice that permissions work differently for directories than for files. For example, permission r denotes the ability to list the contents of a directory or read the contents of a file. A directory or file can have more than one permission. Only the listed permissions are granted; any other operations are prohibited. For example, a user who had file permission rw could read or write the file, but could not execute it.

Table 4.4: Access Permissions
Permission
Meaning for directory
Meaning for file
r
List the directory
Read contents
w
Create or remove files
Write contents
x
Access files and subdirectories
Execute
The access modes of a directory of file consist of three permissions:
owner
Applies to the owner of the file
group
Applies to users who are members of the group assigned to the file
other
Applies to other users
The ls command lists the file access modes in the second column of its long output format, as shown in Figure 4.5. The column contains nine characters: the first three specify the access allowed the owner of the directory or file, the second three specify the access allowed users in the same group as the directory or file, and the final three specify the access allowed to other users (see Figure 4.6).
Figure 4.5: Access modes as shown by the ls command
Figure 4.6: Access modes specify three permissions
You set the access modes of a directory or file by using the chmod command, which has the following pattern:
chmod
nnn directory-or-file
The argument nnn is a three-digit number, which gives the access mode for the owner, group, and other users. Table 4.5 shows each possible digit and the equivalent access permission. For example, the argument 751 is equivalent to rwxr-x--x, which gives the owner every possible permission, gives the group read and execute permission, and gives other users execute permission.

Table 4.5: Numerical Access Mode Values
Value
Meaning
0
---
1
--x
2
-w-
3
-wx
4
r--
5
r-x
6
rw-
7
rwx
If you're the owner of a file or directory (or if you're the root user), you can change its ownership by using the chown command. For example, the following command assigns newuser as the owner of the file hotpotato:
root@desktop:/root#

chown newuser hotpotato
The owner of a file or directory (and the root user) can also change the group of a file. For example, the following command assigns newgroup as the new group of the file hotpotato:
root@desktop:/root#

chgrp newgroup hotpotato
The group you assign to a file or directory must have been previously established by the root user. The valid groups appear in the file /etc/group, which only the root user can alter.
The root user can assign each user to one or more groups. When you log on to the system, you are assigned to one of these groups - your login group - by default. To change to another of your assigned groups, you can use the newgrp command. For example, to change to the group named secondgroup, use the following command:
root@desktop:/root#

newgrp secondgroup
If you attempt to change to a group that does not exist, or to which you have not been assigned, your command will fail. When you create a file or directory, it is automatically assigned your current group as its owning group.
4.3.5.10 Running programs
In Linux, as in MS-DOS and Microsoft Windows, programs are stored in files. Often, you can launch a program by simply typing its filename. However, this assumes that the file is stored in one of a series of directories known as the path. A directory included in this series is said to be on the path. If you've worked with MS-DOS, you're familiar with the MS-DOS path, which works much like the Linux path. You'll learn more about working with the Linux path in Chapter 13.
If the file you want to launch is not stored in a directory on the path, you can simply type the absolute pathname of the file. Linux will then launch the program even though it's not on the path. If the file you want to launch is stored in the working directory, type ./ followed by the name of the program file. Again, Linux will launch the program even though it's not on the path.
For example, suppose the program bigdeal is stored in the directory /home/bob, which is the current directory and which happens to be on the path. You could launch the program any of these ways:
bigdeal
./bigdeal
/home/bob/bigdeal
The first command assumes that the program is on the path. The second assumes that the program resides in the current working directory. The third makes no assumptions about the location of the file.

No comments: