Sie sind auf Seite 1von 37

1

Chapter 2 Linux Command Line Basics


2.1 Overview System administrators are responsible for server reliability, performance, and security. To fulfill these responsibilities, a system administrator must be able to monitor and control a computer system. System administrators control and monitor Linux servers by issuing commands. He or she can issue the commands directly using the command line interface or indirectly using a GUI interface. The command line interface is nearly identical in every Linux distribution. Linux distributions include one or more GUI desktop managers. The most common managers are GNOME and KDE. The appearance and behavior of the desktop environment varies considerably depending on which desktop manager is selected. By default, when you start Fedora it opens to the GNOME interface (If you are not already logged into Fedora, you should login using an account without root privilege). The examples in this chapter primarily use the command line interface. To follow along with the examples you must access a command line prompt. At the top of the Gnome desktop is a menu bar. This menu bar has menus for Applications, Places, and System. The last entry in the applications menu is System Tools. When you place your mouse over this entry a submenu appears. In the System Tools submenu select Terminal to access the command line in Fedora.

2 The Linux command line interface provides users with an easy, quick, efficient way to manage and use the Linux machines. In fact, many Linux servers dont even have a GUI installed, or GUI interfaces are not available for remote access and remote administration. In addition, the Linux GUI interface does not cover all system functions, and it may take many steps to implement a command line action. Linux administrators need to have a good understanding of Linux commands and know when and how to use them in the command line interface. Most Linux server administrators prefer the command line rather than GUI interface for most activities. This includes day to day operations and job automations such as script for backup and recovery, system initializations, configuration, and performance monitoring. Linux provides a rich set of commands for system administrators to use. This chapter will only discuss the day to day basic operation commands. Commands specific to networking, process, system automation, and security will be discussed in other chapters. When you launch a terminal window as described above, most of the window will be blank except for the prompt where you type in a command to be executed. In the example below the terminal window initially displays [john@localhost ~]$. The beginning is an indication of the username. In this case, john. After the @ is an indication of the machine currently connected to. In this case, localhost means the machine you are currently sitting at. If you had connected to a remote host, its name would be displayed after the @ sign. Following this is an indication of the current working directory. The tilde is a symbol indicating the users home directory. The $ sign indicates that we are logged in as an unprivileged user. A root user would have # sign for its prompt instead of a $. To test the command line interface, type in a simple command: whoami followed by a return or enter. The computer should respond by displaying the username of whoever is logged in. Note that in Linux all commands are case sensitive; so if you enter Whoami or WHOAMI, you will receive a command not found notice.
[john@localhost ~]$ whoami john [john@localhost ~]$

Sometimes you will get a command not found error even when you have spelled a command correctly and used the right case. This happens when the command is not found in the search path. You can run these commands by specifying their location, if you know the location. To find the location of a command use the whereis command as shown below.
[john@localhost john]$ useradd bash: useradd: command not found [john@localhost john]$ whereis useradd useradd: /usr/sbin/useradd /usr/share/man/man8/useradd.8.gz [john@localhost john]$ /usr/sbin/useradd

The useradd command does require privilege to run. If you do not have the necessary privilege you will get a Permission denied error message.

3 To obtain privileges in a terminal session, you can switch to the root user by typing su at the shell prompt. The computer will prompt you to enter the password of the root account.
[john@localhost ~]$ su Password:

After you provide the root password you become a root user. Note the # sign in the prompt. Verify that you are logged in as the root user by typing whoami at the prompt.
[root@localhost john]# whoami root

As root you have the proper privileges to run useradd. However, if you try to run useradd by typing /usr/sbin/useradd at the prompt you will get a message about improper usage, because various options need to be specified when adding a user. You do not need root privilege to do most of the commands in this chapter so logout of the root account by typing exit at the prompt.
[root@localhost john]# exit exit [john@localhost ~]$

To determine the proper usage and syntax of command use the man command. The man command will explain a command and the options that can be specified when running the command. Type man useradd at the shell prompt.
[john@localhost john]$ man useradd

Examining the explanation of useradd may be a little confusing. You can scroll down in the man page using the down arrow (moves one line) or the spacebar (moves down a screen at a time). Scroll up using the up arrow. When you have finished reading the man page type the letter q to quit. Useradd is a command that is probably best run using a script file or by using a graphical interface. You did this in the last chapter when you added bill and susan as users. For most people, it is easier and less error prone to click the add user button and fill out the form in the graphical interface, rather than specifying all the options on the useradd command. Once an account is setup the user may want to change the password of the account. Users can change their passwords at anytime using the passwd command. Usage of the passwd command is illustrated below. Password values typed in are not echoed on the screen to prevent others from reading the value that the user types in.
[john@localhost ~]$ passwd Changing password for user john. Changing password for john. (current) UNIX password: New UNIX password:

4
BAD PASSWORD: it is too short New UNIX password: BAD PASSWORD: it is based on a dictionary word New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully.

After the system validates the old password and confirms the new password the computer will update your password. This is done by changing the value stored in the etc/passwd file for the account. 2.2 Files and Directories 2.2.1 Listing, Making, and changing directory Managing a Linux system consists primarily of managing files and processes. We will discuss process manipulation commands later in this chapter. This section looks at file system commands. In Linux, all data files, program files, directories, and devices are called files and treated the same way by the operating system. The files and directories (which are also files) are organized in an inverted tree type hierarchical structure. The top of the hierarchy is called root (represented by a slash / ) . Here are the subdirectories under the / directory that you should have in your newly installed Fedora Linux system.
bin dev home mnt etc lib media proc sbin srv tmp var boot opt root selinux sys usr

A number of these directories are important. The /bin directory contains programs (in the form of executable files called binaries) that the system needs to operate such as the shells and some of the shell commands, such as ls and grep. Configuration files such as the passwd file are located in the /etc directory. The home directory is the parent directory of all user home directories except the root which has a dedicated home directory called root. 2.2.1.1 Listing and Changing Directories ls (list) command lists the contents of your current working directory. When you login, your current working directory is your home directory. This directory will have the same name as the account used to login. A users home directory will be located in the /home directory of the file system. You should save your own files and any subdirectories that you create within your home directory. cd (Change Directory) command is used to change the current working directory. The root user can freely move every where within the system but a regular user cant move to another users home directory unless he is granted the permission. You can verify that the root account can view your directory by the su command to become root and then issuing the ls command at the prompt. This is shown below to list the contents of Johns directory.

5
[john@localhost ~]$ su Password: [root@localhost john]# ls Desktop Documents Download Videos

Music

Pictures

Public

Templates

If you want to find detailed information about the contents of Johns home directory, specify the l option.
[root@localhost john]# total 32 drwxr-xr-x 2 john john drwxr-xr-x 2 john john drwxr-xr-x 2 john john drwxr-xr-x 2 john john drwxr-xr-x 2 john john drwxr-xr-x 2 john john drwxr-xr-x 2 john john drwxr-xr-x 2 john john ls -l 4096 4096 4096 4096 4096 4096 4096 4096 2008-05-20 2008-05-15 2008-05-15 2008-05-15 2008-05-15 2008-05-15 2008-05-15 2008-05-15 08:50 12:00 12:00 12:00 12:00 12:00 12:00 12:00 Desktop Documents Download Music Pictures Public Templates Videos

The option l shows a long listing including Column 1: the permissions of the directory or file Column 2: the number of links or directories within the directory; for a directory, the default amount of directories is 2 because of the . (current) and .. (parent) directories Column 3: The owner of the file or directory who created it or is re-assigned as the owner. Column 4: the group owner assigned to the file or directory Column 5: size of the file or directory Column 6: date of last modification Column 7: time of last modification Column 6: the name of the file or directory. Switch to using Johns account by typing exit and reissue ls l command to verify that you get the same results.
[root@localhost john]# exit [john@localhost ~]$ ls total 32 drwxr-xr-x 2 john john drwxr-xr-x 2 john john drwxr-xr-x 2 john john drwxr-xr-x 2 john john drwxr-xr-x 2 john john drwxr-xr-x 2 john john drwxr-xr-x 2 john john drwxr-xr-x 2 john john exit -la 4096 4096 4096 4096 4096 4096 4096 4096 2008-05-20 2008-05-15 2008-05-15 2008-05-15 2008-05-15 2008-05-15 2008-05-15 2008-05-15 08:50 12:00 12:00 12:00 12:00 12:00 12:00 12:00 Desktop Documents Download Music Pictures Public Templates Videos

The ls-l command does not list all the files in Johns home directory. To see the hidden files use a option to request that all files be included in the listing. Below, both l & a are

6 specified as options by entering ls la at the command prompt. Only part of the resulting list is shown. When you enter it on your system, you may want to scroll back to review all the files listed in the directory.
[john@localhost ~]$ ls -la total 152 drwx------ 26 john john 4096 drwxr-xr-x 3 root root 4096 -rw------- 1 john john 53 -rw-r--r-- 1 john john 18 -rw-r--r-- 1 john john 176 -rw-r--r-- 1 john john 124 drwxr-xr-x 2 john john 4096 . . . [john@localhost ~]$

2008-05-21 2008-05-15 2008-05-20 2008-02-29 2008-02-29 2008-02-29 2008-05-15

21:54 04:55 15:22 09:27 09:27 09:27 12:00

. .. .bash_history .bash_logout .bash_profile .bashrc .config

There are additional options for the ls command. You can find out about the available options by typing man ls at the shell prompt. It is useful to be able to switch the current working directory. Linux provides the cd command to change directories. Some common ways to change to a desired directory are given below. cd / cd or cd ~ cd .. cd <target> moves to the root directory. without destination moves user to his home directory. moves user to the parent directory of the current directory. moves user to the target directory.

You can use cd ~/subdir1 to move you to the sub directory subdir1 in your home directory from anywhere. After you move to a new directory, you can use pwd (Present Working Directory) command to check the directory you are currently in. Here are some examples. The root user moves from johns home directory to his parent directory /home.
[john@localhost Password: [root@localhost [root@localhost [root@localhost /home ~]$ su john]# john]# cd .. home]# pwd

The root then moves to susans home directory.


[root@localhost home]# ls bill john susan [root@localhost home]# cd susan [root@localhost susan]# pwd /home/susan

7 Any user home directories can be referred to by the tilde (~) character no matter where you are. If you want to list the files in the myApp subdirectory of your home directory, you could use this command. ls ~/myApp When listing files or doing other actions on a computer, it is sometime convenient to not fully specify the file name. Wildcard characters are used in these situations. Wildcards such as * or ? tell the computer it can substitute other characters into a particular position when matching the string. The wildcard character * says you can substitute any number of characters (including no characters) into in a file (or directory) name. The wildcard character * y itself would match all files and directory names in the current working directory. The file command reports what type of file a specified file is. Some possible file type types are: directory, ASCCII text, and executable. To specify that you would like to know the file types of all files in a directory use the wildcard *. Here is the result of entering file * at the prompt.
root@localhost john]# file * abc: empty Desktop: directory Documents: directory Download: directory file1: empty file3: ASCII text js: ASCII English text Music: directory mydir: directory mytest.sh: ASCII text Pictures: directory Public: directory Templates: directory Videos: directory

Tying file D* will report the file type of all files in the current directory starting with an uppercase D*. If any files in the directory had a name of D with no additional letters, it would be included in the files reported on by the file command/ file *D Would report on any files in the current directory ending with an uppercase D including any files named D with no preceding letters. The ? wildcard character matches exactly one character. Thus abc? matches files like abc0 or abc1, but not abc without any letters after it of abc00 with two characters after abc.

8 2.2.1.2 Permissions Linux is a multi-user operating system so it is important that the permissions be set on files and directories to allow appropriate users to share files, but prevent other users from accessing files. By default an unprivileged user cannot access files in another users home directory. Access is prevented by the use of access controls called permissions in Linux. Each file or directory is restricted by permission settings. Linux sets permission for three categories: user (owner), group (this category includes members of the same group as the owner), others (anybody else). The owner of a file has whatever permissions were granted to the user. Members of the same group as the owner have the access rights specified by the group permission settings. Any user who is not the owner or a member of the group have the access rights specified by the permissions assigned to others. File type Definition Regular file d Directory l Link b Block device c Character device Table 9.1 Valid file types When you typed ls l or ls la earlier, you saw the permissions for each file or directory listed. Each entry for a file or directory began with something like drwxr-xr-x A d in the first column indicates that an entry is for a directory. If a line starts with the letter l that entry is a symbolic link (links will be discussed later in the text). If a line starts with a hyphen instead of a letter that entry is a regular file. The next 9 characters, are either the letters r, w, x or a hyphen. These characters indicate who can read, write, or execute a file. The first three permission characters indicate what the owner who created the file can do: r (read), w (write), and x (execute). A hyphen tells no permission in its category. The next three letters indicate the permissions for the group the file is assigned to. The last three letters are used for anybody else.

Figure 9.1 A directory with permissions set to 766 When you create a file in your home directory, the file has a default permission of -rwrw-r-- . The owners permissions are rw-, which means the owner can read and write the file but cannot execute it. If you want to be able to run the file, you must change the

9 permissions of the file. Linux provides the chmod (change mode) to reset the permissions on a file. To demonstrate the use of the chmod command we will create a short script and try to run it. Create mytest.sh shell using the cat command. The >mytest.sh after cat redirects what you type on the next few lines into a file called mytest.sh. Type the next two lines as shown below: ca shows the calendar and who | wc l reports the number of login users on this server.
[john@localhost ~]$ cat>mytest.sh cal who | wc -l

After typing these two lines press the Ctrl key and the letter D at the same time (We will abbreviate this key combination as ^D). Entering a ^D indicates you have finished your entry and the text entered is saved to the mytest.sh. Now try to execute the script as follows:
[john@localhost ~]$ ./mytest.sh bash: ./mytest.sh: Permission denied

It should fail to execute and give you a Permission denied error message. Check the permission of this file as follows. [john@localhost ~]$ ls -l mytest.sh -rw-rw-r-- 1 john john 14 2008-05-24 16:37 mytest.sh The permission indicates that no one has execute permission, not even the owner. Now add the execute privilege for the owner using the chmod command. u indicates that you are changing permission for the owner (user) of the file and the +x that you want the owner to add execute on the specified file. Then check the permissions using ls l.
[john@localhost ~]$ chmod u+x test.sh [john@localhost ~]$ ls -l mytest.sh -rwxrw-r-- 1 john john 14 2008-05-24 16:37 mytest.sh

Some system administrators prefer to use an octal number to specify the permissions assigned using the chmod command. The octal equivalent of rwxrw-r-- is 764. You obtain this by determining the binary and then converting to octal. To get the binary, set each letter to a 1 and any hyphens to zero. Thus rwxrw-r-- becomes 11110100. 11110100 in binary is equivalent to 764 in octal. Thus we could have set the permissions on mytest.sh, using this command: chmod 764 mytest.sh

10

Number Equivalent --0 --x 1 -w2 -wx 3 r-4 r-x 5 rw6 rwx 7 Table 9.2 Numeric permissions

Definition No permissions Execute only Write only Write and execute Read only Read and execute Read and write Read, write, execute

Once execute is granted to the owner using either of the chmod syntaxes given above, the owner should be able to run the script as follows:
[john@localhost ~]$ ./mytest.sh May 2008 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 3

Other unprivileged users should not be able to run the script. For instance, if you use su to login as susan and attempt to run the program it will fail to execute due to the lack of permission. [john@localhost ~]$ su susan Password: [susan@localhost john]$ ./mytest.sh bash: ./mytest.sh: Permission denied [susan@localhost john]$ exit To allow anyone to run the script, specify +x with the chmod command.
[john@localhost ~]$ chmod +x mytest.sh [john@localhost ~]$ ls -l mytest.sh -rwxrwxr-x 1 john john 14 2008-05-24 16:37 mytest.sh

11

This allows susan to run the script.


[john@localhost ~]$ su susan Password: [susan@localhost john]$ ./mytest.sh

In Linux, a directory is a special kind of file. Like other files you can set the permissions of a directory. The read access for a directory is the permission for browsing the directory and listing files in the directory. The write access of a directory is the permission for deleting or copying files in the directory. Execute access is for permission to move into the directory and read files if the read access right is granted for the file to be read. You must have execute permission on a directory hosting the file to be able to read a file. With many files and directories on a system it can be a lot of work to reset permissions on each individually. Linux provides a recursive option to apply mode changes to all subdirectories in a directory tree. The recursive option of chmod is invoked by specifying R on the command line. The chmod command lets you change permissions for a group by specifying g when setting permissions (for instance: chmod g+rwx). Sometimes a file belongs to the wrong group. Linux provides a chgrp(change group) command for these situations. As shown below chgrp is used to change the group owner of mytest.sh to the group linux. Then chmod is used to give the group read, write, execute permissions to the group owner of mytest.sh.
chgrp linux mytest.sh chmod g+rwx mytest.sh

If you need to change the owner of a file, use the chown(change owner) command to specify a new owner. Lets practice the commands you have learned so far. Here susan moves to the home parent directory and checks the permissions of the all user home directory. The user susan then fails to move to bills home directory due to the lack of the required permission. The su command is used to switch to the root user, which is able to move into bills directory because of privileges associated with the root account.
[susan@localhost john]$ cd .. [susan@localhost home]$ ls -l total 12 drwx------ 4 bill bill 4096 2008-05-23 22:07 bill drwxrwx--- 30 john linux 4096 2008-05-24 18:34 john drwx------ 4 susan susan 4096 2008-05-24 16:45 susan [susan@localhost home]$ cd .. [susan@localhost /]$ pwd / [susan@localhost /]$ cd [susan@localhost ~]$ pwd

12
/home/susan [susan@localhost home]$ cd bill bash: cd: bill: Permission denied [susan@localhost ~]$ su Password: [root@localhost susan]# pwd /home/susan [root@localhost susan]# cd /home/bill [root@localhost bill]# pwd /home/bill [root@localhost bill]# cd ..

The root user changes the group owner of the bill directory from bill to linux and changes the bill home directory permission to executable for group owner. Since susan is a member of linux group, susan can move into the bill directory now.
[root@localhost home]# chgrp linux bill [root@localhost home]# ls -l drwx------ 4 bill linux 4096 2008-05-23 drwxrwx--- 30 john linux 4096 2008-05-24 drwx------ 4 susan susan 4096 2008-05-24 [root@localhost home]# chmod -g+x bill [root@localhost home]# ls -l drwx--x--- 4 bill linux 4096 2008-05-23 . . . [root@localhost home]# su susan [susan@localhost home]$ cd bill [susan@localhost bill]$

22:07 bill 21:44 john 21:45 susan 22:07 bill

2.2.1.3 Create, Remove, and Copy File and Directory 1) Creating Directories and Files Linux provides a number of commands that system administrators can use to create files and directories. These include mkdir, touch, editing a new file, and cat. Once you have created files on a system you may need to move or copy them to new locations. When files are no longer needed they should be deleted. Linux also provides commands for moving, copying and deleting files. These file manipulation commands are covered in this section. To create a directory in Linux, use the mkdir (Make Directory) command. The syntax is mkdir followed by the name of the directory you want to create. For instance, if susan wanted to create a subdirectory in her home directory called dir1, she would navigate to her home directory using cd without any options specified and then use mkdir dir1 to create the desired subdirectory. She could then navigate to the new directory, which should be empty.

13
[susan@localhost bill]$ ls -l total 0 [susan@localhost ~]$ mkdir dir1 [susan@localhost ~]$ ls -l total 4 drwxrwxr-x 2 susan susan 4096 2008-05-29 12:15 dir1 [susan@localhost ~]$ cd dir1 [susan@localhost dir1]$ pwd /home/susan/dir1

Creating a File As noted above there are many ways to create files in Linux. One way is to use the touch command. If you specify a filename that does not exist Linux will create a new empty file with the specified filename. The intended use of touch was to change the timestamp of a file. If a file exists, it does not create a new file it only changes the timestamp associated with the file. Try using touch to create a file and then update its timestamp (you will have to wait a minute before issuing the second touch command to see the time change).
[susan@localhost dir1]$ ls -l total 0 [susan@localhost dir1]$ touch file1 [susan@localhost dir1]$ ls -l total 0 -rw-rw-r-- 1 susan susan 0 2008-05-29 12:34 file1 [susan@localhost dir1]$ touch file1 [susan@localhost dir1]$ ls -l total 0 -rw-rw-r-- 1 susan susan 0 2008-05-29 12:35 file1

You can use any editor to create a file. Two very popular editor on UNIX systems are vi and GNU emacs (Editor MACroS). The vi is somewhat confusing for new users, but once you learn to use it vi is a powerful editing tool. To use vi to create a new file type vi myNewFile at the shell prompt. Type a or i to enter append or insert mode. Type in a few words while in the append or insert mode. When you finish typing the text press the esc key to exit from append or insert mode. Then type :wq to write your changes to the file and quit from vi editor. An online introduction to the vi editor for system administrators is available at http://www.adminschoice.com/docs/vi_editor_ref.htm GNOME supplies its own editor gedit. It is the default editor so if you double click a text file on the GNOME desktop it will open in gedit. To invoke the editor from the command line type gedit followed by the name of the file you would like to edit. You can invoke the gedit editor from GNOME desktop menu by selecting Application -> Accessories -> text Editor. Another technique frequently used in Linux to create files is the cat command. The cat command was originally used to concatenate multiple files and display them on the screen. It can be used to create a new file by the file redirection as we demonstrated in the previous section.

14 Linux allows users to redirect where input comes from and where error messages and output go. Most Linux commands write to the standard output (monitor screen) -stdout and take their input from the standard input (keyboard) -stdin, and the standard error stderr is sent to the monitor screen by default. The cat command normally takes what is in stdin and copies it to stdout. but you can redirect the stdout to a file by using the > symbol to redirect the stdout to the file named newFile as
[susan@localhost dir1]$ cat > newFile Unix Linux Windows Apple

When you finish typing the four words to be included in the file, use ^D (The ^ is used to indicate the control or ctrl key) to end the input. To copy the contents of the file to the stdout (the monitor screen), so you can see it type: cat newFile If a file exists, using the > will erase the current contents of the specified file and replace it with the new text entered. If you want to append your data to the original contents instead of replacing it use >>. To add more items to the file newfile, type:
[susan@localhost dir1]$ cat >> newFile Solaris HP Fedora

Remember to type ^D to terminate entry after you have typed Fedora. To read the contents of the expanded file, type: cat newFile To combine the file you just created (newFile) with the file you created with the vi editor (myNewFile) and create a file called merge, you would use the cat command like this: cat newFile myNewFile > merged The > and >> symbols were used to redirect output, to redirect input the < symbol is used instead. For instance, to email the merged file that was just created to john, type:
[susan@localhost dir1]$ mail john <merged

2) Copying files and directories

15 The cp command makes a copy of a file. The command below would make a copy of a source file named merged to destination file named copy. Since no directory was specified both files are located in the current working directory. (since you do not have a file named source in your directory you will get an error message when you attempt this command) cp merged destination You can use the cp command to copy a file residing in another directory to current working directory. (no need to issue this command) cp ~/dir1/file1 . You can also use the cp command to copy an entire directory including all subdirectories in the tree. (no need to issue this command) cp -r dir1 dir2 The -r option copies recursively, causing dir1 and all contents to be copied to dir2. 3) Moving files The mv (move) command moves a file from one directory to another directory or simply renames a file in the same directory. mv file1 file2 This moves (or renames) file1 to file2. The source file will not exist after moving. This differs from the copy command that preserves the original file. 4) Removing files and directories The rm (remove) command deletes a file. cp myOldFile myNewFile ls rm myOldFile ls This copies myOldFile to a new file called myNewFile. The original file is then deleted using the rm command. Think about it How could you perform the same action use the mv command? cp myOldFile myNewFile rm myOldFile (no need to issue this command)

16 The rmdir command is used to remove directories. rmdir dir3 The rmdir command will only remove a directory if it is empty. To clear a directory of all its contents so that it is empty, you can use rm with a wildcard, like this: rm * 5) Displaying file The cat command can be used to display file contents to the screen, but if a file is long it will scroll by faster than users can read it. Linux provides several ways for users to review the contents of large files. These include less, more, head, and tail. less myNewFile Both less and more allow users to review file contents one page at a time. The first page of a file will be displayed on the screen, press the space-bar to continue the next page; type the letter q if you want to quit reading. With the less command you can scroll backwards in a file The head command writes the first few lines of a file. The default number of displayed lines is 10. head -3 myNewFile Another command, tail, is used to display the last few lines of a file. tail myNewFile Of course, you can use any Linux editors to open the file and display the file on the screen. 2.2.2 Searching Linux supports many tools to assist system administrators to locate files and directories. These tools allow an administrator to specify file attributes such as name, size, type, and modification history. Linux also provides commands to locate the matched contents in files based on a search pattern. Beyond just identifying files matching a criteria, some of the commands will take actions on the located files or on matching lines in the files. 2.2.2.1 Searching for files The find command is used to locate files or directories with the supplied search criteria within the specified search range. You can search for files by name, owner, group, type, permissions, date, and other file attributes. The search is conducted recursively down to all subdirectories from the starting point. The syntax looks like this:

17 find <search-start-point> <criteria> <action> The default for search-start-point is the current working directory (.) , the default for <criteria> is none and <action> is print: Here is an example of a find command using a search criteria with default action: find / -name dir* It will print all file names starting with dir starting from the root directory /. To find files over 10Mb in size, type: find . -size +10M To find files which anybody can run, type: find . perm +o=x To find the names of files that were modified in last three days, type: find . mtime -3 To find all files which have not been modified for more than two weeks. find / type f mtime +14 To find all file names with extension of tmp and moves these files to a directory named recycle to be removed later. find . name *.tmp -exec mv {} recycle \; The set of brackets ({}) is a result set that holds the names of each file found, and the slash-semicolon (\;) means the end of the command. 2.2.2.2 Searching for contents of a file You can use every editor such as vi or emacs to search for a pattern in a file. There are also many other ways to search the contents of a file. For instance, you can use less command to conduct a simple search, by entering: less myNewFile then type /<seach pattern>

18

It highlights the first match and then you can find next match by typing n. You can keep typing n to continue advancing to the next occurrence of the search pattern. 1) grep command The grep command is a powerful line based search utility that can search through files and folders and check which lines in those files match a specified search patterns. The grep command outputs the filenames and the line numbers or the actual lines that matched the specified pattern. Patterns are specified using regular expressions. Table 9.3 lists some of the valid symbols that can be used in a regular expression. grep <search pattern> <file> The <search pattern> above is a regular expression enclosed in a set of single quotes. Regular expression symbol ^ $ . * Meaning Match the beginning of the line Match the end of the line Match a single char Match multiple occurrence of the previous char(include no occurrence) [] Match any char enclosed Table 9.3 Regular expression symbols To output the lines that start with the or The in a file, type: grep '^[tT]he' myNewFile Some other grep examples are: grep -n shell * grep ll printf *.c 2.2.2.3 sed (Stream EDitor) utility The sed is another powerful line based stream editor (non-interactive) that can do all kinds of manipulations on files in addition to searching mechanisms. The regular expression is also used in sed for search criteria. To replace linux by Linux in all txt files in the current directory, type: sed s/linux/Linux *.txt To remove, the leading <tab> in file 1, type:

19 sed s/^ // file1

To delete the first five lines in file2, type: sed 1,5d file2 There are many other utilities available in Linux such as awk which reads from a file or from its stdin, and outputs to its stdout. You nay redirect that into a file. The regular expression is also used in awk. Actually, awk is a programming language the can generate a program to process a data file. It is very powerful but its syntax is not easy to grasp so we will not discuss this topic in detail. 2.2.3 Other file related commands 2.2.3.1 Word Count The wc (Word Count) command is used to count the number of characters, words, and lines in a file or from stdin and output to stdout. To find how many words are in file1, type: wc -w file1 To find out how many lines are in file1, type: wc -l file1 2.2.3.2 Links for simplifying file navigation The ln (LiNk) command is used to create a link to an existing file or directory. A link is a reference to a file in another directory or even in another file system. There are two types of links: hard link and soft (symbolic) link. Soft links are widely used. There may be many soft links that point to the same file. A soft link is a special file that has a pointer to a target file. The link is useless if the target file is removed. You can think of a soft link as a shortcut. ln -s <targetfile> <linkname> Here is an example of creating a soft link mylink.
[root@localhost susan]# ln -s /home/john/mytest.sh mylink [root@localhost susan]# ls -l total 0 lrwxrwxrwx 1 root root 20 2008-05-26 17:25 mylink -> /home/john/mytest.sh

20 Because data only exists in one location, when links are used disk space is saved. If you did not use a link you might have to copy the entire file contents to several locations. This is because some applications require that a file location be local. A soft link can be used to satisfy this location requirement. That is, if data or software is located somewhere else but must be accessed locally, you set up a local soft link to the target to allow access. 2.2.3.3 Backing up files Fedora comes with a number of utilities to backup files, and additional free and commercial software is available to help with this essential task if the supplied utilities do not meet your needs. In this section we will only mention two of the backup utilities that come with Linux: tar and dump. Later chapters will discuss GNOME File Roller (A graphical tool) and Amanda (A tool for backing up networks). The tar command is frequently used to move files from one Linux system to another, such as when software is distributed. While tar stands for tape archiver, there is no need to actually use magnetic tape since you can create an archive on disk drives, including floppies and USB drives. Some backup utilities require root privilege to run, but tar can be run by any user has long as they have appropriate permissions to read the input files and to write to the output device. Input can include files, directories, and entire directory trees. To create an archive of all the files in the current directory, type: tar cvf myArch.tar . The c instructs tar to create an archive. An x is used to extract files from an archive. The v stands verbose mode which displays the progress of the tar command on the screen as it adds files to or extracts files from an archive. The f indicates to place the archive in a file. The myArch.tar is the name of the archive file. It will be created in the same directory as we are currently working. At the end is the source directory or file. The period or dot indicates to backup the current working directory. To display the contents of the archive you just created, type: tar tf myArch.tar The t in the command indicates that the table of contents should be displayed. To restore the entire contents of an archive to a directory, one would type: tar xvf myArch.tar . Below is a demonstration of using tar to back up a directory and restore the contents of the directory to a subdirectory.
[susan@localhost dir1]$ ls -l total 28 -rw-rw-r-- 1 susan susan 10240 2008-05-29 23:01 file1 -rw-rw-r-- 1 susan susan 39 2008-05-29 13:56 merged

21
-rw-rw-r-- 1 susan susan 15 2008-05-29 13:28 myNewFile -rw-rw-r-- 1 susan susan 24 2008-05-29 13:45 newFile [susan@localhost dir1]$ tar -cvf myArch.tar . ./ ./newFile ./merged ./myNewFile ./file1 tar: ./myArch.tar: file is the archive; not dumped [susan@localhost dir1]$ tar -tf myArch.tar ./ ./newFile ./merged ./myNewFile ./file1 [susan@localhost dir1]$ mkdir myRestore susan@localhost dir1]$ cp myArch.tar ./myRestore/my.tar [susan@localhost dir1]$ cd myRestore [susan@localhost myRestore]$ ls -l total 20 -rw-rw-r-- 1 susan susan 20480 2008-05-29 23:35 my.tar [susan@localhost myRestore]$ tar -xvf my.tar . ./ ./newFile ./merged ./myNewFile ./file1 [susan@localhost myRestore]$ ls -l total 48 -rw-rw-r-- 1 susan susan 10240 2008-05-29 23:01 file1 -rw-rw-r-- 1 susan susan 39 2008-05-29 13:56 merged -rw-rw-r-- 1 susan susan 15 2008-05-29 13:28 myNewFile -rw-rw-r-- 1 susan susan 20480 2008-05-29 23:35 my.tar -rw-rw-r-- 1 susan susan 24 2008-05-29 13:45 newFile

The dump utility was a very popular approach to backing up UNIX systems. On Linux it is more common to use other alternatives to backing up. Linus Torvalds recommended against its use in an April 27, 2001 e-mail to Neil Conway:
Note that dump simply won't work reliably at all even in 2.4.x: the buffer cache and the page cache (where all the actual data is) are not coherent. This is only going to get even worse in 2.5.x, when the directories are moved into the page cache as well. So anybody who depends on "dump" getting backups right is already playing Russian roulette with their backups. It's not at all guaranteed to get the right results - you may end up having stale data in the buffer cache that ends up being "backed up". Dump was a stupid program in the first place. Leave it behind.

22 Even after, Linus Torvalds comments above, some folks still continue to use the dump utility on Linux. The approach of the dump utility is different than with tar. With dump one normally backs up an entire device or partition rather than particular files or directories. The dump utility creates a dump archive that can be placed on tape or disk. A sample command follows (you do not need to execute this command on your system): /sbin/dump -0u -f /dev/st0 /dev/sda1 The 0 indicates a level zero backup, which is a full backup. The u indicates to record the backup in the /etc/dumpdates file. The -f indicates the file for output, in this case the file is a tape drive /dev/st0. The /dev/sda1 is the disk partition to be backed up. To restore data from a dump archive, use the restore command. The command to restore from the dump archive created above is (you do not need to execute this command on your system): /sbin/restore -r -f /dev/st0 The -r indicates that a filesystem should be restored. 2.2.4 Multiple commands in same command line Almost all command lines are executed standalone without association with other commands. Actually, there are many Linux commands that are filter type commands that take input from stdin and send output to stdout. If a command has a stdout, that output can be used as input to another command which takes stdin. A pipe connector is used to connect the output of the one command to the input of another command. The symbol for a pipe is the vertical bar | For example, try typing: who | wc -l Think about it What it make any difference, if you typed: who | wc instead of: who | wc -l

This will display the number of users currently logged on. The next pipe substitutes all Unix by in the file merged and saves it in the file called outfile. The original file merged is unchanged. cat merged | sed s/Unix/Linux/ > outfile You can place multiple commands on a single line by separating them with a semicolon (;) as shown below.

23

<cmd1> ; <cmd2> ; <cmdn> Unlike with the pipe command, there is no connection between two consecutive commands separated by a ; except the commands are executed in that sequence. 2.3 Linux File Systems and Storage 2.3.1 File Systems In the installation section of Chapter 1, you briefly learned about the Linux file system and partitions. The advantage of partitioning is that it protects other partitions when corruption occurs. Each partition has its own file system. You can divide one large hard disk drive into multiple partitions so that you can have many partitions on one disk drive. You can also install multiple hard disk drives. Each device(CDROM, DVD, USB, etc.) is also a separate partition. In order to make any partition accessible each partition must have a file system and must be attached at a mount point by the mount command. A mount point specifies the place of a partition in a file system. The root partition is mounted in the / mount point. All the other partitions are connected via the root partition. Once the partition is un-mounted from the file system by the umount command it will no longer be available. There are two types of partitions: data partition and swap partition. The swap partition is used as an extension to the memory space and is accessible by system itself. The standard installation in Chapter 1 created three partitions: /(root), /boot,. and swap. This is a minimal default partition layout. The boot partition has all the necessary start up files. In case other partitions are damaged, this arrangement allows a computer system to still boot and startup. In custom installation, you can make many more partitions such as the home partition (mounted at /home) for user personal programs and data, the usr partition (mounted at /usr) for user application programs, the var partition( mounted at /var) for temporary data like mail queue and print queue, and the opt partition(mounted at /opt) for extra third party software. During system startup, all partitions are mounted automatically as specified in the file system configuration file /etc/fstab. The partitions and their mount points can be viewed by df and other commands discussed in the next section. Lets take a look at the subdirectories in the root (/) directory in the file system. /bin /boot /dev /etc Common shared programs The startup files and the kernel, References to all the peripheral hardware devices Important system configuration files for system administrator to manage. For example: /etc/fstab, etc/passwd /home Home directories of the common users. /initrd Information for booting. Do not remove! (on some distributions) /lib Library files /lost+found Files restored after a system crash. /misc For miscellaneous purposes.

24 /mnt /net /opt /proc /root /sbin /tmp /usr /var /media Mount point for external file systems Standard mount point for entire remote file systems Extra and third party software. System resources The root administrator home directory which is different from /, the /root is the home directory of the root user. System executables and startup executables Temporary space for use by the system and emptied when system restarts User related application programs, libraries, documentation For all user created variable files and temporary files, log files, the mail queue, the print spooler area, downloaded files Mount point for media devices like usb

Among the list of subdirectories some of them are partitions and some others are simply subdirectory of / directory. The swap partition is not visible. 2.3.2 File System Commands 2.3.2.1 mount, df, du Commands The mount command is used to mount a file system(partition) to a mount point and the umount command is to un-mount a partition from the mount point. The mount command without arguments just shows file system mounting status. The syntax of mount is mount <device> <mount point> The syntax of umount is umount <mount point> For example, you can mount a floppy drive and CDROM drive as follows: mount /dev/fd0 /mnt/floppy where mount point "/mnt/floppy" is set up in advance. mount /dev/cdrom /mnt/cdrom You can un-mount these drives as follows: umount /mnt/floppy or umount /dev/fd0 umount /mnt/cdrom or umount /dev/cdrom

25
[root@localhost ~]# mount /dev/sda5 on / type ext3 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) /dev/sda2 on /boot type ext3 (rw) tmpfs on /dev/shm type tmpfs (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) fusectl on /sys/fs/fuse/connections type fusectl (rw) gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev) /dev/sda1 on /media/disk type fuseblk (rw,nosuid,nodev,allow_other,blksize=4096) /dev/sdb1 on /media/MINI-USB2BU type vfat (rw)

After the un-mount of the USB device partition from the mount point, the USB drive is no longer accessible.
[root@localhost ~]# umount /media/MINI-USB2BU [root@localhost ~]# ls /media/MINI-USB2BU [root@localhost ~]# mount . . . /dev/sda1 on /media/disk type fuseblk (rw,nosuid,nodev,allow_other,blksize=4096) [root@localhost ~]# cd /media/MINI-USB2BU [root@localhost MINI-USB2BU]# ls -l total 0 [root@localhost MINI-USB2BU]# mount /dev/sdb1 /media/MINI-USB2UB mount: mount point /media/MINI-USB2UB does not exist

Now, you can make a mount point for the USB drive and mount it again. The USB drive is available for access. Fedora can detect a newly inserted USB drive and take care of the mounting process automatically. This eliminated the need for a user to manually mount the drive in many situations.
bash: cd: /media/MINI-USB2UB: No such file or directory [root@localhost /]# cd media [root@localhost media]# mkdir /media/MINI-USB2UB [root@localhost media]# mount /dev/sdb1 /media/MINI-USB2UB [root@localhost media]# cd /media/MINI-USB2UB [root@localhost /]#

df (Disk free) The df command reports on the disk space left on the file system. Here is a report from running the df command where the h option was specified for human being readable format.

26
[root@localhost MINI-USB2UB]# df -h Filesystem /dev/sda5 /dev/sda2 tmpfs gvfs-fuse-daemon /dev/sda1 /dev/sdb1 /media/MINI-USB2UB Size Used 18G 99M 501M 18G 18G 125M Avail 3.7G 13G 12M 83M 48K 501M 3.7G 13G 2.0G 16G 101M 25M Use% Mounted on 23% 13% 1% 23% 11% 81% / /boot /dev/shm /root/.gvfs /media/disk

du (Disk Usage) The du command outputs the disk space used by each subdirectory. It is useful when you want to find out which directory has the most files. To see the disk space usage on the root / directory, type:
[root@localhost MINI-USB2UB]# cd / [root@localhost /]# du -sh * 6.0M 6.1M 124K 124M 11M 87M 20K 2.0G 4.0K 0 1.4M 21M 0 4.0K 0 140K 3.2G 74M bin boot dev etc home lib lost+found media mnt proc root sbin selinux srv sys tmp usr var

The -s flag will display only a summary (total size) and the * is a wildcard indicating all files and directories should be included in the listing. The free command displays information about free and used memory on the system including the swap partition usage.
[root@localhost /]# free total used cached

free

shared

buffers

27
Mem: 1025288 156956 -/+ buffers/cache: Swap: 2096472 684156 413084 0 341132 612204 2096472 0 114116

mkfs (Make File System) command builds a Linux file system on devices such as a hard disk partition, CDROM, USB, etc. mkfs [option] <filesys> where <filesys> can be either the device name (e.g. /dev/hda1, /dev/sdb2) or the mount point (e.g. /, /usr, /home). This command should be run with a root privilege. To avoid possible data loss--Do not actually perform the mkfs and fsck commands below! The following command builds a file system for the hard disk drive based on the device name (hda1). mkfs -t ext3 /dev/hda1 mkfs -t ext3 /dev/CDROM This example creates an ext3 file system on CDROM drive. fsck(File System ChecK) command is used to check and repair the file systems. fsck [option] <filesys> where <filesys> can be a device name (e.g. /dev/hdc1, /dev/sdb2), a mount point (e.g. /, /usr, /home), or UUID specifier. By default, fsck checks filesystems described in in /etc/fstab. The fsck is called at boot time when the system detects that a file system is in an inconsistent state. You can run it manually for either interactively repairing damaged file systems or automatically deciding how to fix specific problems, or just for reviewing the problems. 2.3.2.2. User Disk Quota In order to set up user disk quota you must configure and enable the file system for disk quota control. The following example show how to set up disk quota on /dev/sda3 mounted at /home so that each user will have its allocated disk quota. Step 1) Edit /dev/fstab file system configuration file to enable the file system user quota support by adding userquota control to the /dev/hda3(/home) partition. ...

28 /dev/hda3 ... /home ext3 defaults, userguota 0 0

Make sure the quota record files ready. The quotacheck cum check the file system usage in /home directory and create(-c) the user(-u) aquota.user quota record file without re-mount(-m).
[root@host1 john]# /sbin/quotacheck -cum /home -rw------1 root root 7168 2008-05-30 09:03 aquota.user

Next, you remount the /home file system by the mount command with the remount option to mount it which is mounted already to make the quota control flag effective. mount o remount /home You can check the /etc/mtab file to find out the updated quota control status. [root@host1 john]# cat /etc/mtab /dev/sda6 / ext3 rw 0 0 proc /proc proc rw 0 0 sysfs /sys sysfs rw 0 0 devpts /dev/pts devpts rw,gid=5,mode=620 0 0 /dev/sda3 /home ext3 rw,usrquota 0 0 /dev/sda2 /usr ext3 rw 0 0 /dev/sda1 /boot ext3 rw 0 0 . . . Step 2) Use Quotacheck command to update the quota status [root@host1 john]# /sbin/quotacheck vguma quotacheck: Scanning /dev/sda3 [/home] done quotacheck: Old group file not found. Usage will not be substracted. quotacheck: Checked 72 directories and 52 files Step 3) Set the quota for user John by edquota command Turn on the file system quota s [root@host1 john]# quotaon av Check user Johns quota, it is not set yet [root@host1 john]# quota -u john Disk quotas for user john (uid 500): none [root@host1 john]# PATH=$PATH:/sbin:/usr/sbin [root@host1 john]# export PATH [root@host1 john]# edquota -u john

29
Disk quotas for user john (uid 500): Filesystem blocks /dev/sda3 700 soft 200000 hard 250000 inodes 114 soft 0 hard 0

You can edit this file to set soft limit(max blocks before warning when the grace period begins), hard limit(ultimate limit, and grace period. Step 4) List: Check user johns quota again. [root@host1 john]# quota -u john Disk quotas for user john (uid 500): Filesystem blocks quota limit limit /dev/sda3 0 [root@host1 john]# 720* 200000 300000 grace 7days files 102 quota 0

2.4 Process Basics As was mentioned earlier, a Linux computer system consists largely of files and processes. A system administrator must know how to manage processes. A process is an executing program identified by a unique PID (Process IDentifier). The Linux system allows multiple processes to run concurrently sharing the CPU and other computer resources. Some processes run in the foreground and others run in the background. At any given time on a computer system, some processes may be temporarily suspended rather than running. To list processes on a Linux system and find their PID numbers, use the ps (Process Status) command. The top command can also be used to check the resource consumption status of processes. The top command provides an ongoing look at processor activity in real time. It displays a listing of the most CPU-intensive tasks on the system, and can provide an interactive interface for manipulating processes. It can sort the tasks by CPU usage, memory usage and runtime. It pops up every 5 seconds. You can set the interval longer by specifying a command option at the command line. If you run top in interactive mode (the default), you can make adjustments while top is running by typing keys on the keyboard. Some tasks may take a long time to complete, such as searching the entire file system to locate files with certain criteria using the find command. These tasks will hold up the terminal. Users have to sit and wait for the command to complete. Running a job in the background will bring the system prompt back immediately, and the user can resume working instead of waiting. To run a command in the background, include an & at the end of the command line. For example, to force the find command to be executed in the background, type:
find / -name linux &

[1] 4379

30

The user is notified of a job number (numbered from 1) enclosed in square brackets, together with a PID when the background command is issued. The user will be notified when a background process is finished. Backgrounding is useful for jobs that will take a long time to complete. You can also background a current foreground process. You first suspend a process running in the foreground by typing ^Z, and then put it in the background by typing: bg Of course, a background job cannot interact with the user any-more. To see all running, backgrounded, and suspended jobs and their job numbers, type: jobs To bring a suspended process back to foreground, type: fg <job id> The command fg 1 will bring the find command suspended job to foreground. If you omit the job id number, the fg command defaults the last suspended process. It is sometimes necessary to kill a process such as an infinite loop, non-responding, or a long time running process. In a GUI session there may be many foreground processes running at once. The GUI session might have a foreground process editing a file, another browsing the web, and still another for a game of blackjack. In a terminal session with a command line interface, there can only be one foreground process. Now try out the process commands. For demonstration purposes we will use the sleep command. The sleep command does nothing but waits a specified number of seconds. Start two background processes that sleep for 60 seconds. Then start a foreground process for 60 seconds and suspended it with a ^Z. Then issue the bg command to resume the suspended process and bring it to the background. The jobs command should now show all three jobs running. Bring the second job to the foreground then suspend it with ^Z. Check on progress using the jobs command. Two jobs should be running and one is suspended (labeled stopped). Wait a few more seconds and reissue the jobs command. After the 60 seconds have elapsed the running jobs should finish.
[susan@localhost [1] 17138 [susan@localhost [2] 17139 [susan@localhost ^Z [3]+ Stopped [susan@localhost [3]+ sleep 60 & [susan@localhost [1] Running ~]$ sleep 60 & ~]$ sleep 60 & ~]$ sleep 60 ~]$ bg ~]$ jobs sleep 60 & sleep 60

31
[2]- Running [3]+ Running [susan@localhost sleep 60 ^Z [2]+ Stopped [susan@localhost [1] Running [2]+ Stopped [3]- Running [susan@localhost [2]+ Stopped [1] Done [3]- Done [susan@localhost [2]+ Stopped [susan@localhost sleep 60 & sleep 60 & ~]$ fg 2 sleep 60 ~]$ jobs sleep 60 & sleep 60 sleep 60 & sleep 60 sleep 60 sleep 60 sleep 60

~]$ jobs

~]$ jobs ~]$ kill %2

The suspended job will not finish. Thus, it remain on the system until it is resumed or killed. We can kill a suspended job typing kill %< job number>. To kill a job running in the foreground, type ^C (control c). To kill a suspended or background process when you do not know the job id number, find its process PID and then kill it. To find a PID by its name use, pidof <name of application or process> or search the list reported by ps aux command and then type: kill <process id> If a process refuses to be killed, use kill -9 <process id> . The -9 specify that the kill is unconditional and no cleanup should be performed. A regular user can only kill their own processes, but root can kill processes of other users. Notice that many process management commands require root privileges. Action Moves cursor to beginning of line Terminate current job Indicates end of file, can be used to indicate you have finished entering data, at the bash shell prompt it is the same as typing exit ^e Moves cursor to end of command line ^k Clears from cursor position to end of line ^q Resumes scrolling ^s Stops scrolling in a terminal window ^u Clears from cursor position to beginning of line ^w Clears word to the left ^z Suspend current job Table 9.4 Some helpful keyboard shortcuts for command line interface Control Key Shortcuts ^a ^c ^d

2.5Summary

32

Using the command line is a powerful method to manage Linux servers. A large number of commands are available to monitor and configure servers. This chapter covered only a few of the commands that can be used to manage file, directories, file systems, and processes on a server. Information on computers is stored in files. This chapter covered how to create the files and directories, and remove them when no longer needed. Systems administrators need to ensure that authorized people can find information quickly and without difficulties, but need to ensure that unauthorized users cannot find information. If a user already knows where files are located, the system administrator must prevent them from accessing or modifying these files. To make files findable on disk, the files are organized into directories and subdirectories. The find command can be used to locate files that meet certain criteria if you do not remember what directory a file is located in. The chmod command was used to set permissions to control access to files and directories. To protect data against corruption or loss it is important to backup it up using tar, GNU File Roller, Amanda, or some other backup utility. Files and directories are stored on file systems that are created on disk partitions or other devices. Linux provides commands to mount and unmount file systems. The mkfs command is used to create a file system on a disk partition and fsck check is used to check and repair existing file systems. To determine file system utilization, the du and df commands are used. Process management in Linux is important to ensure a high level of performance. To track processes in Linux, the operating system assigns each process a PID. To check the status of a process in Linux, use the ps command. The top command can be used to identify which processes on the system are consuming the most CPU time. Once a process is identified with a problem it can be suspended, sent to the background, or filled. In next few chapters we will expand your knowledge of Linux commands. In the third chapter we will learn how to customize the Linux environment and automate routine system administration tasks. In the fourth chapter we will look at running system utilities from a graphical environment instead of a command line environment.

2.6 Self-Review Questions


1. Which Linux command can be used to create a new file?
a. b. c. d. cat finger gedit touch

2.

The top level directory in a Linux file system is called:


a. b. c. d. root and represented be a / home and represented by % dev and represented by # fedora and represented by &

33 3. GNOME, as a component of GNU project, is primarily a command line environment


a. True b. False

4.

The whoami command is used to determine


a. b. c. d. the brand and model of a PC the operating system version number currently installed the username of whoever is logged in the name of the CPU chip currently being used

5.

If the command chmod 700 myFile.dat is issued, the myFile.dat can be accessed:
a. b. c. d. by the owner and users with sufficient privilege by any member of the same group as the owner by any user who can login only by the owner

6.

The command to switch to a users home directory, from any other directory is.
a. b. c. d. cd home cd /usr cd cd ..

7.

The rmdir deletes a directory and all the files contained in the dir. a. True b. False

8.

The command used to search for files that meet a specified criteria is: a. b. c. d. find grep search hunt

9.

The command used to unmount a device is. a. b. c. d. remove umount unmount none of the above

10. The command to stop a running process is.


a. remove process

34
b. delete process c. stop d. kill

11. The command to set the user disk quota limit.


a. b. c. d. quotacheck edquota quota quotaon

12. Which file can tell the quota status.


a. b. c. d. fstab mtab both none

13. The command to move dir1 of home directory from anywhere.


a. b. c. d. cd /dir1 cd dir1 cd ~/dir1 cd

14. The command to list hidden files in current working directory


a. b. c. d. ls la . ls -l ls l . ls -la

15. The command to backup all files in current directory.


a. b. c. d. tar cvf myarc.tar . tar tf myarc.tar . tar xvf myarc.tar dump cvf mytar.tar .

16. The command to restore all files in the myarc.tar to the current working directory.
a. b. c. d. tar cvf myarc.tar . tar tf myarc.tar . tar xvf myarc.tar . dump cvf mytar.tar

17. The command to find all files which have not been modified for more than one weeks.

35
a. b. c. d. find /type f mtime +7 find /type f mtime 7 find /type f mtime -7 none

18. To copy the directory tree of dir1 to dir2.


a. . cp r dir1 dir2 b. cp dir1 dir2 c. mv dir1 dir2 d. none

19.. The permission of rwxrwxrw- can be represented by numeric symble of


a. b. c. d. 776 660 . 554 665

20. To dump a partition to a tape.


a. /sbin/dump -0u -f /dev/st0 /dev/sda1. b. /sbin/restore -r -f /dev/st0 c. /sbin/dump -0u -f /dev/sda1 /dev/st0 d. dump cvf /dev/st0 /dev/sda1

Keys to the self-Review Questions 1. D 2. A 3. B 13. C 14. A, D 4. C 5. A 6. C 7. B 8. A 9. B 10. D 11. B 12. C 15. A 16. C 17. A 18. A 19. A 20. A

36

2.7 Exercises
1. Login into Fedora using an unprivileged account. Start a terminal session as described in the chapter. At the shell prompt type: script ch2ex1.log The script command is used to record a terminal session to the specified file (if no filename is specified the file will be called typescript. Now type the following commands: who whoami whereis ls whereis tar whereis restore weher is apachectl where is mysql where is php cd /home/bill <or another user if logged in as bill> su root <supply password when prompted> cd /home/bill ls whoami exit <takes you out of root> ls exit <ends scroll session> cal Now examine the ch2ex1.log file in for ways. First be editing it with gedit, then by displaying to the screen using cat, then display on screen using the less command, finally display it to the screen using more. Which method worked better for reviewing the logfile? If your instructor desires proof that you completed this exercise, print or email the logfile to the instructor. Your instructor may request that you create these logfiles for other assignments as well. 2. In a terminal session switch the user to susan using the su command. Then create three subdirectories: dir1, dir2, and dir3. Set the permission on dir1 to allow the owner read, write, execute access and no access for group or others. Set dir2 to allow full access for the owner; read, execute access for group; and no access for others. Set dir3 to allow full access for the owner; read, execute access for group; and no access for others. Change the group ownership of all files to linux Create a file in each dir using cat. Switch to the bill user (The bill account should have been created in chapter 1 and assigned membership in the linux group) and then attempt to list the contents of each of the directories. Try to edit each of the files you created. Then attempt to create a file in each of the directories using the touch command. Use ls -la to determine who owns the created files and whether susan will be able to access them. 3. Create a link in bills home directory that references the file he created in /home/susan/dir2

37 4. In a terminal session, switch user to susan using the su command and navigate to her home directory using cd. Use the du command to obtain a disk utilization report. Disk utilization is reported in 512 byte blocks. Display disk utilization in bytes and then in human readable format. Use the man command to find out how to do this. 5. Create a new file in bills home directory using gedit. Type the following lines into the editor and be sure to press enter after each line:
The amazing combination of Linux, Apache, MySQL, and PHP (LAMP) is a powerful platform for Web development. Apache is an amazing Web Server. MySQL is an open-source database. PHP can be used for scripting.

After you have entered these lines, save the file and return to your terminal session. Use head to display the first three line of the file. Use tail to display the last three lines of the file. Use sed to display lines 2 through 4 of the file. Use sed to display all the lines containing the word Apache. Use sed to replace all occurrences of the word amazing in the file with the word astounding. Use grep to display all the lines containing the word astounding. 6. Use the find command to locate all files owned by the mysql user in the /var directory and its subdirectories. 7. If your machine has a floppy drive, practice mounting and unmounting a floppy drive. If it does not have a floppy drive, use a USB drive. 8. Use the whereis command to determine the location of fsck. Issue the fsck command to repair the disk partition containing your home directory. Say no when asked if you want to continue. 9. Start several things running on your computer: a gedit session, a mozilla session, a sodoku (applications>>Games>>Soduko) session, three terminal sessions Use su so the terminal sessions are from different users. Use the following commands to examine processes running on the system: ps ps a u ps A u |more <space bar advances screen, letter q for quit> 10. Use top to list processes on the system. Review the process list for the process running the top command. Kill the top process from another terminal session (session must either be from the same user or have root privilege). Then reissue the top command and. This time kill the top process by typing a k and the process id number in the session running top.

Das könnte Ihnen auch gefallen