## # 文件系统中跳转

The first thing we need to learn to do (besides just typing) is how to navigate the file system on our Linux system. In this chapter we will introduce the following commands:

• pwd - Print name of current working directory

• cd - Change directory

• ls - List directory contents

• pwd — 打印出当前工作目录名

• cd — 更改目录

• ls — 列出目录内容

### # 理解文件系统树

Like Windows, a Unix-like operating system such as Linux organizes its files in what is called a hierarchical directory structure. This means that they are organized in a tree-like pattern of directories (sometimes called folders in other systems), which may contain files and other directories. The first directory in the file system is called the root directory. The root directory contains files and subdirectories, which contain more files and subdirectories and so on and so on.

Note that unlike Windows, which has a separate file system tree for each storage device, Unix-like systems such as Linux always have a single file system tree, regardless of how many drives or storage devices are attached to the computer. Storage devices are attached (or more correctly, mounted) at various points on the tree according to the whims of the system administrator, the person (or persons) responsible for the maintenance of the system.

### # 当前工作目录

Most of us are probably familiar with a graphical file manager which represents the file system tree as in Figure 1. Notice that the tree is usually shown upended, that is, with the root at the top and the various branches descending below.

\ 图1: 由图形化文件管理器显示的文件系统树 {: .figure}

However, the command line has no pictures, so to navigate the file system tree we need to think of it in a different way.

Imagine that the file system is a maze shaped like an upside-down tree and we are able to stand in the middle of it. At any given time, we are inside a single directory and we can see the files contained in the directory and the pathway to the directory above us (called the parent directory) and any subdirectories below us. The directory we are standing in is called the current working directory. To display the current working directory, we use the pwd (print working directory) command.

[me@linuxbox ~]$pwd /home/me  When we first log in to our system (or start a terminal emulator session) our current working directory is set to our home directory. Each user account is given its own home directory and when operating as a regular user, the home directory is the only place the user is allowed to write files. 当我们首次登录系统（或者启动终端仿真器会话）后，当前工作目录是我们的家目录。 每个用户都有他自己的家目录，当用户以普通用户的身份操控系统时，家目录是唯一 允许用户对文件进行写入的地方。 ### # 列出目录内容 To list the files and directories in the current working directory, we use the ls command. 列出一个目录包含的文件及子目录，使用 ls 命令。 [me@linuxbox ~]$ ls
Desktop Documents Music Pictures Public Templates Videos


Actually, we can use the ls command to list the contents of any directory, not just the current working directory, and there are many other fun things it can do as well. We'll spend more time with ls in the next chapter.

### # 更改当前工作目录

To change your working directory (where we are standing in our tree-shaped maze) we use the cd command. To do this, type cd followed by the pathname of the desired working directory. A pathname is the route we take along the branches of the tree to get to the directory we want. Pathnames can be specified in one of two different ways; as absolute pathnames or as relative pathnames. Let's deal with absolute pathnames first.

### # 绝对路径

An absolute pathname begins with the root directory and follows the tree branch by branch until the path to the desired directory or file is completed. For example, there is a directory on your system in which most of your system's programs are installed. The pathname of the directory is /usr/bin. This means from the root directory (represented by the leading slash in the pathname) there is a directory called "usr" which contains a directory called "bin".

[me@linuxbox ~]$cd /usr/bin [me@linuxbox bin]$ pwd
/usr/bin
[me@linuxbox bin]$ls ...Listing of many, many files ...  Now we can see that we have changed the current working directory to /usr/bin and that it is full of files. Notice how the shell prompt has changed? As a convenience, it is usually set up to automatically display the name of the working directory. 我们把工作目录转到 /usr/bin 目录下，里面装满了文件。注意 shell 提示符是怎样改变的吗？ 为了方便，通常终端提示符自动显示工作目录名。 ### # 相对路径 Where an absolute pathname starts from the root directory and leads to its destination, a relative pathname starts from the working directory. To do this, it uses a couple of special symbols to represent relative positions in the file system tree. These special symbols are "." (dot) and ".." (dot dot). 绝对路径从根目录开始，直到它的目的地，而相对路径开始于工作目录。 为了做到这个（用相对路径表示）， 我们在文件系统树中用一对特殊符号来表示相对位置。 这对特殊符号是 "." (点) 和 ".." (点点)。 The "." symbol refers to the working directory and the ".." symbol refers to the working directory's parent directory. Here is how it works. Let's change the working directory to /usr/bin again: 符号 "." 指的是工作目录，".." 指的是工作目录的父目录。下面的例子说明怎样使用它。 让我们再次把工作目录切换到 /usr/bin： [me@linuxbox ~]$ cd /usr/bin
[me@linuxbox bin]$pwd /usr/bin  Okay, now let's say that we wanted to change the working directory to the parent of /usr/bin which is /usr. We could do that two different ways. Either with an absolute pathname: 好了，比方说我们想更改工作目录到 /usr/bin 的父目录 /usr。可以通过两种方法来实现。可以使用以下绝对路径名： [me@linuxbox bin]$ cd /usr
[me@linuxbox usr]$pwd /usr  Or, with a relative pathname: 或者， 也可以使用相对路径： [me@linuxbox bin]$ cd ..
[me@linuxbox usr]$pwd /usr  Two different methods with identical results. Which one should we use? The one that requires the least typing! 两种不同的方法，一样的结果。我们应该选哪一个呢？ 选输入量最少的那个！ Likewise, we can change the working directory from /usr to /usr/bin in two different ways. Either using an absolute pathname: 同样地，从目录 /usr/ 到 /usr/bin 也有两种途径。可以使用绝对路径： [me@linuxbox usr]$ cd /usr/bin
[me@linuxbox bin]$pwd /usr/bin  Or, with a relative pathname: 或者，也可以用相对路径： [me@linuxbox usr]$ cd ./bin
[me@linuxbox bin]$pwd /usr/bin  Now, there is something important that I must point out here. In almost all cases, you can omit the "./". It is implied. Typing: 有一件很重要的事，我必须指出来。在几乎所有的情况下，你可以省略"./"。它是隐含的。输入： [me@linuxbox usr]$ cd bin


does the same thing. In general, if you do not specify a pathname to something, the working directory will be assumed.

### # 有用的快捷键

In table 3-1 we see some useful ways the current working directory can be quickly changed.

Table 3-1: cd Shortcuts
Shortcut Result
cd Changes the working directory to your home directory.
cd - Changes the working directory to the previous working directory.
cd ~user_name Changes the working directory to the home directory of user_name. For example, cd ~bob will change the directory to the home directory of user “bob.”

cd 更改工作目录到你的家目录。
cd - 更改工作目录到先前的工作目录。
cd ~user_name 更改工作目录到用户家目录。例如, cd ~bob 会更改工作目录到用户“bob”的家目录。

1. Filenames that begin with a period character are hidden. This only means that ls will not list them unless you say ls -a. When your account was created, several hidden files were placed in your home directory to configure things for your account. Later on we will take a closer look at some of these files to see how you can customize your environment. In addition, some applications place their configuration and settings files in your home directory as hidden files.

2. Filenames and commands in Linux, like Unix, are case sensitive. The filenames “File1” and “file1” refer to different files.

3. Linux has no concept of a “file extension” like some other operating systems. You may name files any way you like. The contents and/or purpose of a file is determined by other means. Although Unix-like operating system don’t use file extensions to determine the contents/purpose of files, some application programs do.

4. Though Linux supports long filenames which may contain embedded spaces and punctuation characters, limit the punctuation characters in the names of files you create to period, dash, and underscore. Most importantly, do not embed spaces in filenames. If you want to represent spaces between words in a filename, use underscore characters. You will thank yourself later.

1. 以 "." 字符开头的文件名是隐藏文件。这仅表示，ls 命令不能列出它们， 用 ls -a 命令就可以了。当你创建帐号后，几个配置帐号的隐藏文件被放置在 你的家目录下。稍后，我们会仔细研究一些隐藏文件，来定制你的系统环境。 另外，一些应用程序也会把它们的配置文件以隐藏文件的形式放在你的家目录下面。

2. 文件名和命令名是大小写敏感的。文件名 “File1” 和 “file1” 是指两个不同的文件名。

3. Linux 没有“文件扩展名”的概念，不像其它一些系统。可以用你喜欢的任何名字 来给文件起名。文件内容或用途由其它方法来决定。虽然类 Unix 的操作系统， 不用文件扩展名来决定文件的内容或用途，但是有些应用程序会。

4. 虽然 Linux 支持长文件名，文件名可能包含空格，标点符号，但标点符号仅限 使用 “.”，“－”，下划线。最重要的是，不要在文件名中使用空格。如果你想表示词与 词间的空格，用下划线字符来代替。过些时候，你会感激自己这样做。