Chapter 8 Linux的操作命令

雖然Linux提供類似Microsoft Windows的圖形介面,然而文字模式的Linux才是其真正技術精神所在。

指令(Command)  選項s(Options)  參數s(arguments)



在 Linux 主機上,已經內建各個指令的說明內容了,而且是很詳細的線上說明,你可以使用:
語法:man <指令名稱>
[root@tsai  root]#  man  command <=== command 為你欲查詢的指令名稱
[root@tsai  root]#  info  command 

在上表中,針對 command 這個指令你可以使用 man 或者是 info 來查詢他的功能與說明,

Wildcards and Filename Arguments: *, ?, [ ]

 Table 5-1 lists the shell's shell symbols.

Table 5-1: Shell Symbols

Common Shell Symbols



Match on a class of possible characters in filenames.在括號內的字元範圍內都會符合.

$ ls
doc1 doc2 doc3 docA docB docD document
$ ls doc[1A]
doc1 docA


Quote the following character. Used to quote special characters.將字元的特殊意義取消.
In the next example, the user needs to reference a file that ends with the ? character, answers?.

$ ls answers\?


Pipe the standard output of one command as input for another command.命令輸出入的重導向

You may want to send the standard output of a command to another command, not to a destination file.

Suppose you want to send a list of your filenames to the printer to be printed.

Method 1: You need two commands to do this: the ls command to generate a list of filenames and the lpr command to send the list to the printer.

Method 2: You can connect the ls command and the lpr command with a pipe. The list of filenames output by the ls command is piped into the lpr command.

$ ls | lpr


Execute a command in the background.

Wildcard Symbols



Match on any set of characters in filenames.符合任何字元, 包括一個以上.(包括一個)


Match on any single character in filenames.符合一個字元或零個.

Redirection Symbols



Redirect the standard output to a file or device, creating the file if it does not exist and overwriting the file if it does exist.


Redirect the standard input from a file or device to a program.


Redirect the standard output to a file or device, appending the output to the end of the file.


Common Commands



cat  file  [>|>]  [destination file]

The cat command displays the contents of a file to stdout. It is often helpful to examine the contents of a file by using the cat command. The argument that you pass to cat is the file that you want to view. To view the contents of a file called myfile, run

cat myfile

cat can also merge existing multiple files into one:

cat name1 name2 name3 > allnames

This example combines the files name1, name2, and name3 to produce the final file allnames. The order of the merge is established by the order in which the files are entered at the command line.

Using cat, you can append a file to another existing file. For instance, if you had forgotten to add names4 in the previous command, you could still produce the same results by executing

cat names4 > allnames

to append names4 to allnames.



chmod  [-R] permission-mode  file or directory

The chmod command is used to change the permission mode of a file or directory. The permission mode is specified as a three- or four-digit octal number. For example,

chmod 755

changes the permission of script to 755 (rwxr-xr-x), which allows the file owner to read, write, and execute, and allows only read and execute privileges for everyone else. As another example,

chmod -R 744 public_html

changes the permissions of the public_html directory and all of its contents (files and subdirectories) to 744 (rwxr--r--), which is a typical permission setting for personal Web directories that are accessed using http://server/~username URLs under Apache server. The –R option tells chmod to recursively change permissions for all the files and directories under the named directory.



chown [ -fhR ] Owner [ :Group ] { File . . . | Directory. . . }

The chown command changes the owner of the file specified by the File parameter to the user specified by the Owner parameter. The value of the name found in the Owner parameter can be a user ID or a login /etc/passwd file. Optionally, a group can also be specified. The value of the Group parameter can be a group ID or a group name found in the /etc/group file.

Only the root user can change the owner of a file. You can change the group of a file only if you are a root user or own the file. If you own the file but are not a root user, you can change the group only to a group of which you are a member. Table A-1 lists and describes the chown options.

Table A-1 chown Options




Suppresses all error messages except usage messages.


Changes the ownership of an encountered symbolic link but not that of the file or directory pointed to by the symbolic link.


Descends directories recursively, changing the ownership for each file. When a symbolic link is encountered and the link points to a directory, the ownership of that directory is changed but the directory is not further traversed.

The following example changes the existing owner of the file to another user:

chown bert hisfile.txt



cp  [-R] source file or directory  file or directory

Use the cp command to make an exact copy of a file. The cp command requires at least two arguments. The first argument is the file you want to copy, and the second argument is the location or filename of the file. If the second argument is an existing directory, cp will copy the source file into the directory with the target file’s name.

cp main.c main.c.bak

The preceding example will copy the existing file main.c and create a new file called main.c.bak in the same directory. These two documents will be identical, bit per bit.



find [path] [-type fdl] [-name pattern] [-atime [+-]number of days] [-exec command {} \;] [-empty]

The find command finds files and directories. For example,

find  .  -type d

The find command returns all the subdirectory names under the current directory. The –type option typically is set to d (for directory), f (for file), or l (for links).

The following command finds all the text files (ending with the .txt extension) in the current directory, including all of their subdirectories:

find  .  -type f -name "*.txt"

The following command searches all the text files (ending with the .txt extension) in the current directory, including all of their subdirectories, for the keyword “magic” and returns their names (because –l is used with grep):

find  .  -type f -name "*.txt" -exec grep -l ‘magic’ {} \;

The following command finds all the GIF files that have been accessed in the past 24 hours and displays their details using the ls –l command:

find . -name "*.gif" -atime -1 -exec ls -l {} \;

The following command displays all the empty files in the current directory hierarchy:



grep [-viw] pattern file(s)

The grep command allows you to search for one or more files for particular character patterns. Every line of each file that contains the pattern is displayed at the terminal. The grep command is useful when you have lots of files and want to find out which ones contain particular words or phrases.

Using the –v option, you can display the inverse of a pattern. Perhaps you want to select the lines in data.txt that do not contain the word “the”:

grep –vw ‘the’ data.txt

If the –w option was not specified, then any word containing “the” would match, such as toge[the]r. The –w option specifies that the pattern must be a whole word. The –i option ignores the difference between upper- and lowercase letters when searching for the pattern.



ln [-s] sourcefile target

The ln command creates two types of links: hard and soft. A link can best be thought of as two names for the same file. After a link is created, it is indistinguishable from the original file. A file that has hard links to it will not be removed from the hard disk until all links are removed. Hard links are created without the –s option:

ln ./www ./public_html

However, a hard link does have limitations: it can link neither to another directory nor to a file on another file system. But, by using the –s option, you can create a soft link, which eliminates these restrictions:

ln –s /dev/fs02/jack/www /dev/fs01/foo/public_html

The preceding creates a soft link between the directory www on file system 2 and a newly created file public_html on file system 1.



locate keyword

The locate command finds the path of a particular file or command. locate will find an exact or substring match. For example:

locate foo

The output that locate produces will contain the keyword “foo” in the absolute path or will not have any output at all.



ls [-1aRl] file or directory

The ls command allows you to list files (and subdirectories) in a directory. It is one of the most commonly used programs. When used with the –1 option, the ls command displays only the file and directory names in the current directory. When the –l option is used, a long listing containing file/directory permission information, size, modification date, and so on is displayed. Using the –a option enables you to view all files and directories (including the ones that have a leading period in their names) within the current directory. The –R option allows the command to recursively display contents of the subdirectories (if any).



mkdir directory . . .

To make a directory, use the mkdir command. Only two restrictions apply when choosing a directory name: filenames can be up to 255 characters long, and directory names can contain any character except /.

The following example creates three directories, all in the current directory, provided write permission is granted in the current directory:

mkdir dir1 dir2 dir3



mv [-if]sourcefile targetfile

Use the mv command to move or rename directories and files. The command will perform a move or a rename, depending on whether or not the targetfile is an existing directory. To illustrate, suppose that you want to rename an existing directory currently called foo to the new name of foobar:

mv foo foobar

Because foobar does not already exist as a directory, foo will be renamed to foobar. If the following command is issued

mv doc.txt foobar

and foobar is an existing directory, a move will be performed. The file doc.txt will now reside in the directory foobar.

The –f option removes existing destination files and never prompts the user. The –i option prompts the user whether to overwrite each destination file that already exists. If the response does not begin with “y” or “Y,” the file is skipped.





The pwd command prints the current working directory. The directories displayed will be the absolute path. None of the directories displayed will be hard or soft symbolic links.



rm [-rif] directory/file

To remove a file or directory, use the rm command. Here are some examples:

rm doc.txt
rm ~/doc.txt
rm /tmp/foobar.txt

To remove multiple files with rm, you can use wildcards or type each file individually. For example,

rm doc1.txt doc2.txt doc3.txt

is equivalent to

rm doc[1-3].txt

rm is a powerful command that can cause chaos if used incorrectly. For instance, suppose that you have stored on your computer a thesis that you’ve worked hard on for the last six months. You decide to rm all of your docs, mistakenly thinking that you are in another directory from the one in which your thesis is stored. After finding out a backup file does not exist (and you are no longer in denial), you wonder whether you could have prevented this mistake somehow.

The rm command’s –i option enables rm to be interactive. It tells rm to ask your permission before removing each file. For example, if you enter

rm –i *.doc
rm: remove thesis.doc (yes/no)? n

the –i option gives you a parachute, asking you whether to pull the cord (answer no, the default) or suffer the consequences (answer yes). The –f option is completely the opposite. The –f (force) option tells rm to remove all the files you specify regardless of the file permissions. Use the –f option only when you are 100-percent sure you are removing the correct file(s).

To remove a directory and all of the files and directories within it, use the –r option. rm –r will remove an entire subtree:

rm –r documents

If you are not sure of what you are doing, combine the –r option with the –i option:

rm –ri documents

The preceding example asks for your permission before removing each file and directory.



touch file or directory

The touch command updates the timestamp of a file or directory. If the named file does not exist, it will be created as an empty file.


See the section on setting default file permissions for users.


Vi is an editor, which is important to learn and understand.




要使用 gzip 來壓縮一個檔案, 請在 shell 提示符號下輸入如下的指令:

gzip filename

這個檔案將會被壓縮並儲存為 filename.gz


gunzip filename.gz

filename.gz 檔案會被刪除, 並以 filename 檔案取代之。

您可以使用 gzip 來同時壓縮多重的檔案與目錄,只要將它們以空白分隔的方式列出即可:

gzip -r filename.gz file1 file2 file3 /usr/work 

以上的指令壓縮 file1, file2, file3 以及在 /usr/work 目錄中的內容(假設有這個目錄存在),並將它們儲存成 filename.gz



 tar 指令將指定的目錄與檔案打包成一個檔,但本身並沒有提供壓縮的功能。可是,tar 指令有一個特色,她能以選項的方式直接呼叫壓縮解壓縮程式,如 gzip(-z) 以及 compress(-Z)。



-A :將檔案合併(附加)到已經存在 tar 檔案中;append tar files to an archive

-c :製作 tar 檔案;create a new archive

-x :還原(展開) tar 檔案;extract files from an archive

-t :顯示 tar 檔案的內容;list the contact of an archive

-f :指定儲存的檔名或設備


-T, --files-from=F :進行轉檔的資訊由檔案取得(文字檔案)

--remove-files :打包後,刪除原檔案(小心使用)

-v :顯示指令運作過程資訊;verbosely list files processed

--exclude FILE :打包時,排除指定的檔案

-X, --exclude-from FILE :讀取指定檔案(文字檔案),作為排除打包的檔案表列依據。

-z :進行 gzip 壓縮(製作檔案時)或解壓縮(還原檔案時)


# tar cvfz kk200.tar.gz /home/html

以上是將 /home/html 目錄之下的所有資料壓縮(呼叫gzip)轉存成 kk200.tar.gz 檔案。


# tar xvfz game.tar.gz

將 game.tar.gz 檔案,呼叫gzip解壓縮,並解開 tar 的檔案到在現在的目錄中。


# tar tvfz kk201.tar.gz

顯示 kk201.tar.gz 檔案的內容,通常用這方式來查看一個檔案是否完整或者是任何錯誤訊息在其中。



所謂的 daemon 是指系統內的某個服務程式(service),通常我們叫它server,可以接受來自使用者或者是網路用戶端(client)的要求,進而提供服務,一般都在背景當中執行

為了讓使用者可以很輕易的啟動服務,會利用shell script來協助,以減少複雜參數的設定,這些shell script一般放在/etc/init.d/目錄之下,我們也可以打開這些shell script檔案, 進一步了解詳細的啟動過程。

重新啟動 daemon服務的方式, 以 syslog 服務為例:

[root@linux]#/etc/init.d/syslog restart

除了這樣的啟動方式外,也可以使用 Fedora 系統所提供的 service 這個程式來進行 daemon 的啟動,  service本身是一支 script程式, 他可以解析後面帶有的參數,然後到 /etc/init.d/ 目錄中找相對應的 script來啟動,  service的語法如下:

[root@linux]# service [service name] (start|stop|restart)
service name:需要啟動的服務名稱,需與 /etc/init.d/中的script 程式對應。

在下面的例子中,以 service 這個程式來啟動daemon服務,或者直接到 /etc/init.d/ 底下啟動,結果是一樣的.

範例:重新啟動 syslog 這個 daemon service :
service syslog restart
/etc/init.d/syslog restart

如果我們想要機器在重新開機後daemon服務能自動啟動, 可以開啟/etc/rc.d/rc.local 這個檔案, 將任何想要在開機時啟動的程序寫入這個檔案中,這個檔案是以 shell script 的語法寫成的,以文字編輯的模式就可以加入想要啟動的script