Unix 简明教程

Unix / Linux - File System Basics

文件系统是分区或磁盘上的文件的逻辑集合。分区是信息的容器,可以跨越整个硬盘,如果需要的话。

A file system is a logical collection of files on a partition or disk. A partition is a container for information and can span an entire hard drive if desired.

您的硬盘可以有各种分区,这些分区通常只包含一个文件系统,例如一个文件系统容纳 /file system 或另一个包含 /home file system

Your hard drive can have various partitions which usually contain only one file system, such as one file system housing the /file system or another containing the /home file system.

每个分区一个文件系统可以对不同的文件系统进行逻辑维护和管理。

One file system per partition allows for the logical maintenance and management of differing file systems.

在 Unix 中,所有内容都被认为是一个文件,包括物理设备,例如 DVD-ROM、USB 设备和软盘驱动器。

Everything in Unix is considered to be a file, including physical devices such as DVD-ROMs, USB devices, and floppy drives.

Directory Structure

Unix 使用分层文件系统结构,非常像一棵倒置的树,其中根 (/) 位于文件系统的底部,所有其他目录都从那里展开。

Unix uses a hierarchical file system structure, much like an upside-down tree, with root (/) at the base of the file system and all other directories spreading from there.

Unix 文件系统是一组文件和目录,具有以下属性:

A Unix filesystem is a collection of files and directories that has the following properties −

  1. It has a root directory (/) that contains other files and directories.

  2. Each file or directory is uniquely identified by its name, the directory in which it resides, and a unique identifier, typically called an inode.

  3. By convention, the root directory has an inode number of 2 and the lost+found directory has an inode number of 3. Inode numbers 0 and 1 are not used. File inode numbers can be seen by specifying the -i option to ls command.

  4. It is self-contained. There are no dependencies between one filesystem and another.

目录有特定的用途,通常保存相同类型的信息,以便轻松地查找文件。下面是在 Unix 主要版本中存在的目录:

The directories have specific purposes and generally hold the same types of information for easily locating files. Following are the directories that exist on the major versions of Unix −

Sr.No.

Directory & Description

1

/ This is the root directory which should contain only the directories needed at the top level of the file structure

2

/bin This is where the executable files are located. These files are available to all users

3

/dev These are device drivers

4

/etc Supervisor directory commands, configuration files, disk configuration files, valid user lists, groups, ethernet, hosts, where to send critical messages

5

/lib Contains shared library files and sometimes other kernel-related files

6

/boot Contains files for booting the system

7

/home Contains the home directory for users and other accounts

8

/mnt Used to mount other temporary file systems, such as cdrom and floppy for the CD-ROM drive and floppy diskette drive, respectively

9

/proc Contains all processes marked as a file by process number or other information that is dynamic to the system

10

/tmp Holds temporary files used between system boots

11

/usr Used for miscellaneous purposes, and can be used by many users. Includes administrative commands, shared files, library files, and others

12

/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

13

/sbin Contains binary (executable) files, usually for system administration. For example, fdisk and ifconfig utlities

14

/kernel Contains kernel files

Navigating the File System

现在您已经了解了文件系统的基础知识,即可开始导航到所需文件。以下命令用于导航系统:

Now that you understand the basics of the file system, you can begin navigating to the files you need. The following commands are used to navigate the system −

Sr.No.

Command & Description

1

cat filename Displays a filename

2

cd dirname Moves you to the identified directory

3

cp file1 file2 Copies one file/directory to the specified location

4

file filename Identifies the file type (binary, text, etc)

5

find filename dir Finds a file/directory

6

head filename Shows the beginning of a file

7

less filename Browses through a file from the end or the beginning

8

ls dirname Shows the contents of the directory specified

9

mkdir dirname Creates the specified directory

10

more filename Browses through a file from the beginning to the end

11

mv file1 file2 Moves the location of, or renames a file/directory

12

pwd Shows the current directory the user is in

13

rm filename Removes a file

14

rmdir dirname Removes a directory

15

tail filename Shows the end of a file

16

touch filename Creates a blank file or modifies an existing file or its attributes

17

whereis filename Shows the location of a file

18

which filename Shows the location of a file if it is in your PATH

您可以使用 Manpage Help 查看此处提到的每个命令的完整语法。

You can use Manpage Help to check complete syntax for each command mentioned here.

The df Command

管理分区空间的第一种方法是使用 df (disk free) 命令。命令 df -k (disk free) 显示 disk space usage in kilobytes ,如下所示:

The first way to manage your partition space is with the df (disk free) command. The command df -k (disk free) displays the disk space usage in kilobytes, as shown below −

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

某些目录,如 /devices 在 kbytes 中显示 0,使用的和可用的列以及容量的 0%。这些是特殊(或虚拟)文件系统,尽管它们驻留在 / 下的磁盘上,但本身不占用磁盘空间。

Some of the directories, such as /devices, shows 0 in the kbytes, used, and avail columns as well as 0% for capacity. These are special (or virtual) file systems, and although they reside on the disk under /, by themselves they do not consume disk space.

df -k 输出在所有 Unix 系统上通常都是相同的。以下是它通常包括的内容:

The df -k output is generally the same on all Unix systems. Here’s what it usually includes −

Sr.No.

Column & Description

1

Filesystem The physical file system name

2

kbytes Total kilobytes of space available on the storage medium

3

used Total kilobytes of space used (by files)

4

avail Total kilobytes available for use

5

capacity Percentage of total space used by files

6

Mounted on What the file system is mounted on

您可以使用 -h (human readable) option 以更容易理解的符号显示输出格式。

You can use the -h (human readable) option to display the output in a format that shows the size in easier-to-understand notation.

The du Command

du (disk usage) command 使您能够指定目录以显示特定目录的磁盘空间使用情况。

The du (disk usage) command enables you to specify directories to show disk space usage on a particular directory.

如果您想确定某个目录占用了多少空间,此命令很有用。以下命令显示每个目录消耗的块数。根据您的系统,单个块可能占用 512 字节或 1 千字节。

This command is helpful if you want to determine how much space a particular directory is taking. The following command displays number of blocks consumed by each directory. A single block may take either 512 Bytes or 1 Kilo Byte depending on your system.

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

-h 选项能让输出更易于理解 −

The -h option makes the output easier to comprehend −

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

Mounting the File System

文件系统必须被挂载才能被系统使用。要查看系统当前挂载(可用于使用)的内容,请使用以下命令 −

A file system must be mounted in order to be usable by the system. To see what is currently mounted (available for use) on your system, use the following command −

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

按照 Unix 公约, /mnt 目录是临时挂载(例如 CDROM 驱动器、远程网络驱动器和软盘驱动器)的位置。如果需要挂载文件系统,则可按以下语法使用 mount 命令 −

The /mnt directory, by the Unix convention, is where temporary mounts (such as CDROM drives, remote network drives, and floppy drives) are located. If you need to mount a file system, you can use the mount command with the following syntax −

mount -t file_system_type device_to_mount directory_to_mount_to

例如,如果要将 CD-ROM 挂载到 /mnt/cdrom 目录,则可输入 −

For example, if you want to mount a CD-ROM to the directory /mnt/cdrom, you can type −

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

这假设您的 CD-ROM 设备名为 /dev/cdrom ,并且您希望将其挂载到 /mnt/cdrom 。有关更具体的信息,请参阅 mount 手册页,或在命令行中输入 mount -h 以获取帮助信息。

This assumes that your CD-ROM device is called /dev/cdrom and that you want to mount it to /mnt/cdrom. Refer to the mount man page for more specific information or type mount -h at the command line for help information.

挂载后,您可以使用 cd 命令通过刚刚创建的挂载点来导航新可用的文件系统。

After mounting, you can use the cd command to navigate the newly available file system through the mount point you just made.

Unmounting the File System

要卸载(移除)系统中的文件系统,请使用 umount 命令识别挂载点或设备。

To unmount (remove) the file system from your system, use the umount command by identifying the mount point or device.

例如, to unmount cdrom ,使用以下命令 −

For example, to unmount cdrom, use the following command −

$ umount /dev/cdrom

mount command 使您能够访问自己的文件系统,但在大多数现代 Unix 系统上, automount function 使此进程对于用户来说不可见,无需介入。

The mount command enables you to access your file systems, but on most modern Unix systems, the automount function makes this process invisible to the user and requires no intervention.

User and Group Quotas

用户配额和组配额提供了限制单个用户或特定组内所有用户所用空间量的机制,可限制为管理员定义的值。

The user and group quotas provide the mechanisms by which the amount of space used by a single user or all users within a specific group can be limited to a value defined by the administrator.

配额围绕两个限制运行,在空间量或磁盘块数开始超过管理员定义的限制时,允许用户采取一些措施 −

Quotas operate around two limits that allow the user to take some action if the amount of space or number of disk blocks start to exceed the administrator defined limits −

  1. Soft Limit − If the user exceeds the limit defined, there is a grace period that allows the user to free up some space.

  2. Hard Limit − When the hard limit is reached, regardless of the grace period, no further files or blocks can be allocated.

有多个用于管理配额的命令 −

There are a number of commands to administer quotas −

Sr.No.

Command & Description

1

quota Displays disk usage and limits for a user of group

2

edquota This is a quota editor. Users or Groups quota can be edited using this command

3

quotacheck Scans a filesystem for disk usage, creates, checks and repairs quota files

4

setquota This is a command line quota editor

5

quotaon This announces to the system that disk quotas should be enabled on one or more filesystems

6

quotaoff This announces to the system that disk quotas should be disabled for one or more filesystems

7

repquota This prints a summary of the disc usage and quotas for the specified file systems

您可以使用 Manpage Help 查看此处提到的每个命令的完整语法。

You can use Manpage Help to check complete syntax for each command mentioned here.