Unix 檔系統基礎

檔系統是一個分區或磁片上的檔的邏輯集合。分區是資訊的容器,如果需要的話,可以跨越整個硬碟驅動器。

你的硬碟驅動器,可以有不同的分區通常只包含一個檔系統,如一個檔系統 / 檔系統或其他包含/home檔系統。

每個分區的一個檔系統允許不同的檔系統的維護和管理邏輯。

在Unix中的一切都被認為是一個檔,包括物理設備,如DVD-ROM,USB設備,軟碟驅動器,等等。

目錄結構:

Unix 使用層次結構的檔系統的結構,很象一個倒置的樹,在該檔系統的基礎上,並從那裏擴展的所有其他目錄的根目錄(/)。

UNIX 檔系統中的檔和目錄的集合,具有以下屬性:

  • 它有一個根目錄(/),其中包含其他檔和目錄。

  • 每個檔或目錄被唯一標識,它的名字,它的目錄,以及一個唯一的識別字,通常被稱為一個inode。

  • 按照慣例,根目錄下有一個inode號為2和3的lost + found目錄中有一個inode號。 inode編號0和1不使用。指定-i選項的ls命令可以看到檔的inode編號。

  • 這是自包含的。沒有一個檔系統,以及任何其他的之間的依賴關係。

目錄有特定的目的,普遍持相同類型的資訊,輕鬆定位檔。以下是主要的Unix版本上存在的目錄:

目錄 描述
/ This is the root directory which should contain only the directories needed at the top level of the file structure.
/bin This is where the executable files are located. They are available to all user.
/dev These are device drivers.
/etc Supervisor directory commands, configuration files, disk configuration files, valid user lists, groups, ethernet, hosts, where to send critical messages.
/lib Contains shared library files and sometimes other kernel-related files.
/boot Contains files for booting the system.
/home Contains the home directory for users and other accounts.
/mnt Used to mount other temporary file systems, such as cdrom and floppy for the CD-ROM drive and floppy diskette drive, respectively
/proc Contains all processes marked as a file by process number or other information that is dynamic to the system.
/tmp Holds temporary files used between system boots
/usr Used for miscellaneous purposes, or can be used by many users. Includes administrative commands, shared files, library files, and others
/var Typically contains variable-length files such as log and print files and any other type of file that may contain a variable amount of data
/sbin Contains binary (executable) files, usually for system administration. For examplefdisk and ifconfig utlities.
/kernel Contains kernel files

流覽檔系統:

現在你瞭解基本的檔系統,你就可以開始導航到你需要的檔。以下是您將使用流覽系統的命令:

命令 描述
cat filename Displays a filename.
cd dirname Moves you to the directory identified.
cp file1 file2 Copies one file/directory to specified location.
file filename Identifies the file type (binary, text, etc).
find filename dir Finds a file/directory.
head filename Shows the beginning of a file.
less filename Browses through a file from end or beginning.
ls dirname Shows the contents of the directory specified.
mkdir dirname Creates the specified directory.
more filename Browses through a file from beginning to end.
mv file1 file2 Moves the location of or renames a file/directory.
pwd Shows the current directory the user is in.
rm filename Removes a file.
rmdir dirname Removes a directory.
tail filename Shows the end of a file.
touch filename Creates a blank file or modifies an existing file.s attributes.
whereis filename Shows the location of a file.
which filename Shows the location of a file if it is in your PATH.

您可以使用聯機幫助幫助這裏提到的每個命令的語法檢查完成。 

df 命令:

第一種方式來管理你的分區空間使用df(磁片空閒)命令。命令df-K(可用磁片)以KB為單位顯示磁片空間使用情況,如下圖所示:

$df -k
Filesystem      1K-blocks      Used   Available Use% Mounted on
/dev/vzfs        10485760   7836644     2649116  75% /
/devices                0         0           0   0% /devices
$

一些目錄,如  /devices,顯示0位元組,使用和可用列的能力,以及0%。這些特殊的檔系統(或虛擬),雖然它們駐留在磁片上/下,它們本身不佔用磁片空間。

df -k 輸出所有的Unix系統上大致相同。下麵是它通常包括:

Column 描述
Filesystem The physical file system name.
kbytes Total kilobytes of space available on the storage medium.
used Total kilobytes of space used (by files).
avail Total kilobytes available for use.
capacity Percentage of total space used by files.
Mounted on What the file system is mounted on.

你可以使用-h(人類可讀的)選項顯示的輸出格式,顯示的大小更容易理解的符號。

du 命令:

使用du(磁片使用率)命令使您能夠在一個特定的目錄指定目錄顯示磁片空間使用情況。

此命令是有幫助的,如果你要確定一個特定的目錄多大的空間。下麵的命令將顯示每個目錄所消耗的塊數。單塊可能需要512位元組或1千位元組,這取決於你的系統。

$du /etc
10     /etc/cron.d
126    /etc/default
6      /etc/dfs
...
$

-h選項使輸出更容易理解:

$du -h /etc
5k    /etc/cron.d
63k   /etc/default
3k    /etc/dfs
...
$

安裝的檔系統:

必須安裝的檔系統,以便使用該系統。要看到什麼是目前在您的系統上安裝(可以使用),使用這個命令:

$ mount
/dev/vzfs on / type reiserfs (rw,usrquota,grpquota)
proc on /proc type proc (rw,nodiratime)
devpts on /dev/pts type devpts (rw)
$

/ mnt目錄下,Unix的慣例,是位於臨時裝片(如CD-ROM驅動器,遠程網路驅動器,軟碟驅動器)。如果您需要掛載檔系統,可以使用mount命令的語法如下:

mount -t file_system_type device_to_mount directory_to_mount_to

例如,如果你想要的CD-ROM安裝到目錄 /mnt/cdrom,例如,您可以鍵入:

$ mount -t iso9660 /dev/cdrom /mnt/cdrom

假定您的CD-ROM設備名為  /dev/cdrom命令要掛載到 /mnt/cdrom。更具體的資訊或幫助資訊在命令行中鍵入mount -h ,請參閱安裝手冊頁。

安裝後,您可以使用cd 命令導航最新的檔系統掛載點。

卸載檔系統:

從您的系統中卸載(刪除)檔系統,使用umount命令識別掛載點或設備

例如,要卸載光驅,使用下麵的命令:

$ umount /dev/cdrom

mount 命令,使您能夠訪問您的檔系統,但最現代的Unix系統,自動裝載功能,使這個過程對用戶不可見,無需干預。

用戶和組配額:

用戶和組配額提供的機制,可以限制特定組內的單個用戶或所有用戶使用的空間量由管理員定義的值。

配額圍繞兩個限制,允許用戶採取一些行動,如果量的空間或磁片塊數開始超過管理員定義的限制:

  • Soft Limit: 如果用戶超過定義的限制,有一個寬限期,允許用戶騰出一些空間。

  • Hard Limit: 當達到硬限制,無論在寬限期,沒有進一步的檔或塊可以分配。

有一些命令來管理配額:

命令 描述
quota Displays disk usage and limits for a user of group.
edquota This is a quota editor. Users or Groups quota can be edited using this command.
quotacheck Scan a filesystem for disk usage, create, check and repair quota files
setquota This is also a command line quota editor.
quotaon This announces to the system that disk quotas should be enabled on one or more filesystems.
quotaoff This announces to the system that disk quotas should be disabled off one or more filesystems.
repquota This prints a summary of the disc usage and quotas for the specified file systems

您可以使用 聯機幫助幫助 提到的每個命令的語法檢查完成。


上一篇: Unix 正則運算式SED 下一篇: UNIX 用戶管理