Unix 简明教程

Linux - Directories

Linux 目录是一个文件,其唯一的工作是存储文件名和相关信息。所有文件(无论是普通文件、特殊文件还是目录文件)都包含在目录中。

A Linux Directory is a file the solo job of which is to store the file names and the related information. All the files, whether ordinary, special, or directory, are contained in directories.

本教程将详细讨论 Linux/Unix 中的目录管理。

This tutorial will discuss in detail about directories management in Linux/Unix.

Linux 使用了一种层级结构来组织文件和目录。这种结构通常称为目录树。树只有一个根节点,即斜杠字符 ( / ),所有其他目录都包含在它的下面。

Linux uses a hierarchical structure for organizing files and directories. This structure is often referred to as a directory tree. The tree has a single root node, the slash character (/), and all other directories are contained below it.

Linux Directory Structure

文件系统的最高级别是 / 或根目录。所有其他文件和目录都存在于根目录之下。以下是直接位于根目录 (/) 之下的常见目录的列表:

The highest level of the file system is the / or root directory. All other files and directories exist under the root directory. The following is a listing of common directories that are directly under the root (/) directory:

Directory

Description

/bin

important binary applications

/boot

boot configuration files, kernels, and other files needed at boot time.

/dev

System device files.

/etc

configuration files, startup scripts, etc.

/home

List of home directories for different users

/lib

system libraries, shared libraries

/lost+found

a lost+found system for files that exist under the root (/) directory

/media

automatically mounted (loaded) partitions on your hard drive and removable media such as CDs, digital cameras, etc.

/mnt

manually mounted filesystems on your hard drive

/opt

3rd part applications to be installed

/proc

Maintains information about the state of the system, including currently running processes.

/root

root user’s home directory.

/sbin

important system binaries

/srv

contain files that are served to other systems

/sys

system files

/tmp

temporary files

/usr

applications and files that are mostly available for all users to access

/var

variable files such as logs and databases

Home Directory

第一次登录时所在的目录称为主目录。你将在主目录及其子目录(用于整理文件)中进行大部分工作。

The directory in which you find yourself when you first login is called your home directory. You will be doing much of your work in your home directory and subdirectories that you’ll be creating to organize your files.

你可以随时使用以下命令进入你的主目录:

You can go in your home directory anytime using the following command −

$cd ~
$

此处 ~ 表示主目录。假设你必须进入其他用户的目录,请使用以下命令:

Here ~ indicates the home directory. Suppose you have to go in any other user’s home directory, use the following command −

$cd ~username
$

要进入你的上一个目录,可以使用以下命令:

To go in your last directory, you can use the following command −

$cd -
$

Absolute/Relative Pathnames

目录按层次结构排列,根 (/) 在最上面。目录树中任何文件的位置都由其路径名来描述。

Directories are arranged in a hierarchy with root (/) at the top. The position of any file within the hierarchy is described by its pathname.

路径名的元素由 / 分隔。如果路径名是相对于根来描述的,则该路径名是绝对路径名,因此绝对路径名总以 / 开头。

Elements of a pathname are separated by a /. A pathname is absolute, if it is described in relation to root, thus absolute pathnames always begin with a /.

以下是一些绝对文件名示例。

Following are some examples of absolute filenames.

/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3

路径名也可以相对于您的当前工作目录。相对路径名永远不会从 / 开始。相对于用户 amrood 的主目录,一些路径名可能看起来像这样 -

A pathname can also be relative to your current working directory. Relative pathnames never begin with /. Relative to user amrood’s home directory, some pathnames might look like this −

../chem/notes
personal/res

../ 表示从当前工作目录后退一级,然后你会发现 chem/notes 。如要随时确定自己在文件系统层次结构中的位置,请输入命令 pwd 以打印当前工作目录

Here ../ means go back one level from the current working directory and then you will find chem/notes. To determine where you are within the filesystem hierarchy at any time, enter the command pwd to print the current working directory −

$pwd
/user0/home/amrood

$

Listing Directories

要列出目录中的文件,可以使用以下语法 -

To list the files in a directory, you can use the following syntax −

$ls dirname

以下是列出 (ls) /usr/local 目录中包含的所有文件的示例 -

Following is the example to list all the files contained in /usr/local directory −

$ls /usr/local

X11       bin          gimp       jikes       sbin
ace       doc          include    lib         share
atalk     etc          info       man         ami

Creating Directories

我们现在将了解如何创建目录。目录是通过以下命令创建的 -

We will now understand how to create directories. Directories are created by the following command −

$mkdir dirname

此处,directory 是您要创建的目录的绝对或相对路径名。例如,命令 -

Here, directory is the absolute or relative pathname of the directory you want to create. For example, the command −

$mkdir mydir
$

在当前目录中创建一个目录 mydir 。这是另一个示例 -

Creates the directory mydir in the current directory. Here is another example −

$mkdir /tmp/test-dir
$

此命令在目录 /tmp 中创建目录 test-dir 。如果 mkdir 命令成功创建了请求的目录,则不会产生任何输出。

This command creates the directory test-dir in the /tmp directory. The mkdir command produces no output if it successfully creates the requested directory.

如果您在命令行上给出了多个目录, mkdir 将创建每个目录。例如,-

If you give more than one directory on the command line, mkdir creates each of the directories. For example, −

$mkdir docs pub
$

在当前目录下创建目录 docs 和 pub。

Creates the directories docs and pub under the current directory.

Creating Parent Directories

我们现在将了解如何创建父目录。有时,当您要创建目录时,它的父目录或目录可能不存在。在这种情况下, mkdir 会发出如下错误消息 -

We will now understand how to create parent directories. Sometimes when you want to create a directory, its parent directory or directories might not exist. In this case, mkdir issues an error message as follows −

$mkdir /tmp/amrood/test
mkdir: Failed to make directory "/tmp/amrood/test";
No such file or directory
$

在这种情况下,可以将 -p 选项指定给 mkdir 命令。它会为您创建所有必要的目录。例如 -

In such cases, you can specify the -p option to the mkdir command. It creates all the necessary directories for you. For example −

$mkdir -p /tmp/amrood/test
$

以上命令创建所有必需的父目录。

The above command creates all the required parent directories.

Removing Directories

可以使用 rmdir 命令以如下方式删除目录 -

Directories can be deleted using the rmdir command as follows −

$rmdir dirname
$

Note - 要删除目录,请确保它为空,这意味着该目录中不应有任何文件或子目录。

Note − To remove a directory, make sure it is empty which means there should not be any file or sub-directory inside this directory.

您可以一次删除多个目录,如下所示 -

You can remove multiple directories at a time as follows −

$rmdir dirname1 dirname2 dirname3
$

如果以上目录为空,则该命令将删除目录 dirname1、dirname2 和 dirname3。如果成功, rmdir 命令不会产生输出。

The above command removes the directories dirname1, dirname2, and dirname3, if they are empty. The rmdir command produces no output if it is successful.

Changing Directories

除了更改到主目录之外,还可以使用 cd 命令执行更多操作。您可以通过指定有效的绝对或相对路径来使用它更改到任何目录。语法如下 -

You can use the cd command to do more than just change to a home directory. You can use it to change to any directory by specifying a valid absolute or relative path. The syntax is as given below −

$cd dirname
$

此处, dirname 是你希望更改为的目录名称。例如,命令 −

Here, dirname is the name of the directory that you want to change to. For example, the command −

$cd /usr/local/bin
$

更改为目录 /usr/local/bin 。在这个目录中,你可以使用以下路径进行 cd 至目录 /usr/home/amrood

Changes to the directory /usr/local/bin. From this directory, you can cd to the directory /usr/home/amrood using the following relative path −

$cd ../../home/amrood
$

Renaming Directories

mv (move) 命令还可以用于重命名目录。语法如下 −

The mv (move) command can also be used to rename a directory. The syntax is as follows −

$mv olddir newdir
$

你可以将目录 mydir 重命名为 yourdir ,如下 −

You can rename a directory mydir to yourdir as follows −

$mv mydir yourdir
$

The directories . (dot) and .. (dot dot)

filename . (点)表示当前工作目录; filename .. (双点)表示高于当前工作目录一级,通常称为父目录。

The filename . (dot) represents the current working directory; and the filename .. (dot dot) represents the directory one level above the current working directory, often referred to as the parent directory.

如果我们输入命令来显示当前工作目录/文件的列表并使用 -a option 来列出所有文件和 -l option 提供长列表,我们将会收到以下结果。

If we enter the command to show a listing of the current working directories/files and use the -a option to list all the files and the -l option to provide the long listing, we will receive the following result.

$ls -la
drwxrwxr-x    4    teacher   class   2048  Jul 16 17.56 .
drwxr-xr-x    60   root              1536  Jul 13 14:18 ..
----------    1    teacher   class   4210  May 1 08:27 .profile
-rwxr-xr-x    1    teacher   class   1948  May 12 13:42 memo
$