For example, to get the current working directory, enter the following command: [root@ford src]# pwd /usr/local/src tar: Tape Archive If you are familiar with the pkzip program, you are
Trang 1NOTE Under Linux, you cannot abbreviate the rmdir command as rd as you can under DOS.
pwd: Print Working Directory
It is inevitable that eventually you will sit down in front of an already logged-in
workstation and not know where you are located in the directory tree To get this
information, you need the pwd command It has no parameters and its only task is to
print the current working directory The DOS equivalent is to type cd alone; however,
under bash, typing cd simply takes you back to your home directory.
For example, to get the current working directory, enter the following command:
[root@ford src]# pwd
/usr/local/src
tar: Tape Archive
If you are familiar with the pkzip program, you are used to compression tools not
only reducing file size, but also combining multiple files into a single large file
Linux separates this process into two tools The compression tool is gzip, which was
discussed earlier
The tar program combines multiple files into a single large file The reason for
separating this program from the compression tool is that tar allows you to select
which compression tool to use or whether you even want compression Additionally,
tar is able to read and write to devices in much the same way that dd can, thus making
tar a good tool for backing up tape devices
NOTE Although the name of the program includes the word tape, you do not need to read or
write to a tape drive when creating archives In fact, you will rarely use tar with a tape drive in your
day-to-day work (aside from your backups)
The format of the tar command is as follows:
[root@ford /root]# tar [commands and options] filenames
Some of options available to tar are listed in Table 21-8 Refer to the man page for
the complete list
For example, to create an archive called apache.tar containing all the files from
/usr/ src/apache, type the following:
[root@ford src]# tar -cf apache.tar /usr/src/apache
To create an archive called apache.tar containing all the files from /usr/ src/apache
and see the list of files as they are added to the archive, type the following:
[root@ford src]# tar -cvf apache.tar /usr/src/apache
Trang 2To create a gzipped compressed archive called apache.tar.gz containing all the files from /usr/src/apache and list the files as they are being added to the archive, type the following:
[root@ford src]# tar -cvzf apache.tar.gz /usr/src/apache
To extract the contents of a gzipped tar archive called apache.tar.gz and list the files
as they are being extracted, type the following:
[root@ford /root]# tar -xvzf apache.tar.gz
cat: Concatenate Files
The cat program serves a simple purpose: to display the contents of files While you
can do more creative things with it, you will almost always use the program simply to
display the contents of text files, much like you would use the type command under
DOS Because you can specify multiple filenames on the command line, it is possible to
concatenate files into a single large continuous file Thus, cat differs from tar in that the
resulting file has no control information to show the boundaries of different files For example, to display the /etc/passwd file, type the following:
[root@ford /root]# cat /etc/passwd
To display the /etc/passwd file and the /etc/group file, type the following:
[root@ford /root]# cat /etc/passwd /etc/group
To concatenate the /etc/passwd file with the /etc/group file into the /tmp/ complete file, type the following:
[root@ford /root]# cat /etc/passwd /etc/group > /tmp/complete
To concatenate the /etc/passwd file to an existing file called /tmp/orb, type the following:
[root@ford /root]# cat /etc/passwd >> /tmp/orb
Options Descriptions
-c Create a new archive
-t View the contents of an archive
-x Extract the contents of an archive
-f Specify the name of the file (or device) in which the archive is located -v Be verbose during operations
-z Assume that the file is already (or will be) compressed with gzip
Table 21-8. Common tar Command Options
Trang 3more: Display a File One Screen at a Time
The more command works in much the same way as the DOS version of the program
It displays an input file one screen at a time The input file can come from either more’s
standard input or a command-line parameter Additional command-line parameters
exist for this command; however, they are rarely used See the man page for additional
information
For example, to view the /etc/passwd file one screenful at a time, type the following:
[root@ford /root]# more /etc/passwd
To view the directory listing generated by the ls command one screenful at a time,
type the following:
[root@ford /root]# ls | more
du: Disk Utilization
You will often need to determine where and by whom disk space is being consumed,
especially when you’re running low on it! The du command allows you to determine
the disk utilization on a directory-by-directory basis Table 21-9 lists some of the
options for du.
For example, to display in a human-readable format the amount of space each
directory in the /home directory is taking up, type the following:
[root@ford /root]# du -sh /home/*
which: Show the Directory in Which a File Is Located
The which command searches your entire path to find the name of the file specified
on the command line If it finds the filename, the tool displays the actual path of the
requested file The purpose of this command is to help you find fully qualified paths
Options Description
-c Produce a grand total at the end of the run
-h Print sizes in human-readable format
-k Print sizes in kilobytes rather than block sizes (Note that under
Linux, one block is equal to 1KB However, this is not true for all flavors of UNIX.)
-s Summarize; print only one output for each argument
Table 21-9. Common du Command Options
Trang 4For example, to find out which directory the ls command is in, type the following:
[root@ford /root]# which ls
whereis: Locate the Binary, Source, and Manual Page for a Command
The whereis program not only searches your path and displays the name of the
program and its absolute directory, but also finds the source file (if available) and the man page for the command (again, if available)
For example, to find the location of the binary, source, and manual page for the
command grep, type the following:
[root@ford /root]# whereis grep
df: Determine the Amount of Free Space on a Disk
The df program displays the amount of free space on a partition-by-partition basis The drives/partitions must be mounted for df to retrieve this information You can also
gather Network File System (NFS) information using this command
Two options are commonly used with df: -h and -l The -h option specifies to use a
human-readable measurement, other than simply the number of free blocks, to indicate
the amount of free space The -l option lists only the mounted file systems that are
local; do not display any information about network-mounted file systems Additional command-line options are available; however, they are rarely used You can read about
them in the df man page.
For example, to show the free space for all locally mounted drivers, type the following:
[root@ford /root]# df -l
To show the free space in a human-readable format for the file system on which your current working directory is located, type the following (the trailing period is shorthand that means “current directory,“ just as it does under DOS):
[root@ford /root]# df -h
To show the free space in a human-readable format for the file system on which /tmp is located, type the following:
[root@ford /root]# df -h /tmp
sync: Synchronize Disks
Like most other modern operating systems, Linux attempts to improve efficiency
by maintaining a disk cache This means, however, that at any given moment not everything you want written to disk has been written to disk
To schedule the disk cache to be written out to the disk, use the sync command
If sync detects that writing the cache out to disk has already been scheduled, the tool
Trang 5causes the kernel to flush the cache immediately For example, to ensure that the disk
cache has been flushed, type the following:
[root@ford /root]# sync ; sync
The sync command does not have any command-line parameters.
Process Manipulation
Under Linux (and UNIX in general), each running program is composed of at least
one process From the operating system’s standpoint, each process is independent of
one another, and unless you specifically ask the processes to share resources with each
other, they are confined to the memory and CPU allocation assigned to them Processes
that overstep their memory allocation (which could potentially corrupt another
running program and make the system unstable) are immediately killed This method
of handing processes has been one of the key reasons that UNIX has been able to
sustain its claims to system stability for so long—user applications cannot corrupt other
user programs or the operating system
This section discusses the tools used to list and manipulate processes This
information is very useful to systems administrators, since it’s always important to
keep an eye on what’s going on
ps: List Processes
The ps command lists all of the processes in a system, as well as their state, size, name,
owner, CPU time, wall clock time, and much more The command has many
command-line parameters Table 21-10 lists the ones that are most commonly used
The most common parameter used with the ps command is -auxww, which shows
all of the processes (regardless of whether or not they have a controlling terminal),
Option Description
-a Show all processes with a controlling terminal, not just the current user’s
-r Show only running processes
-x Show processes that do not have a controlling terminal
-u Show the process owners
-f Show which processes are the parents to which other processes
-l Produce long format
-w Show the process’s command-line parameters (up to half a line)
-ww Show all of a process’s command-line parameters, despite length
Table 21-10. Common ps Command Options