This section examines some of the commands that you can use to find your way around Fedora Core 2 using the command line shell. There are different categories of commands:
Filesystem commands : Used for navigating and manipulating the filesystem
Process management commands : Used to manage processes
Administrative commands : Used to perform various administrative tasks on the system
This section takes a look at these three categories of commands and examines some frequently used examples of each.
Filesystem commands allow you to access and manipulate various parts of the UNIX filesystem tree. In UNIX, the filesystem is organized as a tree structure, in much the same way as in other operating systems (each directory can contain files and subdirectories). For each machine, there is always one root directory; this is the topmost directory, and contains subdirectories and files. Refer to the root directory by using the forward slash ( / ).
From this basic structure, there are a number of tasks that you may need to do, such as changing the working directory, establishing the contents of a particular directory, or creating, copying, moving, and deleting files and directories.
The working directory is the directory you re currently working in. To change the working directory (that is, to select a particular directory to be the working directory), you use the cd (change directory) command.
For example, to select the root directory to be the working directory, you would type the following:
$ cd /
To select the directory /home/deepakt (the home directory of the user deepakt ) to be the working directory, you could type this:
$ cd /home/deepakt
In both of the preceding examples, you ve specified the absolute path of the directory in question. You can also use relative paths. For example, if you wanted to change the working directory from /home/deepakt to its parent directory, /home , you could use the absolute path, like this:
$ cd /home
Or use the relative path, like this:
$ cd ..
You can use cd to change the working directory to any directory that the system allows you to. If you use the cd command with no arguments, it will always set the working directory to be the directory that you logged into (often this is /home/ < username >).
Note | This is different from the Microsoft DOS command prompt, where the cd command with no arguments merely prints the absolute path of the present directory. |
Now comes the question of how to determine your current directory. For this, you use the pwd (print working directory) command, which outputs the absolute path of the present working directory. For example:
$ cd /home/deepakt $ pwd /home/deepakt
You can list the contents of a directory by using the ls (list directory contents) command. If you don t specify a directory, ls will list the contents of the present working directory:
$ ls -al total 288 drwxr-xr-x 3 deepakt users 4096 Jan 16 14:29 . drwxr-xr-x 11 deepakt users 16384 Jan 16 14:27 .. -rw-r--r-- 1 deepakt users 49714 Jan 16 14:27 f.html -rw-r--r-- 1 deepakt users 22696 Jan 16 14:29 ff.html drwxr-xr-x 2 deepakt users 0 Jan 16 17:10 foo -rw-r--r-- 1 deepakt users 131498 Jan 16 14:20 showthread.php.html
The ls command has a multitude of options. The preceding example uses two options:
The “l option requests that the output is the long form ”that is, with more details than just the name of the file.
The “a option lists all the files, including those beginning with a dot ( . ). Files beginning with a dot are usually configuration files. For those familiar with Microsoft operating systems, they are vaguely analogous to hidden files in DOS.
Note | As discussed in Chapter 5, each item in the output is either a file or a directory; a directory is denoted by a d in the first position of the 10-character code in the first column. In the output, there are three directories listed: foo , the present working directory (denoted . ), and the parent directory (denoted .. ). |
When it comes to creation and removal, directories and files are handled differently, so we ll take a couple of subsections to explain.
You use mkdir (make directory) and rmdir (remove directory), respectively, to create or remove directories. The following example creates a subdirectory under the present working directory, called grapple :
$ mkdir grapple
You can also create an entire hierarchy of directories at one go. To do this, use the “p option. For example, the following command creates the directory 4wds (as a subdirectory of the vehicles/ cars directory), but it also creates the vehicles and cars directories if they do not already exist:
$ mkdir p vehicles/cars/4wds
By default, the rmdir command can remove only an empty directory, that is, one that has no files in it. This is designed to help you avoid deleting files that you didn t necessarily know were there:
$ rmdir grapple
To remove a directory and its contents recursively including the subdirectories, you can use the rm “r command:
$ rm r vehicles
A quick way to create a file is to use the touch command. The actual purpose of the touch command is to update the timestamp of an existing file (the idea being that if you touch the file, the OS should update its timestamp). A side effect of this is that if you specify the path of a file that does not yet exist, Linux also creates an empty file:
$ touch tigers.txt
To remove a file, use the rm (remove) command:
$ rm tigers.txt
To move a file or directory, use the mv (move) command. This requires two arguments: the file or directory to be moved, and the destination:
$ mv squidgy.txt squeezy.txt
This is a classic example of moving a file. It uses mv not to move the file to a different directory, but simply to change the name of the file. In Linux, moving a file really means changing its path.
It s also possible to move multiple files at the same time. For example, the following command moves the files squeezy.txt and stodgy.txt from the present working directory to the directory /etc/textures :
mv squeezy.txt stodgy.txt /etc/textures
Let s step through a simple example in which you create a directory with a file in it, and then rename the file. After you are done experimenting, you will clean everything up by deleting both the file and the directory.
Change the present working directory to your home directory. For example, if you were logged in with the username deepakt , you would simply type this:
$ cd
Just to check that the first step worked, use pwd to check the present working directory:
$ pwd /home/deepakt
Now create a subdirectory called test , and make it the working directory:
$ mkdir test $ cd test
Now create a new file called nomme.txt :
$ touch nomme.txt
To prove that the touch command really did create a new file, you can use the ls command to list the contents of the test directory:
$ ls al total 4 drwxr-xr-x 2 deepakt users 0 Jan 16 17:29 . drwxr-xr-x 3 deepakt users 4096 Jan 16 17:29 .. -rw-r--r-- 1 deepakt users 0 Jan 16 17:29 nomme.txt
Now use mv to change the name of the file, and use ls again to check the change:
$ mv nomme.txt name.txt; ls -al total 4 drwxr-xr-x 2 deepakt users 0 Jan 16 17:29 . drwxr-xr-x 3 deepakt users 4096 Jan 16 17:29 .. -rw-r--r-- 1 deepakt users 0 Jan 16 17:29 name.txt
Note | Note that we used a semicolon ( ; ) after the mv command and typed in the ls command. A semicolon is used to separate multiple commands on the same line. |
Next, to start the cleanup, use rm to remove the file name.txt , and use ls to check it:
$ rm name.txt $ ls al total 4 drwxr-xr-x 2 deepakt users 0 Jan 16 17:29 . drwxr-xr-x 3 deepakt users 4096 Jan 16 17:29 ..
Now move back to the parent directory (that is, make your user s home directory the working directory):
$ cd .. $ pwd /home/deepakt
Finally, remove the test directory that you created right at the beginning:
$ rmdir test
The following table provides a quick summary of common commands necessary for getting around the filesystem:
Command | Description |
---|---|
cd | Change directory. Without arguments, this command changes the current working directory to the user s home directory. When a directory name is supplied as an argument, it changes the current working directory to that directory. |
pwd | Prints the current working directory. |
mkdir | Creates new directories. Creates one or more directories as specified by arguments to this command. When a hierarchy is specified, the “p option creates intermediate directories if they are missing. |
rmdir | Removes an empty directory. |
cat | Concatenates the contents of a file. May be used in conjunction with the > operator to create a file or the >> operator to append to a file (see the sub-section on I/O Redirection under the Command-Line Syntax section for details). |
rm | Removes a file. The “i option prompts the user to confirm whether to proceed with deletion. The “f option is the silent remove option. It attempts to remove the file without seeking confirmation; if it is unsuccessful in removing the file, it does not report an error. |
mv | Moves one or more files or directories supplied as arguments to another directory. |
ls | Lists the contents of one or more directories supplied as arguments to it. The “l option lists the details of a file. The - “ color option lists files with color-coding describing the type of file. The “t option lists the timestamps of files and directories. The “R option recursively lists the contents of a directory. Several of the important ls options to ls were discussed in the previous chapter. |
touch | Modifies the timestamp of files or directories supplied as arguments. If a specified argument does not exist, touch creates an empty file by that name in the current directory. |
UNIX (and therefore Linux) has processes that are essentially tasks running on the operating system infrastructure. Each time a command is invoked, the invocation results in one or more processes being spawned. Processes belong to users, and in general are insulated from one another. Often, the only time general users of the system are concerned with processes is when one refuses to respond; this is commonly referred to as a program hang .
To list the current processes spawned that are started from a particular shell or its parent shell, you can use the ps (processes spawned) command:
$ ps PID TTY TIME CMD 5355 pts/0 00:00:00 bash 5381 pts/0 00:00:00 ps
To list all the processes on the system, you can use the ps command with the “A option:
$ ps -A PID TTY TIME CMD 1 ? 00:00:04 init 2 ? 00:00:00 keventd 3 ? 00:00:03 kapmd 4 ? 00:00:00 ksoftirqd_CPU0 5 ? 00:00:03 kswapd 6 ? 00:00:00 bdflush 7 ? 00:00:00 kupdated 8 ? 00:00:00 mdrecoveryd ...
Note | Note that this option is case-sensitive. |
Processes represent commands that were executed. But what ps does not tell you (at least not easily) is what the complete command line was when the process was invoked. Sometimes, you need to know what arguments were passed to the command and what options were invoked. To see the complete command line of a process with the name of the owner of the process , you can coax the ps command with the “auxww options:
$ ps -auxww USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.2 1336 428 ? S Jan15 0:04 init ... root 449 0.0 0.3 2056 756 ? S Jan15 0:00 /sbin/dhclient -1 -q -lf /var/lib/dhcp/dhclient-eth0.leases -pf /var/run/dhclient-eth0.pid -cf /etc/dhclient-eth0.conf eth0 root 619 0.0 0.2 1328 412 ? S Jan15 0:00 /usr/sbin/apmd -p 10 -w 5 -W -P /etc/sysconfig/apm-scripts/apmscript ident 657 0.0 0.3 27032 708 ? S Jan15 0:00 identd root 671 0.0 0.4 3276 860 ? S Jan15 0:00 /usr/sbin/sshd root 685 0.0 0.3 2096 752 ? S Jan15 0:00 xinetd -stayalive -reuse -pidfile /var/run/xinetd.pid deepakt 5270 0.0 3.1 16436 5952 ? S 12:03 0:04 /usr/bin/gnome-session deepakt 5313 0.0 0.3 2900 724 ? S 12:03 0:00 /usr/bin/ssh-agent /etc/X11/xinit/Xclients
Now, the final column gives the complete command used. For example, the lines shown in bold show the complete command used to invoke the DHCP client daemon. What you should note in all of these cases is the presence of the PID column. The Process ID (PID) number is used to uniquely identify a process at any given time.
You can use the kill command to send a signal to a process by specifying the signal to be sent and the Process ID. The signal that most general users are interested in is the SIGTERM signal, which terminates the process. This is particularly useful for dealing with a process that is hanging or has run amok:
$ ps PID TTY TIME CMD 5355 pts/0 00:00:00 bash 5365 pts/0 00:00:00 haywire 5381 pts/0 00:00:00 ps $ kill s SIGTERM 5365 $ ps PID TTY TIME CMD 5355 pts/0 00:00:00 bash 5381 pts/0 00:00:00 ps
The SIGTERM signal can be handled by processes. In other words, it allows processes to clean up before exiting; for example, the process may want to close the files it has opened before exiting. If the safer option of SIGTERM does not work, you can resort to SIGKILL . The SIGKILL signal forces a process to terminate immediately ”it does not allow it to exit gracefully. The man 7 signal command lists and describes the complete set of signals.
The top command allows you to see which processes consume maximum resources on the system (see Figure 6-1). The top command refreshes the screen periodically because the resource consumption on a system usually fluctuates rapidly .
It is Linux s administrative commands that set it apart from GUI-based operating systems. These commands are as numerous as they are powerful. This section examines only the most important administrative commands (in terms of day-to-day use). More commands will be introduced in later chapters, especially in Chapter 11.
For day-to-day activity on your Linux system, it s generally a good idea to log in using a regular user account ”that is, one that has no administrative privileges. (For example, this protects you from the dangers of accidentally deleting or changing important files and configuration.)
However, to perform certain privileged tasks, such as shutting down the system, you need to assume the identity of an administrator (or root user, in UNIX parlance). In these situations, it s rather inconvenient to have to log out of the system and log back in as a root user. There is a simpler alternative ”you can use the su command (with no arguments) and supply the root user s password. This provides you with a shell that has the privileges of the root user. When you ve completed your administrative tasks, you can switch back to the safety of non-admin privileges by exiting the root shell by typing exit or Ctrl+D .
The su command can also be used to switch identities between two non-root users. The id command prints the current user ID of the user:
$ id uid=500( brucewayne ) gid=513(users) groups=513(users) $ su Password: bash# id uid=0( root ) gid=0(root) groups=0(root), 1(bin), 2(daemon) bash# exit $ id uid=500( brucewayne ) gid=513(users) groups=513(users) $ su batman Password: $ id uid=800( batman ) gid=513(users) groups=513(users)
The uid field indicates the user ID of the user, which is a unique number, assigned to each and every user on the system. The gid is the group ID of the primary group that a user belongs to. A group is essentially a collection of users who have some common set of privileges. For example, all members of the engineering group may have access to the blueprints directory on a machine. Each user belongs to at least one group, which is known as the primary group. In addition, they may also belong to other groups as indicated by the groups field. Groups, such as users , have a number associated with them; in the preceding code, 513 denotes the group ID of the users group.
The shutdown command shuts down the system. This command is restricted to root users only. Shutdown can be immediate (in which case the now argument is passed), or it can be delayed (by n seconds by passing the “t n option).
If you need to reboot rather than shut down the system, the reboot command comes in handy. It is usually a good idea to run the sync command before rebooting or shutting down the system. The sync command ensures that filesystem objects are in a consistent state.
The df command displays a partition-by-partition summary of disk usage. You can supply arguments such as “k to indicate to df that it should report disk usage in increments of kilobytes, as shown here:
$ df -k Filesystem 1K-blocks Used Available Use% Mounted on /dev/hda2 5716172 3424988 2000812 64% / /dev/hda1 101089 19137 76733 20% /boot none 94752 0 94752 0% /dev/shm /dev/cdrom 659488 659488 0 100% /mnt/cdrom
The Available column indicates remaining disk space. In this case, roughly 2GB of disk space remains free on the root partition.
Often, you need to read from CD-ROM discs, and to read from and write to floppy disks. Because Linux treats all peripheral devices as filesystems, you must mount or associate these physical devices to a filesystem mount point ”essentially a directory. The mount command can be used to mount such media. You must have root user privileges to mount and unmount devices.
Here s an example. Start by trying to use ls to list the contents of /mnt/cdrom :
# ls /mnt/cdrom
At this stage, no files are listed because there is no device associated with the /mnt/cdrom mount point. Now put a disc in, mount the CD-ROM device to the /mnt/cdrom mount point, and list the contents again:
# mount /dev/cdrom /mnt/cdrom # ls /mnt/cdrom Fedora Autorun README
The exact listing you get depends on which CD is in your CD-ROM drive.
The complementary command is the umount command, which disassociates a device from a filesystem mount point:
# umount /mnt/cdrom
The eject command is also handy for CD-ROM devices because it unmounts the device and pops the tray open (on condition that the underlying CD-ROM drive supports this operation).
Note | As you saw in the previous chapter, when you insert a CD into the drive Fedora automatically mounts the CD. The preceding commands may be used to do this manually. |
Often, administrators (and even regular users) need to see the list of users currently logged on to the system. (This is a little counterintuitive to users familiar with desktop operating systems because the UNIX model allows users to connect to a system remotely using programs such as telnet and rlogin .)
You can achieve this with the who command, which lists all users currently logged on to a machine:
# who deepakt :0 Jan 13 12:03 hbatman 192.168.1.100 Jan 11 11:30
The finger command goes one step further, listing users logged on to a remote system somewhere on the network. For finger to work, the remote server should have the fingerd daemon running; this is becoming rarer by the day in these times of increased system security.