命令行星期二 —— 第二篇
命令行 命令行星期二
Hi，极客们！


让我们来更新一下我们的记忆。[上周](http://linux.cn/article-3300-1.html)，我们学习了一些基础命令，了解了shell是什么，同时介绍了我们CLI的星期二系列。


今天的菜单将提供点别的东西：通过文件系统导航。


现在，我设法找到最好的图片是从一个叫[devopsbootcamp](http://devopsbootcamp.readthedocs.org/)的网站。你可以在上面找到他们其余的教程。但无论如何，这是一个关于Linux根文件系统的看起来非常不错的图。


![Filesys](/data/attachment/album/201407/07/233707ujanspjf9bq77dfa.jpg)


例如，在上面的图片说明中，你的用户目录（你通常用来存储你的电影，音乐，文档等）是位于/home文件夹下。 /home文件夹位于/。然后，/下有个 /etc 文件夹，其中文件大部分为配置文件。无论如何，你可以在这里找到详细的描述，因为我们将进入这些文件夹来了解他们的功能，直到我们开始使用和配置它们。今天是仅用于导航。而关于这一点，让我们来开始今天的第一个命令...


### pwd


pwd，或者 ‘Print Working Directory’，当你觉得在文件丛林之中迷失了方向时是一个非常有用的命令。在任何给定时刻，键入pwd命令，瞧！这是你到达这个文件夹的完整路径。在电影《异次元杀阵（The Cube）》里的那些家伙总在用它，这些笨蛋！


![](/data/attachment/album/201407/07/233559w224vpnrb3s22f45.gif)


想象一下，自己在一个巨大的公寓里面从一个房间走到另一个房间房间，迷路了。 pwd就像面包屑指引着你到你的出发点，这样你就不会在文件夹迷宫里面失去你的方向！


![pwd](/data/attachment/album/201407/07/233709fguylz5yopyfzp44.png)


### cd


现在你学习了如果想知道自己在哪个目录的pwd命令的用法。现在，你要做的下一步骤就是移动到另一个目录。比方说，你在你的home文件夹下有一个文件夹（目录），你要将你的绝密的东东放到里面。要这样做，你需要使用用'cd'命令。 cd，或‘Change Directory’，将改变所处目录的位置。你怎么使用它呢？简单，键入cd和你的文件夹路径。比方说，例如，你想从你的主文件夹进入你的Hello Kitty图片集。你输入‘cd /home/username/Hello\ Kitty’。


正如你看到的，我们并没有只使用文件夹名称的空格键。这是因为终端将无法识别它。每当你要导航到它的名称中有空格的文件夹，你**用反斜杠字符，后跟空格**代替它。您也可以不使用反斜杠+空格选项，只是把**整个文件夹名称加引号**，例如，cd /home/username/ "Hello Kitty"。


![cd](/data/attachment/album/201407/07/233710hkkogk9kdc69m355.png)


自己尝试一下。使用cd导航到不同的目录，同时，键入pwd命令，看看一切工作是否如期望的那样。


### 肖茨先生的快捷键


肖茨先生提醒我们也有一些可用的快捷键。


如果你仅键入cd，不带路径，你的终端将从你的工作目录（无论是不是）切换到你的/home文件夹。


同样地，如果你键入 cd ~user\_name 它会带你到你指定的特定用户的主文件夹。


### 下周


下周，我们将进入到下一章 - 我们将学习如何列出文件和目录，查看文本文件和文件的内容，因此会比之前我们已经学习的有更多的工作，但我希望你将会有足够的时间。一条命令又一条命令，如果你没有时间自己学习的话，那让我们在几个月内一起学习基础知识吧！


同时，记得...


### …玩得开心!


P.S.：感谢bwl的评论，我们修正了一个在目录名称中包含空格的文本的一个错误。


P.P.S：GreatEmerald还增加了有关文件层次结构的一些新信息。您可以在[意见](https://news.opensuse.org/2014/06/24/command-line-tuesdays-part-two/comment-page-1/#comment-99186)中阅读。


感谢你们的贡献和更正




---


via: <https://news.opensuse.org/2014/06/24/command-line-tuesdays-part-two/>


译者：[乌龙茶](https://github.com/yechunxiao19) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译，[Linux中国](http://linux.cn/) 荣誉推出


OK
# Command Line Tuesdays - Part Two

24. Jun 2014 | Nenad Latinović | No License

Heya geekos!

Let’s refresh our memories.[ Last week](https://news.opensuse.org/2014/06/10/command-line-tuesdays-part-one/), we skimmed through some basic commands, learned what a shell is actually, and made a steady introduction into our CLI Tuesdays series.

Today’s menu offers something else: navigation through the file system.

Now, the best picture I managed to find on the www is from a site called [devopsbootcamp](//devopsbootcamp.readthedocs.org/). You can find their tutorials and the rest here. But anyway, here’s a very nice diagram of what a linux root filesystem looks like.

For example, as stated in the above picture, your user directory (where you usually store your movies, music, documents etc.) is located in the /home folder. /home folder is located under /. Then you have the /etc folder, where most of the files for configuration are located. Anyway, you can find the detailed description [here](//www.tldp.org/LDP/Linux-Filesystem-Hierarchy/html/), as we’ll not be getting into which folder is for what, until we start using and configuring them. Today is reserved for navigation only. And on that note, let’s get down to the first command of the day…

#### pwd

pwd, or ‘print working directory’ is a very useful command if you think you are lost navigating through the folders. At any given moment, type pwd, and voila! What appears is a complete pathway you took to arrive to this folder. Those guys in The Cube could sure use it, losers.

Imagine yourselves walking from room to room inside a massive apartment, loosing your way. pwd is like the breadcrumb trail leading you to your starting point, so you don’t loose your way inside the folder maze!

#### cd

Now you know in which directory you’re situated thanks to your usage of the pwd command. Now you want to take the next step and move to another directory. Let’s say you have a folder/directory inside your home folder you want to relocate your top secret terminal operation to. For this, you use the ‘cd’ command. cd, or ‘change directory’, will change the location of wanted directory. How do you use it? Simple, type cd and the path to your folder. Let’s say, for example, you want to enter your Hello Kitty picture collection in your home folder. You type ‘cd /home/username/Hello\ Kitty’.

As you see, we didn’t only use the space bar in the folder name. That’s because the terminal won’t recognize it. Whenever you want to navigate to a folder containing a space in it’s name, you** replace it with the backslash character, followed by space**. You can also, without using the backslash+space option, just put the **whole folder name into quotes**, f.ex. cd /home/username/”Hello Kitty”.

Try it out yourself. Navigate to a different directory using cd, and when there, type pwd to see if everything worked as it’s supposed to.

#### Mr. Shotts’ Shottcuts

Mr. Shotts reminds us there are also some shortcuts available.

If you type only cd, without the path following it, your terminal will change your working directory (whatever it may be) to your /home folder.

The same, if you type cd ~*user_name* it will lead you to the home folder of the specific user you stated.

#### Next Week

Next week, we’ll head over to the next chapter - we’ll learn how to list files and directories, view text files and classify file’s contents so it will be a bit more work than we’re used to, but I hope you’ll have enough time. Command by command, and if you haven’t the time to do it yourself, we’ll learn the basics together in a matter of months!

and remember…

### …have a lot of fun!

P.S.: Thanks to bwl’s comment, we fixed an error in the text regarding the space bar in directory names.

P.P.S.: GreatEmerald also added some fresh information about file hierarchy. You can read it in [the comments](https://news.opensuse.org/2014/06/24/command-line-tuesdays-part-two/comment-page-1/#comment-99186).

Thanks for your input/corrections.

**Categories:**
[
Distribution
](/category/distribution)

**Tags:**